how to find sum of digits in C?

Answer Posted / pooja

main()
{
int n,i,sum=0,r;
printf("enter the value");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
n=n/10;
sum=sum+r;
}
printf("\nsum of digits is %d",sum);
}

Is This Answer Correct ?    57 Yes 11 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Where are some collections of useful code fragments and examples?

716


What is || operator and how does it function in a program?

630


in any language the sound structure of that language depends on its a) character set, input/output function, its control structures b) character set, library functions, input/output functions its control structures c) character set, library functions, control sturctures d) character set, operators, its control structures

678


#define PRINT(int) printf("int = %d ",int) main() {< BR> intx,y,z; x=03;y=02;z=01; PRINT(x^x); z<<=3;PRINT(x); y>>=3;PRINT(y); }

720


Explain how do you determine the length of a string value that was stored in a variable?

670






Can a pointer be null?

565


Explain how do I determine whether a character is numeric, alphabetic, and so on?

654


any "C" function by default returns an a) int value b) float value c) char value d) a & b

667


What are different storage class specifiers in c?

618


find the value of y y = 1.5x+3 for x<=2 y = 2x+5 for x>2

1529


Explain why C language is procedural?

771


how can I convert a string to a number?

597


How many levels of indirection in pointers can you have in a single declaration?

595


Explain pointer. What are function pointers in C?

628


What is a string?

666