I need some help in that program to read 2 numbers and print sum of them
the program will be tested on one or more test cases. The first line of the input will be a single integer T, the number of test cases (1 ≤ T ≤ 100). Followed by the test cases, each test case is described in one line which contains 2 integers separated by a single space X Y (1 ≤ X,Y ≤ 100). X and Y are the 2 numbers which you should print the sum of them.
what's the suitable way to write this code as included

Dani AI

Generated

This thread describes the classic “A + B for T test cases” pattern. As asked, input comes from standard input; as suggested, iostream-based I/O is perfectly fine; and ’s pointers on basic C++ are relevant for beginners. The core idea is simple: read an integer T, then loop T times reading two integers X and Y and print their sum on its own line. A minimal, portable example is below.

#include <iostream>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    if (!(cin >> T)) return 0;
    for (int i = 0; i < T; ++i) {
        int X, Y;
        cin >> X >> Y;
        cout << (X + Y) << '\n';
    }
    return 0;
}

Notes and quick troubleshooting: prefer '\n' instead of endl to avoid unnecessary flushing; ios::sync_with_stdio(false) plus cin.tie(nullptr) speeds I/O for many test files. Given the stated bounds (X,Y <= 100) int is fine; switch to long long if inputs might be larger. If the judge does not supply T and instead gives pairs until EOF, use while (cin >> X >> Y) cout << X + Y << '\n';. Avoid non-portable headers like bits/stdc++.h if compiling outside GCC. Finally, the confusion about the phrase “test case” is common—’s later post confirms that once the loop over T is understood, the implementation is straightforward.

Recommended Answers

All 4 Replies

Where are you going to read the two numbers from ?

What programing language will you use ?

I guess you should read on cin and cout stuff. It is a basic input-output way of C++...

Hi ,

Do you know how to code an "Hello World" type program in C++ ?

If you do, then you can prompt for input.

Then the next thing ... is to code to take in some number ...
and then to take in numbers in a loop and to sum them up.

There are actually many ways to code for this...
but a simple way is demo'd at the following link:

Six Fast Steps to Programming in C++

But code your own version of a solution for your problem ...
and if you get stuck,
then you will need to show us the code you have tried so far.

Then ... we can see how to help you.

Oh , thanks all i got the answer by myself .. i just missed the mean of test case that i didnt understand the program so well .. it was so simple code
thanks again :))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.