Saturday 14 March 2015

Program to find the sum of digits of an integer Using C

# include <stdio.h>
# include <conio.h>
void main()
{
int n, r, s = 0 ;
clrscr() ;
printf("Enter a number : ") ;
scanf("%d", &n) ;
while(n > 0)
{
r = n % 10 ;
s = s + r ;
n = n / 10 ;
}
printf("\nThe sum of the digits is : %d", s) ;
getch() ;
}


RUN 1 :
~~~~~~~
Enter a number : 12345
The sum of the digits is : 15

No comments:

Post a Comment