programming-examples/c/Control_Statements/C Program to find sum of digits of a given number..c

29 lines
557 B
C
Raw Normal View History

2019-11-15 12:59:38 +01:00
/* Sum of digits - Program to find sum of digits of a given number */
#include <stdio.h>
#include <conio.h>
void main()
{
int n, sum=0, r, temp ;
clrscr() ;
printf("Enter a number: ") ;
scanf("%d", &n) ;
temp=n ;
while(n!=0)
{
r=n%10 ; /* Extract the last digit */
sum=sum+r ; /* Finding sum */
n=n/10 ; /* Reduce number by 1 digit */
}
printf("Sum of digits of %d is %d", temp, sum) ;
getch() ;
}
/*
Output
Enter a number: 246
Sum of digits of 246 is 12
*/