
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).
//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();
}
#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();
}
No comments:
Post a comment
Note: only a member of this blog may post a comment.