Advertisement
tepyotin2

Collecting Inputs

May 1st, 2025
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4.  
  5. int main() {
  6.     string line;
  7.     // Read input line by line
  8.     while (getline(cin, line)) {
  9.         istringstream iss(line);
  10.         int a, b;
  11.         if (iss >> a) {
  12.             if (iss >> b) {
  13.                 // Case: two numbers on the line
  14.                 cout << (a + b) << endl;
  15.             } else {
  16.                 // Case: only one number on the line, sum its digits
  17.                 int sum = 0;
  18.                 while (a != 0) {
  19.                     sum += a % 10;
  20.                     a /= 10;
  21.                 }
  22.                 cout << sum << endl;
  23.             }
  24.         }
  25.     }
  26.     return 0;
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement