Pages

Thursday, December 22, 2011

Program to find sum of all digits of a number

This program calculates the sum of all digits of a number into a single digit.for instance if user has input 2345..the end result will be 2345=14=5


If a number of any digit is iput through keyboard,write a program to obtain the sum of all digits of the number into a single digit


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

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

// it will give sum of all digits
while(num!=0)
{
r=num%10;
num=num/10;
total=total+r;

}

// This will further sum up the total of digits
while(total!=0)
{
temp=total%10;
total=total/10;
sum=sum+temp;
}

printf("Total=%ld ",sum);
getch();

}

No comments: