My Tech notes: how to find sum of digits in C?
Subscribe

Unix Documentation

Free Online Unix Training Materials

Lists many links to free Unix training materials.

Pointers and Arrays Materials

Pointers and Arrays materials Explained for C beginners

C FAQ and General Questions C Interview Questions

Powered By

Free XML Skins for Blogger

Powered by Blogger

Sunday, June 1, 2008

how to find sum of digits in C?

main()
{
int n,i,sum=0,r;
printf("enter the value");
scanf("%d",&n);
for(i=0;i<=5;i++)
{
r=n%10;
n=n/10;
sum=sum+r;
}

2 comments:

Bhagwat said...

while(n!=0) is prefect instead of for loop. :-)

Chinna said...

swap two nos in one step.....
a = a+b - (a=b);

here b assigning value to a. then a and b value will be the same. in this step it wont swap...if how? let me know

Post a Comment