Write algorithm using pseudocode and flowchart that uses while loops to perform the
following steps:
i. Prompt the user to input two integers: firstNum and secondNum note that firstNum
must be less than secondNum.
ii. Output all odd numbers between firstNum and secondNum.
iii. Output the sum of all even numbers between firstNum and secondNum.
iv. Output the numbers and their squares between firstNum and secondNum.
v. Output the sum of the square of the odd numbers between firstNum and secondNum.
b. Redo Exercise (a) using for loops.
c. Redo Exercise (a) using do. . .while loops.
Function Main
Declare Integer firstNum
Declare Integer secondNum
Declare Integer i
Declare Integer sumEvenNum
Declare Integer sumSqOddNum
Assign sumEvenNum = 0
Assign sumSqOddNum = 0
Output "Enter firstNum: "
Input firstNum
Assign secondNum = firstNum
While firstNum>=secondNum
Output "Enter secondNum: "
Input secondNum
End
Output "All odd numbers between firstNum and secondNum:"
For i = firstNum to secondNum
If i%2!=0
Output i
False:
Assign sumEvenNum = sumEvenNum+i
End
End
Output "The sum of all even numbers between firstNum and secondNum: "&sumEvenNum
Output "The numbers and their squares between firstNum and secondNum:"
For i = firstNum to secondNum
Output i & "^2 = " & i*i
If i%2!=2
Assign sumSqOddNum = sumSqOddNum+i*i
End
End
Output "The sum of the square of the odd numbers between firstNum and secondNum: " & sumSqOddNum
End
do. . .while loops.
Function Main
Declare Integer firstNum
Declare Integer secondNum
Declare Integer i
Declare Integer sumEvenNum
Declare Integer sumSqOddNum
Assign sumEvenNum = 0
Assign sumSqOddNum = 0
Output "Enter firstNum: "
Input firstNum
Assign secondNum = firstNum
Loop
Output "Enter secondNum: "
Input secondNum
Do firstNum>=secondNum
Output "All odd numbers between firstNum and secondNum:"
Assign i = firstNum
Loop
If i%2!=0
Output i
False:
Assign sumEvenNum = sumEvenNum+i
End
Assign i = i+1
Do i<=secondNum
Output "The sum of all even numbers between firstNum and secondNum: "&sumEvenNum
Output "The numbers and their squares between firstNum and secondNum:"
Assign i = firstNum
Loop
Output i & "^2 = " & i*i
If i%2!=2
Assign sumSqOddNum = sumSqOddNum+i*i
End
Assign i = i+1
Do i<=secondNum
Output "The sum of the square of the odd numbers between firstNum and secondNum: " & sumSqOddNum
End
Comments
Leave a comment