r/code • u/Winter_Ad2149 • Mar 29 '24
Help Please Two compilers showing different outputs!
This is a program to calculate sum of individual digits of a given positive number. The compiler available on onlinegdb.com provides correct output for the input "12345" but the compiler in VS code showing 17 as output for the same input.
#include <stdio.h>
#include <math.h>
int main() {
int num, temp, size=0, digit=0, digitSum=0;
do {
printf("Enter a positive number: ");
scanf("%d", &num);
}
while(num<0);
temp=num;
while(temp!=0) {
size++;
temp=temp/10;
}
for(int i=1; i<=size; i++) {
digit=(num%(int)pow(10,i))/(int)pow(10,i-1);
digitSum+=digit;
}
printf("The sum of digits of %d is %d.", num, digitSum);
return 0;
}
View all comments
Show parent comment
2
u/Winter_Ad2149 Mar 29 '24
Thanks a lot for your help. I'm finally in peaceemote:free_emotes_pack:smile