First of all we all should know what is sum of digits of a number?
Sum of digits of a number is defined as the sum of all the digits present in that number.
Example: 145, 1+4+5=10
PROGRAM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
int main()
{
int num,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(temp!=0)
{
sum=sum+temp%10;
temp=temp/10;
}
printf("Sum of digits of %d is: %d",num,sum);
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<stdio.h> int main() { int num,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); temp=num; while(temp!=0) { sum=sum+temp%10; temp=temp/10; } printf("Sum of digits of %d is: %d",num,sum); return 0; } |
0 comments:
Post a Comment