Program to enter a three digit number and find sum of its digit

Write a program to enter a three-digit number and find sum and print.

C Program

#include <stdio.h>
int main()
{
    int num;
    printf("Enter a three digit number : ");
    scanf("%d", &num);
    int sum = 0;
    int n = num;
    while (n > 0)
    {
        int temp = n % 10;
        sum = sum + temp;
        n = n / 10;
    }
    printf("Sum of digits of %d is %d "numsum);
    return 0;
}

Input:

Enter a three digit number : 342

Output:

Sum of digits of 342 is 9 


No comments:

Post a Comment