Hello! I'm having a rather difficult time because the concepts weren't very much explained in class for complex situations. I'm a bit entangled with the few if-else conditions I have... :( I've been trying hard to crack the case, but I think somebody really needs to see my work.

Write a menu driven program for the following options using ladderized if-else
statement:
a. To find whether the number is odd or even.
b. To display sum of input five numbers.
c. Exit.

#include <stdio.h>
#include <stdlib.h>

int main()

{
    int menu, n1, n2, n3, n4, n5, sum;
    printf("Press 'A' to determine whether a number is odd or even.\n");
    printf("Press 'B' to find for the sum of five numbers.\n");
    printf("Press 'C' to exit.\n\n");

    scanf("%c", &menu);

    if((menu=='A')||(menu=='a'))
    printf("\nEnter a number: ");
    scanf("%d", &n1);
    if(n1%2==0)
    printf("\nEven.\n");
    else
    printf("\nOdd.\n");

    if((menu=='B')||(menu=='b'))
    printf("Enter five numbers.");
    scanf("%f" "%f" "%f" "%f" "%f", &n1, &n2, &n3, &n4, &n5);
    sum=n1+n2+n3+n4+n5;
    printf("Sum is: %.2f", sum);

    if((menu=='C')||(menu=='c'))
    printf("\nAre you sure you want to exit?");  

system("PAUSE");
return 0;
}

Can we see the results of your tryings? Don't be afraid if they contain mistakes. We are here to fix them if we can.

Hello Sir! :D I've been wondering how to have an if-else condition, with the 'else' leading to another if-else provision... :)

The one above is my best try so far :) Thank you very much!! :)

You need to add curly braces { } whenever you have an expression with multiple lines of sub expressions, or it won't work as you want it to.

if(some test is true {
   first code expression;
   second code expression;
}

Without the braces being in the above, ONLY the first code expression would be conditional to the if statement being true. The second code expression, if the braces were NOT there, would ALWAYS be executed by the program.

Note that the indentation does NOT change the syntax required. It's just to help us humans, not the compiler. In Python, the indentation affects how it's compiled, but not in C.

Still, good indentation is VERY helpful - you should work on yours. Dependent expressions are indented 2 to 4 spaces to the right.

The indentation makes it so much easier to find our errors and fix them fast.

This article has been dead for over six months. Start a new discussion instead.