C++ Exercises: Compute the sum of the two given integers and count the number of digits of the sum value
Sum of Two Integers and Digit Count
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:
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:
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?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics