0

i tried compiling my source code in visual studio 2017 , it compiles without errors , but doesn't work

it is a program to print armstrong numbers below a certain limit , ie addition of cube of each digit in number must be equal to the number ie , 153 where 1*1*1 + 5*5*5+3*3*3 = 153 here is my source code i tried compiling #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #define _CRT_SECURE_NO_WARNINGS int i, a, b, c; char d[12]; char e[12]; int main(int argc, char const *argv[]) { for (i = 0; i < 1000; i++) { if (i<9) { printf("\n"); } if (i > 10 && i >= 99) { _itoa_s(i, d, 12); // sprintf (i,"%d",d); b = d[0] * d[0] * d[0] + d[1] * d[1] * d[1]; if (b == i) { printf("%d", i); } } if (i>99 && i<999) { _itoa_s(i, e, 12); //sprintf (i,"%d",d); b = d[0] * d[0] * d[0] + d[1] * d[1] * d[1] + d[2] * d[2] * d[2]; if (b == i) { printf("%d", i); } } if (i <= 999) { break; } } return 0; }

10th Sep 2017, 9:58 AM
Hridyansh Thakur
Hridyansh Thakur - avatar
2 Answers
+ 3
If there's no syntax errors then there may be logical errors. Example (Errors (Javascript)): //Syntax error var num # 5; alert(num); -the syntax error is that # should be = //Logical error var num = 5; /*alert(num);*/ -this is a logical error because it will compile with no syntax errors but will produce nothing because the alert function is commented out
10th Sep 2017, 10:11 AM
Ghauth Christians
Ghauth Christians - avatar
+ 3
You're trying to calculate the ASCII of d, instead of the actual number. You need to convert each digit of d. For example you would do: int digit1 = d[0] - '0'; int digit2 = d[1] - '0'; then calculate b = digit1*digit1*digit1 + digit2*digit2*digit2;
10th Sep 2017, 10:43 AM
Karl T.
Karl T. - avatar