write a program to input an integer and find the sum of its digits
Answers
Answered by
1
//write a program to input an integer and find the sum of its digits
//works only for numbers between 10 to 99
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, sum=0, rem, quo;
printf("Enter an integer :- ");
scanf("%d",&n);
quo = n/10;
rem = n%10;
//printf("Quotient = %d\nRemainder = %d\n",quo,rem);
sum = quo + rem;
printf("Sum of digits = %d\n\n",sum);
return 0;
}
//works for all integers
#include <stdio.h>
int main()
{
int n, t, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
printf("Sum of digits of %d = %d\n", n, sum);
return 0;
}
Similar questions