Write a C Program to find the Sum of the Digits of a Number
Program:
#include<stdio.h>
int main()
{
int n, remainder, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n % 10;
sum += remainder;
n = n / 10;
}
printf("sum = %d", sum);
return 0;
}
------------------------------------------------------------------------------------
Output:-
Enter a number: 12345
sum = 15
--------------------------------
Process exited after 8.671 seconds with return value 0
Press any key to continue . . .
Program:
#include<stdio.h>
int main()
{
int n, remainder, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n % 10;
sum += remainder;
n = n / 10;
}
printf("sum = %d", sum);
return 0;
}
------------------------------------------------------------------------------------
Output:-
Enter a number: 12345
sum = 15
--------------------------------
Process exited after 8.671 seconds with return value 0
Press any key to continue . . .
0 Comments:
Post a Comment