Download csc111_Tut1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Addition wikipedia , lookup

Law of large numbers wikipedia , lookup

Elementary mathematics wikipedia , lookup

Location arithmetic wikipedia , lookup

Transcript
King Saud University
College of Computer and Information Sciences
Information Technology Department
First Semester 1430/143129
CSC 111: Introduction to Programming with Java
TUTORIAL # 1
Chap1: Problem-solving
Give the algorithm or draw the flowchart for the following problems
Exercise#1
Write a program that finds the smallest of several integers. Assume that the first value read
start
specifies the number of values remaining.
I/P:number of integers,integers
Operations: find the smallest (smallest=value)
O/p:smallest
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
Start the program
Print "Enter the number of integers to be processed:”
Read the number and save in the variable ( Num)
Print "Enter the integers:”
Read the number and save in the variable ( Smallest)
Create a variable to hold a counter from 1 to Num.
Initialize the counter to 1.
Loop While the counter is less-than to number
Read the number and save in the variable ( value)
If value less than smallest
Smallest =value
End if
add one to the counter
Now repeat
Print the smallest number.
End of program
Print"enter the number…"
Read num
Print"enter the integers"
Read smallest
Counter=1
no
Counter
< num
yes
Read value
Value<
smallest
no
yes
Smallest=value
Counter=counter+1
Print smallest
end
1
Exercise#2
Write a program that calculates and prints the sum of the even integers from 2 to 30.
I/P:no input
Operations:sum=2+4+6+…..+30
O/p: the summation
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
Start the program
Create a variable to hold the sum.
Initialize the sum to zero.
Create a variable to hold a counter from 2 to 30.
Initialize the counter to 2.
Loop While the counter is less-than-or-equal to 30
add the counter to the sum
add two to the counter.
Now repeat
Print the sum.
End of program
Start
Sum=0 ,counter=2
no
Counter≤30
yes
Sum=sum+counter
Counter=counter+2
Print sum
End
2
Exercise#3
A mail order house sells five different products whose retail prices are shown in the
following table
Product number
Retail price
1
$2.98
2
$4.50
3
$9.98
4
$4.49
5
$6.87
Write a program that reads a series of pairs of numbers as follows:
 Product number
 Quantity sold for one day
