Answer to Question #297148 in C++ for Sadala

Question #297148

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.


1
Expert's answer
2022-02-12T18:30:42-0500
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>








using namespace std;


void main_for()
{
	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");
		exit(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';
}
void main_while()
{
	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");
		exit(0);
	}
	//
	std::cout << "All odd numbers between firstNum and secondNum : ";
	int i = firstNum;
	while ( i < secondNum)
	{
		if (i % 2 == 1)
		{
			std::cout << i << '\t';
		}
		i++;
	}
	std::cout << '\n';
	//
	std::cout << "The sum of all even numbers between firstNum and secondNum : ";
	int sum = 0;
	i = firstNum;
	while (i < secondNum)
	{
		if (i % 2 == 0)
		{
			sum += i;
		}
		i++;
	}
	std::cout << sum << '\n';
	//
	std::cout << "The numbers and their squares between firstNum and secondNum. : ";
	i = firstNum;
	while (i < secondNum)
	{
		std::cout << "{" << i << " , " << i * i << "} ";
		i++;
	}
	std::cout << '\n';
}
void main_dowhile()
{
	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");
		exit(0);
	}
	//
	std::cout << "All odd numbers between firstNum and secondNum : ";
	int i = firstNum;
	do
	{
		if (!(i < secondNum))
		{
			break;
		}
		if (i % 2 == 1)
		{
			std::cout << i << '\t';
		}
		i++;
	} while (i < secondNum);
	std::cout << '\n';
	//
	std::cout << "The sum of all even numbers between firstNum and secondNum : ";
	int sum = 0;
	i = firstNum;
	
	do
	{
		if (!(i < secondNum))
		{
			break;
		}
		if (i % 2 == 0)
		{
			sum += i;
		}
		i++;
	}while (i < secondNum);
	std::cout << sum << '\n';
	//
	std::cout << "The numbers and their squares between firstNum and secondNum. : ";
	i = firstNum;
	
	do
	{
		if (!(i < secondNum))
		{
			break;
		}
		std::cout << "{" << i << " , " << i * i << "} ";
		i++;
	}
	while (i < secondNum);
	std::cout << '\n';
}


int main()
{
	


	main_dowhile();
	main_for();
	main_while();


	system("pause");

	return 0;
}

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