Sum of digits in a number
The below program takes a number as input produces the sum of digits in the input number. The below program is implemented using c.
Ex:
Let 856 be number.
So the sum of digits of 856 will be 8+5+6=19
Ex:
Let 856 be number.
So the sum of digits of 856 will be 8+5+6=19
#include<stdio.h> int main() { int x,k,sum=0,c; printf("\nEnter a number : "); scanf("%d",&x); while(x>0) { c=x%10; x=x/10; sum=sum+c; } printf("Sum of digits=%d",sum); return 0; }
Description:
The above program computes the sum of individual digits of a number and prints it.
Output:
Case 1:
Enter a number:647
Sum of digits:17
Case 2:
Enter a number:78
Sum of digits:15
Comments
Post a Comment