how to find sum of digits in C?

Answer Posted / ruchi

#include<stdio.h>
#include<conio.h>
int main()
{
int n,m,d,m1,s=0,s1;
printf("enter the number ");
scanf("%d",&n);
m=n%10;
d=n/10;
while(d>=10)
{
m1=d%10;
d=d/10;
s=s+m1;
}
s1=s+m+d;
printf("\nThe sum of digits is ");
printf("%d",s1);
getch();
}

Is This Answer Correct ?    7 Yes 11 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Write a program to display all the prime nos from 1 to 1000000, your code should not take time more than a minute to display all the nos.

1597


What are types of functions?

571


What is a loop?

558


If a variable is a pointer to a structure, then which operator is used to access data members of the structure through the pointer variable?

782


How to write c functions that modify head pointer of a linked list?

548






Why c is called free form language?

575


What is calloc()?

633


Why main is not a keyword in c?

653


What is variables in c?

611


a c code by using memory allocation for add ,multiply of sprase matrixes

2306


What is #include in c?

604


Explain what is #line used for?

610


Calculate the weighted average of a list of n numbers using the formula xavg = f1x1+f2x2+ ….+ fnxn where the f’s are fractional weighting factors, i.e., 0<=fi<1, and f1+f2+….+fn = 1

3663


You are to write your own versions of strcpy() and strlen (). Call them mystrcpy() and mystrlen(). Write them first as code within main(), not as functions, then, convert them to functions. You will pass two arrays to the function in the case of mystrcpy(), the source and target array.

1785


What are the different types of constants?

644