Write a program to find out sum of digits of given number


2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Write a program to find out sum of digit of given number
// Eg: 123 -> 6, 35323 -> 16
#include
int main(){
    int n;
    printf("Enter a positive integer:");
    scanf("%d",&n); // input from user
    while(n<0){ printf("Please enter positive integer only:"); scanf("%d",&n); } int sum=0; while(n>0){
         sum = sum+n%10; // calculating sum of digits
         n = n/10;
    }
    printf("Sum of digits of given number: %d",sum);
    getch();
}

No comments:

Post a Comment