Home » NO IDEA » Sum of the digits of a number

Sum of the digits of a number

Blog Stats

  • 13,912 hits
January 2013
M T W T F S S
« Dec   Mar »
 123456
78910111213
14151617181920
21222324252627
28293031  
//sum of the digits
#include<stdio.h>
int main()
{
 int num,sum,r;
 sum=0;
 printf("\n Enter the number ");
 scanf("%d",&num);
 while(num)
 {
 r=num%10;
 sum=sum+r;
 num=num/10;
 }
 printf("\n The sum of the digits %d",sum);
 return 0;
}
Sample output:
Enter a number: 45
Sum of digits of number:  9
 
Advertisements

1 Comment

  1. […] Sum of the digits of a number […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: