Input and Output in C++ - Hacker Rank Solution
What's up coders! Today we are going to learn about how to take input from the user and display the output to the user in C++. There is a myth that C++ is a very difficult to learn language, but trust me guys it is one of the easiest language to learn if you are interested in coding.
So, today we will be solving Input and Output in C++ which is a hackerrank beginners level problem. This Input and Output in C++ Hacker Rank Solution will guide you to cin and cout statements and you will get to know how to implement them in your code.
Contents
Objective
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 a 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 ( ), then the integer n. We end our line of output with a newline 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.
Constrains
- 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
Solution - Input and Output 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, d; cin >> a >> b >> c; d = a + b + c; cout << d; return 0; }
Input and Output in C++ - Hacker Rank Solution |
Input and Output in C++ - Result |
Explanation
The sum of three numbers is 1 + 2 + 7 = 10.
Cin object is used to take input from the user.
Cout object is used to display the output to standard output devices. Just like we use print statement in python, we use cout in C++.
Conclusion
So, today we have learned what is cin and cout statement and how we can use them to take input and display output. The above problem is generated By Hacker Rank but the Solution is provided by Sloth Coders.
If you have any doubt regarding the problem (Input and Output in C++), feel free to contact in the Comment Section.
Share it with your friends and relatives and Don't forget to Subscribe our blog to receive daily solutions and updates related to coding.
Happy Coding!!
A Sloth Who loves to Code.