InstructionsAsk the user to input an integer. Print out the next three consecutive numbers.Sample RunEnter an integer: 4567
Question:
Sample Run
Enter an integer: 4
5
6
7
Answers
a = float(input("Enter an integer: "))
print(a + 1)
print (a + 2)
print (a + 3)
Explanation:
Takes any number user inputs, and next three numbers that come out are what the given number is plus one, then two, then three.
For example :
Enter an integer: 4
5
6
7
Codes are given below:
Explanation:
Since the language is not specified, I am writing it in c++
#include <iostream>
using namespace std;
int main() //Start of main function
{
int number;
cout << "Enter your number"<<endl;
cin >> number; //this will take your number as an input
cout <<"The next three numbers are: " << number + 1 << endl; //this will write the next number to your input input
cout << number + 2 << endl;
cout << number + 3 << endl;
return 0;
} //End of main function
Codes are given below:
Explanation:
Since the language is not specified, I am writing it in c++
1) Ask the user to input an integer. Print out the next three consecutive numbers.
#include <iostream>
using namespace std;
int main() //Start of main function
{
int number;
cout << "Enter your number<<endl;
cin >> number; //this will take your number as an input
cout <<"The next three numbers are: " << number + 1 << endl;
cout << number + 2 << endl;
cout << number + 3 << endl;
return 0;
} //End of main function
2) Write a program that accepts three decimal numbers as input and outputs their sum.
#include <iostream>
using namespace std;
int main() //Start of main function
{
int number1, number2, number3;
cout << "Enter your three numbers<<endl;
cin >> number1 >> number2>> number3 ; //this will take your three number as an input
cout <<"The sum of three numbers are: " << number1 + number2+ number3 << endl;
return 0;
} //End of main function
Explanation:
n = int(input("Enter Number : "))
print(n+1,n+2,n+3)
I've included my code in the picture below. Best of luck.
[tex]Ask the user to input an integer. Print out the next three consecutive numbers. Sample Run Enter an[/tex]
num = int(input("Enter an integer: "))
i = 1
while i <= 3:
print(num+i)
i += 1
I've written my code in python 3+. I hope this helps!
The code is written using Python language as it is one of the programming languages introduced by the Edhesive courses.
Answer (Question 1):
num = int(input("Input an integer: "))for i in range(1, 4): print(num + i)Explanation (Question 1):
Line 1 :
Prompt user for input an integer. In Python we can easily use input method to ask user for an input.
However the default data type of the input is a string and therefore we need to enclose our input value within int() to convert the value from string to integer.
Line 3 - 4:
Create a for-loop to loop though the code in Line 4 for three times using the range() function. The range(1, 4) will return a list of numbers, [1, 2, 3] .
In each round of the loops, one number from the list will be taken sequentially and total up with the input number, num and display using print() function.
Answer (Question 2):
counter = 3sum = 0while (counter > 0): num = float(input("Enter a decimal number: ")) sum += num counter = counter - 1print("The sum is " + str(sum))Explanation (Question 2):
Line 1 - 2 :
Create a counter variable and a sum variable and initialize them with 3 and 0, respectively.
Line 4:
Create a while loop and set a condition while counter is bigger than zero, the loop should keep running the code from Line 5-7.
Line 5:
Use input function again to prompt user for a decimal number. This time we need to use float function to convert the input value from string to decimal data type.
Line 6:
Every time when a decimal number is input, the number is added to the sum variable.
Line 7:
Decrement the counter variable by 1. This step is important to ensure the while loop will only be repeated for three times and each loop will only accept one input value from user.
Line 9
Display the sum.