2.2 Code Practice: Question 1
Ask the user to input an integer. Print out the next three consecutive numbers.
Answers
Answer:
num1=int(raw_input(enter your number))
print"next three consecutive numbers are",num1+1,num1+2,num1+3
Answer:
Following is the C++ code for the problem.
#include <iostream>
using namespace std;
int main() {
int z;
cout<<"Enter the number"<<endl;
cin>>z;//taking the input.
cout<<"Next three consecutive integers are as following:"<<endl;
for(int i=1;i<=3;i++) //for loop to print three integers.
{
cout<<++z<<endl;//printing the integers.
}
return 0;
}
Output:-
Enter the number
5
Next three consecutive integers are as following:
6
7
8
Explanation:
I have used for loop to print next 3 consecutive integers after taking the input from the user.Loop is better method then writing three print statements.In the for loop I am printing ++z means print z+1. ++ is an increment operator which increases the value by 1.In our case it is pre increment operator.It first increases the value then operates on it.