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.
for loops
Start
Declare Integer firstNum
Declare Integer secondNum
Declare Integer i
Declare Integer sumEvenNum
Declare Integer sumSqOddNum
Set sumEvenNum = 0
Set sumSqOddNum = 0
Display "Enter firstNum: "
Input firstNum
Set secondNum = firstNum
While firstNum >= secondNum
Display "Enter secondNum: "
Input secondNum
End While
Display "All odd numbers between firstNum and secondNum:"
For i = firstNum To secondNum
If i MOD 2 != 0 Then
Display i
Else
Set sumEvenNum = sumEvenNum + i
End If
End For
Display "The sum of all even numbers between firstNum and secondNum: ", sumEvenNum
Display "The numbers and their squares between firstNum and secondNum:"
For i = firstNum To secondNum
Display i, "^2 = ", i * i
If i MOD 2 != 2 Then
Set sumSqOddNum = sumSqOddNum + i * i
End If
End For
Display "The sum of the square of the odd numbers between firstNum and secondNum: ", sumSqOddNum
Stop
do. . .while loops.
Start
Declare Integer firstNum
Declare Integer secondNum
Declare Integer i
Declare Integer sumEvenNum
Declare Integer sumSqOddNum
Set sumEvenNum = 0
Set sumSqOddNum = 0
Display "Enter firstNum: "
Input firstNum
Set secondNum = firstNum
Do
Display "Enter secondNum: "
Input secondNum
While firstNum >= secondNum
Display "All odd numbers between firstNum and secondNum:"
Set i = firstNum
Do
If i MOD 2 != 0 Then
Display i
Else
Set sumEvenNum = sumEvenNum + i
End If
Set i = i + 1
While i <= secondNum
Display "The sum of all even numbers between firstNum and secondNum: ", sumEvenNum
Display "The numbers and their squares between firstNum and secondNum:"
Set i = firstNum
Do
Display i, "^2 = ", i * i
If i MOD 2 != 2 Then
Set sumSqOddNum = sumSqOddNum + i * i
End If
Set i = i + 1
While i <= secondNum
Display "The sum of the square of the odd numbers between firstNum and secondNum: ", sumSqOddNum
Stop
Comments
Leave a comment