Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C++ that uses while loops performing the following steps: Mak

ID: 3885584 • Letter: W

Question

Write a program in C++ that uses while loops performing the following steps: Make sure to use while loop only. No "for loop."

A.Prompt the user to input two integers: firstNum and secondNum(firstNum must be less than secondNum).

B.Output the numbers between (inclusive) firstNum and secondNum

C.Output the sum of the numbers between (inclusive) firstNum and secondNum.

D.Output all odd numbers between (exclusive) firstNum and secondNum.

I am a beginner in C++ so try to keep it basic and explain as much as possible. Thank you!

Explanation / Answer

#include<iostream>
using namespace std;
int main(){
  
//1.Prompt the user to input two integers: firstNum and secondNum(firstNum must be less than secondNum).
int firstNum,secondNum,i,sum=0;
cout << "Enter two numbers:";
cin >> firstNum;
cin >> secondNum;
//ask secondNum input untill it's less than firstNum
while(secondNum < firstNum){
cout << "secondNum should be greater than firstNum" << endl << "Enter secondNum:";
cin >> secondNum;
}
  
//2.Output the numbers between (inclusive) firstNum and secondNum, using <= for inclusive condition
i = firstNum;
while(i<=secondNum){
// post increment is used to print the number and increment.
cout << i++;
}
cout << endl;
  
//3.Output the sum of the numbers between (inclusive) firstNum and secondNum.
i=firstNum;
while(i<=secondNum){
// post increment after adding the number to sum
sum += i++;
}
cout << "sum of numbers between : " << sum << endl;
  
//Output all odd numbers between (exclusive) firstNum and secondNum.
i=firstNum+1;
while(i<secondNum){
// check if number is odd
if(i%2 != 0){
cout << i;
}
i++;
}
cout << endl;
return 0;
}
/*
sample output
Enter two numbers: 2 5
2345
sum of numbers between : 14
3
*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote