Saturday, November 7, 2009

Add Digits Of Any Fugure Number | C Programming

//wap to add digits of any figure no
#include
#include
void main()
{
int i,n,r,sum=0,d=1; //students are advised to use
clrscr(); // “long” instead of “int” so as to use high figure no
printf(“\n\n enter a number: “);
scanf(“%d”,&n);
for(i=1;i<=d;i++)
{
r=n%10;
sum=sum+r;
n=n%10;
if(n!=0)
{d++;}
}
printf(“\n\n\a\tthe sum is :%d”,sum);
getch();
}

or

#include
#include
void main()
{int n,sum=0,a,b,s;
clrscr();
printf(“\n\n enter a number: “);
scanf(“%d”,&n);
while(n!=0)
{
s=n%10;
n=n/10;
sum=sum+s;
}
printf(“\n\n\a\tthe sum is :%d”,sum);
getch();
}

o/p→
enter a number : 12345
the sum is : 15




No comments:

Post a Comment