21/03/2023

Input And Output Hackerrank Solution in C++ | Algorithms

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:

Input And Output Hackerrank Solution in C++

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.

The Output of Input And Output Hackerrank Solution


The Output of Input And Output Hackerrank Solution

Similar to Input And Output


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: