+ 2

How we can print sum of digits of number ?

Sum of digits of number https://code.sololearn.com/cJWUfsjDBC6J/?ref=app

6th Jun 2018, 8:06 AM
ABHISHEK Kumar
ABHISHEK Kumar - avatar
5 Respostas
+ 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
6th Jun 2018, 8:11 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 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; }
6th Jun 2018, 8:36 AM
Ipang
+ 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
6th Jun 2018, 9:50 AM
Raj Chhatrala
Raj Chhatrala - avatar
0
what ? say
6th Jun 2018, 8:15 AM
ABHISHEK Kumar
ABHISHEK Kumar - avatar