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.
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;
}
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:
Check out other C programs:
- To check if the given number is an Armstrong number or not?
- To check if the given number is a prime number or not?
- To find the sum of square series.
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