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 loo
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int firstNum=0, secondNum=0;
std::cout << "Enter firstNum : ";
std::cin >> firstNum;
std::cout << "Enter firstNum : ";
std::cin >> secondNum;
if (!(firstNum < secondNum))
{
std::cout << "firstNum > secondNum , error\n";
system("pause");
return 0;
}
//
std::cout << "All odd numbers between firstNum and secondNum : ";
for (int i = firstNum; i < secondNum; i++)
{
if (i % 2 == 1)
{
std::cout << i << '\t';
}
}
std::cout << '\n';
//
std::cout << "The sum of all even numbers between firstNum and secondNum : ";
int sum = 0;
for (int i = firstNum; i < secondNum; i++)
{
if (i % 2 == 0)
{
sum += i;
}
}
std::cout << sum<<'\n';
//
std::cout << "The numbers and their squares between firstNum and secondNum. : ";
for (int i = firstNum; i < secondNum; i++)
{
std::cout <<"{" << i << " , "<<i*i<<"} ";
}
std::cout << '\n';
system("pause");
return 0;
}
Comments
Leave a comment