C++ Write a program that uses while loops to perform the following steps: a. Pro
ID: 3665522 • Letter: C
Question
C++
Write a program that uses while loops to perform the following steps:
a. Prompt the user to input two integers: firstNum and secondNum (firstNum must be less than
secondNum).
b. Output all odd numbers between firstNum and secondNum.
c. Output the sum of all even numbers between firstNum and secondNum.
d. Output the numbers and their squares between 1 and 10. (This has nothing to do with the user
input.)
e. Output the sum of the square of the odd numbers between firstNum and secondNum. f. Output all uppercase letters. (This also has nothing to do with user input.)
Explanation / Answer
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int firstNum, secondNum, oddNumCount = 0, i, evenSum = 0, j, k, l;
double oddSum = 0;
// a. prompt the user to input two integers: firstNum and secondNum
//(firstNum must be less than secondNum)
cout << "Please enter two integers in ascending order: ";
cin >> firstNum >> secondNum;
cout << endl;
if (!cin)
{
cout << "Wrong data type! Program terminated... " << endl;
return 1;
}
if (secondNum < firstNum)
{
cout << "Integers must be in ascending order! Program terminated... " << endl;
return 1;
}
cout << "Your first number is: " << firstNum << " and your second number is: " << secondNum << " " << endl;
// must determine if firstNum is odd or even
//if (firstNum % 2 > 0)
// cout << firstNum << " is odd.";
//else
// cout << firstNum << " is even.";
// b. output all odd numbers between firstNum and secondNum
cout << "Odd numbers are: ";
for (i = firstNum + 1; i < secondNum; i++)
{
if (i % 2 != 0)
{
cout << i << " ";
oddNumCount += 1;
//cout << "oddNumCount is: " << oddNumCount << endl;
}
}
//cout << "oddNumCount after the loop is: " << oddNumCount;
if (oddNumCount == 0)
cout << "None";
cout << " ";
// c. output the sum of all even numbers between firstNum and secondNum
cout << "The sum of the even numbers is: ";
for (i = firstNum + 1; i < secondNum; i++)
{
if ((i % 2) == 0)
//cout << i << " + ";
evenSum += i;
}
cout << evenSum << endl;
cout << " ";
// d. output the numbers and their square between 1 and 10
cout << setw(8) << left << "Number" << setw(6) << right << "Square" << endl;
for (j=1; j<=10; j++)
{
cout << setw(3) << setfill(' ') << right << j << setw(9) << setfill('.') << right << pow(j,2) << endl;
}
cout << " ";
// e. output the sum of the square of the odd numbers between firstNum and secondNum
for (k = firstNum + 1; k < secondNum; k++)
{
if (k % 2 != 0)
{
//cout << k << " and " << k << " squared is " << pow(k,2) << endl;
oddSum = oddSum + pow(k,2);
}
}
cout << "The sum of the square of the odd numbers between " << firstNum << " and " << secondNum << " is: " << oddSum << endl;
cout << " ";
// f. output all the uppercase letters
cout << "Capital letters..." << endl;
for (l=65; l<=90; l++)
{
cout << static_cast<char>(l) << " ";
}
cout << " " << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.