Finding the sum of digits of a given number

/* This program finds the sum of the digits of the given number*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int m=0,n=0,r=0,sum=0;
    clrscr();
    printf("Enter a no.:");
    scanf("%d",&m);
    n=m;
    while(n>0)
    {
        r=n%10;
        sum+=r;
        n=n/10;
    }
    printf("Sum of digits:%d",sum);
    getch();
}

Output:

Enter a no.:456
Sum of digits:15
About these ads

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: