w3resource

C++ Exercises: Compute the sum of the two given integers and count the number of digits of the sum value

C++ Basic: Exercise-64 with Solution

Write a C++ program to compute the sum of the two given integers and count the number of digits in the sum value.

Visual Presentation:

C++ Exercises: Compute the sum of the  two given integers and  count the number of digits of the sum value

Sample Solution:

C++ Code :

#include <iostream> // Including input-output stream header file
#include <sstream>  // Including stringstream header file for string manipulation
using namespace std; // Using the standard namespace

int main() { // Start of the main function
    int x, y; // Declaring variables x and y to store integer inputs

    while (cin >> x >> y) { // Loop to read integer inputs until the end of input (Ctrl+D in terminal)
        stringstream str1; // Declaring a stringstream object 'str1' for string manipulation

        str1 << x + y; // Adding integers x and y and storing the result as a string in stringstream 'str1'

        cout << str1.str().size() << endl; // Printing the size (number of characters) of the resulting string
    }

    return 0; // Indicating successful completion of the program
}

Sample Output:

Sample input : 15 20
Sample Output:
2

Flowchart:

Flowchart: Compute the sum of the  two given integers and count the number of digits of the sum value

C++ Code Editor:

Previous: Write a C++ program which prints three highest numbers from a list of numbers in descending order.
Next: Write a C++ program to check whether given length of three side form a right triangle.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.