how to find sum of digits in C?

Answer Posted / rakesh ranjan

#include<conio.h>
#include<stdio.h>
main()
{
int x=0,n,i;
printf("entre the number");
scanf("%d",&n);
for(;n>0;n/=10)
x=x+n%10;
printf("sum = %d",x);
getch();
}

Is This Answer Correct ?    0 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal. [ I gave the obvious solution of taking % 10 and / 10, which gives us the decimal value in reverse order. This requires an array since we need to print it out in the correct order. The interviewer wasn't too pleased and asked me to give a solution which didn't need the array ].

648


How to draw the flowchart for structure programs?

8764


What are c header files?

585


What does sizeof return c?

610


What are local static variables?

623






What are the benefits of c language?

651


disply the following menu 1.Disply 2.Copy 3.Append; as per the menu do the file operations 4.Exit

1633


main() { struct s1 { char *str; struct s1 *ptr; }; static struct s1 arr[] = { {"Hyderabad",arr+1}, {"Bangalore",arr+2}, {"Delhi",arr} }; struct s1 *p[3]; int i; < BR> for(i=0;i<=2;i++) p[i] = arr[i].ptr; printf("%s ",(*p)->str); printf("%s ",(++*p)->str); printf("%s ",((*p)++)->str); }

925


What is bss in c?

609


how many types of operators are include in c language a) 4 b) 6 c) 8 d) 12

658


What is static memory allocation? Explain

634


Explain how can you restore a redirected standard stream?

594


What does 3 periods mean in texting?

604


What is a char in c?

559


Why pointers are used in c?

596