Template by:
Free Blog Templates

Showing posts with label Add digits. Show all posts
Showing posts with label Add digits. Show all posts

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




Add digits of 3 digit no C programming

//wap to add digits of any 3 digit no
#include
#include
void main()
{int i,n,r,sum=0;
clrscr();
printf(“\n\n enter a number: “);
scanf(“%d”,&n);
for(i=1;i<=3;i++)
{
r=n%10;
sum=sum+r;
n=n%10;
}
printf(“\n\n\a\tThe sum is :%d”,sum);
getch();
}

o/p→
enter a number : 123
The sum is : 6