Tuesday, February 10, 2015

C Program to find sum of digits of a number


For example: Number is 4511 than sum is 11
====================================
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
unsigned long i,p,n,sum=0;
cout<<"Enter any number:";
cin>>n;
while(n!=0)
{
p=n%10;
sum+=p;
n=n/10;
}
cout<<endl<<"Sum of its digits is:"<<sum;
getch();
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.