+ 2
How we can print sum of digits of number ?
Sum of digits of number https://code.sololearn.com/cJWUfsjDBC6J/?ref=app
4 Answers
+ 6
#include <stdio.h>
#include<conio.h>
void main()
{
int num,sum,r;
printf ("Enter Number to get sum of their digits:");
scanf("%d",&num);
for(sum=0;num!=0;num=num/10)
{
r=num%10;
sum=sum+r;
num=num/10;
}
printf("\nSum of digits = %d",sum);
}
your code it right, just add one line
+ 6
#include <stdio.h>
int main()
{
int num, sum;
printf ("Enter Number to get sum of their digits:");
scanf("%d",&num);
for(sum = 0; num; num /= 10)
sum += num % 10;
printf("\nSum of digits = %d",sum);
return 0;
}
+ 4
#include <stdio.h>
#include<conio.h>
void main()
{
int num,sum,r;
printf ("Enter Number to get sum of their digits:");
scanf("%d",&num);
while(num != 0)
{
r=num%10;
sum=sum+r;
num=num/10;
}
printf("\nSum of digits = %d",sum);
}
now this will okay, I haven't read whole code, just seen the loop.
Immortal
0
what ? say