Q1 If a five-digit number is input through the keyboard , write a c program to calculate the sum of its digits.

C program to calculate the sum of n digit number's digits

Program: 59

If a five-digit number is input through the keyboard , write a c program to calculate the sum of its digits. | Let us C Solution

#include<stdio.h>
int main()
{
    int number, sum=0, i, n, count=0, num;
    printf("Enter a Number: "); //we are taking n digits number as input
    scanf("%d", &number);
    num = number;
    while(num!=0)
    {
        num /= 10;
        count += 1;
    }
    for(i=0;i<count;i++)    //i=0 to i=count-1 (length(number))
    {
        n = number % 10;
        number = number / 10;
        sum = sum + n;
    }

    printf("Sum: %d", sum);
    return 0;

}

Output

 Enter Five Digit Number: 1111111111
 Sum: 10

Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.
Related Post
Leave a Comment

This website uses cookies.