0

Why isn't this code working?

If a four digit number is input through the keyboard, write a program to obtain the sum of the first and last digit of the number. my code... #include <stdio.h> int main() { //prompt the user to input the four digit number int theNum; printf("Enter the four-digit number: "); scanf("%d", &theNum); //compute the sum of the first and the last digits int sum = 0, i; for( i = 4; i > 0; i--) { if (i == 3 || i == 2) theNum = theNum / 10; continue; sum += (theNum % 10); theNum = theNum / 10; } printf("The sum of the entered digits is %d", sum); return 0; } but it's not giving the exact answer. Please can someone explain why it's not working and also the correct program for the question

1st Nov 2018, 1:36 AM
PrinceCEE
PrinceCEE - avatar
2 Answers
+ 8
If you don't use any curly braces { } after if statement, then only one line will be executed when the condition will be true. So the "continue" keyword is being executed in all cases. Using curly braces may solve this issue.
1st Nov 2018, 2:51 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 2
thanks, I noticed yesterday that omitting the curly braces may cause a program to malfunction.. thanks
2nd Nov 2018, 8:51 AM
PrinceCEE
PrinceCEE - avatar