Sunday, 5 May 2013

SUM OF DIGITS OF A NUMBER


Here is a very simple C program that prompts the user to input a number, and then it calculates the sum of all the digits in that number.
For example, if the user inputs 152, then the sum of digits will be 1+5+2=8.


Source code :-
/*****************************************************************************/
//PROGRAM TO FIND THE SUM OF DIGITS OF NUMBER ENTERED BY USER

#include<stdio.h>
#include<conio.h>

int main()
{
long num;
int sum=0;
clrscr();
printf("ENTER THE NUMBER : ");
scanf("%ld",&num);
while(num)
{
sum+=(num%10);
num/=10;
}
printf("THE SUM OF DIGITS IS : %d",sum);
getch();
return 1;
}
/******************************************************************************/





No comments:

Post a Comment