w3resource

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


Sum two integers and count digits in the result

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.


For more Practice: Solve these Related Problems:

  • Write a C program to sum two integers and then determine the number of digits in the result using a loop.
  • Write a C program to calculate the sum of two numbers and count digits by converting the result to a string.
  • Write a C program to compute the sum and use a recursive function to count the digits of the resulting number.
  • Write a C program to add two numbers and iteratively divide the sum by 10 to count its digits.

Go to:


PREV : Find heights of top three tallest buildings.
NEXT : Check if three sides form a right triangle.

C programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.