Pages

Friday, December 16, 2011

Program to calculate sum of digits of a number

This program calculates sum of any digit number(within integer range).since integer range is-32768 to 32767,it gives error wen number exceeds 5 digit....

#include<stdio.h>
void main()
{
int num,r;
int sum=0;
clrscr();

printf("Enter number : ");
scanf("%d",&num);

while(num!=0)
{
r=num%10;
num=num/10;
sum=sum+r;
}
printf("%d",sum);

getch();
}

No comments: