w3resource

C Exercises: Count the number of digits of the sum value of two integer values

C Basic Declarations and Expressions: Exercise-133 with Solution

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

Sample Solution:

C Code:

#include <stdio.h>
int main()
{
    int x, y, sum_val, ctr;
    printf("Input two integer values:\n");
    scanf("%d %d", &x, &y);

    ctr = 0; // Initialize a counter to keep track of digits
    sum_val = x + y; // Calculate the sum of x and y

    // Loop to count the number of digits in the sum value
    while (sum_val != 0)
    {
        if (sum_val > 0)
        {
            sum_val = sum_val / 10; // Divide by 10 to remove the rightmost digit
        }
        ctr++; // Increment the counter
    }

    // Print the number of digits in the sum value
    printf("\nNumber of digits of the sum value of the said numbers:\n");
    printf("%d\n", ctr);

    return 0; // End of program
}

Sample Output:

Input two integer values:
68
75

Number of digits of the sum value of the said numbers:
3

Flowchart:

C Programming Flowchart: Count the number of digits of the sum value of two integer values.

C programming Code Editor:

Previous: Write a Java program to find heights of the top three building in descending order from eight given buildings.
Next: Write a C program to check whether three given lengths (integers) of three sides of a triangle form a right triangle or not. Print "Yes" if the given sides form a right triangle otherwise print "No".

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming-exercises/basic-declarations-and-expressions/c-programming-basic-exercises-133.php