Input And Output Hackerrank Solution in C++. In this challenge, we practice reading input from stdin and printing output to stdout. In C++. you can read a single whitespace-separated token of input using cin, and print output to stdout using cout. For example, let's say we declare the following variables:
string s;
int n;
and we want to use cin to read the input "High 5" from stdin. We can do this with the following code:
cin >> s >> n;
This reads the first word ("High") from stdin and saves it as string s, then reads the second word ("5") from stdin and saves it as integer n. If we want to print these values to stdout, separated by a space, we write the following code:
cout << s << " " << n << endl;
This code prints the contents of string s, a single space (" "), and then the integer n. We end our line of output with a new line using endl. This results in the following output:
High 5
Task
Read 3 numbers from stdin and print their sum to stdout.
Input Format
One line that contains 3 space-separated integers: a, b, and c.
Constraints
1 <= a, b, c <= 1000
Output Format
Print the sum of the three numbers on a single line.
Sample Input
1 2 7
Sample Output
10
Explanation
The sum of the three numbers is 1 + 2 + 7 = 10.
Submit your solution here: Click here
Input And Output Hackerrank Solution in C++
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
/*Enter your code here. Read input from STDIN. Print output to STDOUT */
int a, b, c;
cin >> a >> b >> c;
if ((a >= 1 and a <= 1000) and(b >= 1 and b <= 1000) and(c >= 1 and c <= 1000))
{
cout << a + b + c;
}
return 0;
}
Input And Output Explanation
This is a very simple problem we have to just use three variables and take a user input and store all three user inputs to already declare variables after that declare the fourth variable for holding all three variables SUM. below is the full explanation with examples.
So as we already know that we have to declare three variables, so instead of declaring three variables Integer or Float we are going to declare all fourth variables Long Double, in this way we can store all types of values instead of a single type of value.
Declare Variables
long double a,b,c,sum=0;
Here we can see that three variable are declared simply declare but the fourth variable sum = 0 we define that is why if any garbage value hold sum then it changes into 0 so we can get our answer simple as we want.
Taking User Input
cin>>a>>b>>c;
storing the value in all three variables by the user, now the next step is Add all values held by all three variables after adding all values store the values in another variable Define Sum. At the end print the SUM (holding the sum of all three variables).
Adding and Printing
sum=a+b+c;
cout<<sum;
As we can see that our main function is an Integer type which means the main function should return some value but in this case, we are not returning any value so we are returning zero here
Returning to Main Function
return 0;
Now all steps of the problem are explained clearly. I hope you got it,
Before pasting the code into the editor make sure you have chosen C++ or if C++ not worked fine then change it into C++14 an editor in the top right drop-down option.
So as we already know that we have to declare three variables, so instead of declaring three variables Integer or Float we are going to declare all fourth variables Long Double, in this way we can store all types of values instead of a single type of value.
Declare Variables
long double a,b,c,sum=0;
Here we can see that three variable are declared simply declare but the fourth variable sum = 0 we define that is why if any garbage value hold sum then it changes into 0 so we can get our answer simple as we want.
Taking User Input
cin>>a>>b>>c;
storing the value in all three variables by the user, now the next step is Add all values held by all three variables after adding all values store the values in another variable Define Sum. At the end print the SUM (holding the sum of all three variables).
Adding and Printing
sum=a+b+c;
cout<<sum;
As we can see that our main function is an Integer type which means the main function should return some value but in this case, we are not returning any value so we are returning zero here
Returning to Main Function
return 0;
Now all steps of the problem are explained clearly. I hope you got it,
Before pasting the code into the editor make sure you have chosen C++ or if C++ not worked fine then change it into C++14 an editor in the top right drop-down option.
The Output of Input And Output Hackerrank Solution
Similar to Input And Output
- Solve Me First Hackerrank Solution in C & C++
- A Very Big Sum Hackerrank Solution C++
- Deque STL Hackerrank Solution in C++
- Mini Max Sum Hackerrank Solution in C++
- Staircase Hackerrank Solution in C
- Attribute Parser Hackerrank Solution in C++
- Inheritance Introduction Hackerrank Solution in C++
- StringStream Hackerrank Solution in C++
- Attribute Parser Hackerrank Solution in C++
- Basic Data Types HackerRank Solution in C++
- For Loop Hackerrank Solution in C++
- Functions in C++ Hackerrank Solution
- Pointer Hackerrank Solution in C++
- Arrays Introduction Hackerrank Solution in C++
- Strings Hackerrank Solution in C++
- Plus Minus Hackerrank Solution C++
- Hello World in C Hackerrank Solution
- Divisible Sum Pairs Hackerrank Solution in C++
- Apple And Orange Hackerrank Solution
- Simple Array Sum Hackerrank Solution C++
- Compare The Triplets Hackerrank Solution C++
0 Comments: