Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Input and Output solution in c++

YASH PAL, 31 July 20241 September 2024

In this HackerRank Input and Output problem solution in c++ programming language, 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 (” “), 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.

HackerRank Input and Output solution in c++ programming

HackerRank Input and Output problem solution in c++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {

    int sum = 0;
    int x, y, z;

    cin >> x >> y >> z;
    sum = x + y + z;
    cout << sum << endl;

    return 0;
}

Second solution

#include <iostream>
using namespace std;

int main() {
    int a;
    int b;
    int c;
    cin >> a >> b >> c;
    
    cout << a + b + c << endl;
    
    return 0;
}

Third solution

#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 i = 0;
    int sum = 0;
    
    while (cin >> i) {
        sum += i;
    }
    
    cout << sum << endl; 
    return 0;
}
coding problems cpp hackerrank solutions cPrograms

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes