C program for sum of digits of a given number. It is a simple source code that provides the output of an actual number given by the user. It will display the digits count of an input value. For this C program, questions may asked like “write a c program to find the sum of the digits of a given number & sum of few digit number in c”. Take care of a questions for your interviews or students exam. This is a totally free of cost code.
SUM OF DIGITS OF A GIVEN NUMBER IN C
Calculating the sum of digits of a given number program in C is used to find the sum value of an entering sequences digit value by unconditional statement (while statement). Basic operation is (sum+=number%10).
C CODE:
/* Title: Sum of digits of given number
Author: Vinoth Selvaraj
© http://students3k.com
*/
//Header files
#include <stdio.h>
#include <conio.h>
void main()
{
//Program variables
int num,s=0;
clrscr(); //Function to clear previous output
printf("Enter the digits : "); //Display function
scanf("%d",&num); //Getting input function
//sum the enter digits
while(num>0) //<strong>Unconditional statement</strong>
{
s+=num%10;
num/=10;
}
printf("Sum of digits value is = %d ",s);
getch();
}