LoopWithStep.cpp (2pt) This program will display a series of 15 numbers using ne
ID: 3831924 • Letter: L
Question
LoopWithStep.cpp (2pt)
This program will display a series of 15 numbers using nested loops: while and do while
Write a program that asks the user to enter an integer number. This number is the initial value that start the series of numbers. Then asks the user for the step value, which will be added to the previous number to determine the next number in the series.
The program displays the user’s number and 14 numbers that have been incremented by the step value. Place a comma between the numbers.
After the loops have finished, display a goodbye message to the screen.
Sample run
Please enter a number: -9
Please enter the increment value (step): 3
Increment results
-9, -6, -3, 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33
Go again? yes/no: yes
Please enter a number: 10
Please enter the increment value (step): 5
Increment results
10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80
Go again? yes/no: no
Goodbye for now.
!!! Please use C++
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
string choice = "yes";
int n, inc;
do{
int i= 0;
cout<<"Please enter a number: ";
cin >> n;
cout<<"Please enter the increment value (step): ";
cin >> inc;
cout<<"Increment results "<<endl;
while(i<15){
cout<<n<<" ";
n = n + inc;
i++;
}
cout<<endl;
cout<<"Go again? yes/no: ";
cin >> choice;
}while(choice=="yes");
cout<<"Goodbye for now."<<endl;
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Please enter a number: -9
Please enter the increment value (step): 3
Increment results
-9 -6 -3 0 3 6 9 12 15 18 21 24 27 30 33
Go again? yes/no: yes
Please enter a number: 10
Please enter the increment value (step): 5
Increment results
10 15 20 25 30 35 40 45 50 55 60 65 70 75 80
Go again? yes/no: no
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.