Monday 25 January 2016

Write a C program to find the sum of digits.

Post by On at 15:43:00
            Hi viewers , We are going to include a number of viewers required programs and information in our blog. kindly stay connected.

       Today , I am going to explain how to write a C program to find the sum of digits . What does the sum of digits mean ? If you consider a number say , 975 , the sum of its digits are - (9+7+5) = 21.
How to write a C program for this ? Let's see that now.

Note : In order to compile a C program , you need a C compiler. If you don't have kindly download it from here.




Source code:


#include<stdio.h>
int main()
{
int num,sum=0;
printf("Enter the number");                    // Accepting input from user scanf("%d",&num);
while(num>0)                                             //Loop to find the sum of digits {
sum=sum+(num%10);
num=num/10;
}
printf("Sum of digits is %d",sum);            //Print the result to the user return 0;
}


Output:


                     
Sum of digits C program

Check out other C programs:




We value your suggestions and feedback.  
Subscribe to our blog and we will send our posts to you by mail

Enter your email id in the subscription form on our page.

Share it with your friends in social media. 


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...