Your program should determine the retail price for each product. Your program should
calculate and display the total retail value of all products sold last week.
I/P: product number, Quantity
Operations: total =total +quantity * retail price
O/p: Total
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
Start the program
Create a variable to hold the total.
Initialize the total to zero.
Initialize the day to 1.
Loop While day less or equal to7.
Print day
Print "Enter Enter pairs of product numbers and quantities. Enter -1 for the item number to end input:”
Read the first number and save in the variable (product)
Loop While product not equal to -1
Read the quantity number and save in the variable (quantity)
If product equal to 1
Add to total the Multiplying of quantity by 2.98
Else If product equal to 2
Add to total the Multiplying of quantity by 4.50
Else If product equal to 3
Add to total the Multiplying of quantity by 9.98
Else If product equal to 4
Add to total the Multiplying of quantity by 4.49
Else If product equal to5
Add to total the Multiplying of quantity by 6.87
Else Print” invalid product code”
End if
Read (product)
Now repeat
Print the total.
End of program
3
start
Day=1 ,total=0
no
day ≤=7
yes
Print day
Print"enter the product number,quantity,-1..|"
end"
Read product
no
Product !=1
Day=day+1
yes
Read quantity
no
no
product=1
yes
product=2
no
yes
product=3
no
product=5
Total=toatal+ quantity*4.50
yes
product=4
yes
Total=toatal+
quantity*2.98
Total=toatal+ quantity*4.98
Total=toatal+ quantity*4.49
yes
Total=toatal+ quantity*6.87
Print"Invalid|"
Read product
Print Total
4
End
Exercise#4
Calculate the value of π from the infinite series
π = 4 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 +…
Print a table that shows the value of π approximated by one term of this series by two terms,
by three terms, etc…
I/P:number of times.
Operations:
π = 4 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 +…
O/p: π (n times)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
Start the program
Print “enter number of times:”
Read times and save it in variable (times).
Create a variable to hold pi value.
Initialize the pi to 4.
Create a variable to hold sign.
Initialize sign to 1.
Create a variable to hold a counter from 1 to times.
Initialize the counter to 1.
Loop While the counter is less-than times
Sign=sign*-1
Pi=pi+(sign*4)/(2*counter+1)
print the value of pi.
Now repeat.
End of program.
start
Print"enter number of times"
Read times
Pi=4
Counter=1,sign=1
no
Counter<times
yes
Sign=sign*-1
Pi=pi+(sign*4)/(2*counter+1)
Print pi
Counter=counter+1
End
5
Exercise#5
Your company pays its employees time and a half for all hours worked over 40 hours a
weekd. Employees who earn more than $100.00 a week pay union dues of $25 per week.
Write a program to compute an employee's gross pay and net pay.
I/P: hourly salary, hours
Operations:
hours >40 Gross pay = (( hours - 40 ) * 1.5 * hSalary + hSalary * 40.0 Else Gross Pay = hourly salary *
40.0
Gross Pay > 100Net Pay=Gross Pay-25 Else Net Pay=Gross Pay
O/p:Gross Pay ,Net Pay
1.
2.
3.
4.
5.
6.
7.
Start the program
Print "Enter the hourly salary .Enter -1 to end input”
Read the number and save in the variable ( hSalary)
Print "Enter the total hours worked: "
Read the number and save in the variable ( hours)
If hours greater than 40
Calculate (( hours - 40 ) * 1.5 * hSalary + hSalary * 40.0 and
save it in variable (grossPay)
8.
Else
9.
Calculate hSalary * 40.0 and save it in variable (grossPay)
10.
End If
11. If grossPay greater than 100
12.
Subtract 25 from grossPay and save it in variable (netPay)
13.
Else
14.
GrossPay assigned to Net pay
15.
End If
16. Print grossPay,netPay.
17. End of program
start
Print "Enter the hourly salary
.Enter -1 to end input”
read hSalary
Print "Enter the total hours
worked: "
Read hours
No
Hours>40
yes
Gross=hsalary*40
Gross=(hSalary*40)+((hours40)*1.5*hSalary)
Gross>100
no
Net=gross
yes
Net=gross-25
Print gross,net
End
6
Exercise#6
Write a program that determines the additional state tax owed by an employee. The state
charges a 4% tax on net income. Determine net income by subtracting a $500 allowance for
each dependent from gross income. Your program will read gross income, number of
dependents, and tax amount already deducted. It will then compute the actual tax owed and
print the difference between tax owed and tax deducted followed by the message 'SEND
CHECK' or 'REFUND', depending on whether the difference is positive or negative.
I/P: gross income, dependents, tax amount
Operations:
netIncome=gross-(500*dependent)
actualTax = netIncome*0.04
difference= actualTax-tax
O/p: difference , 'SEND CHECK' or 'REFUND'
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
Start the program
Print "Enter the gross income, dependents, the number of tax amount already deducted:”
Read the number and save in the variable ( gross)
Read the number and save in the variable (dependent)
Read the number and save in the variable ( tax)
Create a variable to hold the netIncome.
Calculate gross-(500*dependent) and save it in variable netIncome.
Create a variable to hold the actualTax
Calculate netIncome*0.04 and save it in variable actualTax
Create a variable to hold the difference.
Start
difference= actualTax-tax
If (Difference) greater than or equal 0
Print difference
Print Enter the gross…"
Print”SEND CHECK”
Else
Print difference*-1
Read gross ,dependent, tax
“REFUND”
End of program
netIncome=gross-(500*dependent)
actualTax=netIncome*0.04
Difference=actualTax-tax
no
difference≥0
Print difference * -1
yes
Print difference
Print "Refund"
Print "Send Check|
end
7