Answer to Question #296948 in C++ for Dessigner

Question #296948

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.




1
Expert's answer
2022-02-12T08:06:45-0500
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










C++ code:


#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <cmath>


using namespace std;


// Headers
string toString (double);
int toInt (string);
double toDouble (string);


int main() {
    int firstNum;
    int secondNum;
    int i;
    int sumAllEvenNumbers;
    int sumSquareOddNumbers;
    
    sumAllEvenNumbers = 0;
    sumSquareOddNumbers = 0;
    cout << "Enter firstNum: " << endl;
    cin >> firstNum;
    secondNum = firstNum;
    do {
        cout << "Enter secondNum: " << endl;
        cin >> secondNum;
    } while (firstNum >= secondNum);
    cout << "All odd numbers between firstNum and secondNum:" << endl;
    i = firstNum;
    do {
        if (i % 2 != 0) {
            cout << i << endl;
        } else {
            sumAllEvenNumbers = sumAllEvenNumbers + i;
        }
        i = i + 1;
    } while (i <= secondNum);
    cout << "The sum of all even numbers between firstNum and secondNum: " << sumAllEvenNumbers << endl;
    cout << "The numbers and their squares between firstNum and secondNum:" << endl;
    i = firstNum;
    do {
        cout << i << "^2 = " << i * i << endl;
        if (i % 2 != 2) {
            sumSquareOddNumbers = sumSquareOddNumbers + i * i;
        }
        i = i + 1;
    } while (i <= secondNum);
    cout << "The sum of the square of the odd numbers between firstNum and secondNum: " << sumSquareOddNumbers << endl;
    return 0;
}


// The following implements type conversion functions.
string toString (double value) { //int also
    stringstream temp;
    temp << value;
    return temp.str();
}


int toInt (string text) {
    return atoi(text.c_str());
}


double toDouble (string text) {
    return atof(text.c_str());
}






Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog