a. 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.
using for loops.
Declare Integer firstNum
Declare Integer secondNum
Declare Integer i
Declare Integer sumAllEvenNumbers
Declare Integer sumSquareOddNumbers
Set sumAllEvenNumbers = 0
Set sumSquareOddNumbers = 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 sumAllEvenNumbers = sumAllEvenNumbers + i
End If
End For
Display "The sum of all even numbers between firstNum and secondNum: ", sumAllEvenNumbers
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 sumSquareOddNumbers = sumSquareOddNumbers + i * i
End If
End For
Display "The sum of the square of the odd numbers between firstNum and secondNum: ", sumSquareOddNumbers
do. . .while loops.
Declare Integer firstNum
Declare Integer secondNum
Declare Integer i
Declare Integer sumAllEvenNumbers
Declare Integer sumSquareOddNumbers
Set sumAllEvenNumbers = 0
Set sumSquareOddNumbers = 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 sumAllEvenNumbers = sumAllEvenNumbers + i
End If
Set i = i + 1
While i <= secondNum
Display "The sum of all even numbers between firstNum and secondNum: ", sumAllEvenNumbers
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 sumSquareOddNumbers = sumSquareOddNumbers + 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: ", sumSquareOddNumbers
Comments
Leave a comment