Thread: Nested if question.

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    18

    Question Nested if question.

    Code:
    /*program 3.3 using nested ifs to analyze numbers*/
    #include <stdio.h>
    #include <limits.h>/*for LONG_MAX*/
    
    int main()
    {
        
    long test = 0L;         /*stores the integer to be checked*/
        printf("Enter an integer less than %ld:", LONG_MAX);
        scanf("%ld", &test);
        
    /*test for odd or even by checking the remainder after dividing by 2*/
        
    if(test % 2L == 0l)
        {
            printf("The number %ld is even", test);
            
    /*now check whether half the number is also even*/
            
    if( (test/2L) % 2L == 0L)
            {
                printf("\nHalf of %ld is even", test);
                printf("\nThat's interesting isn't it?\n");
            
    else
                printf("\n Half of %ld is odd\n", test);
            }
        }
        
    else
            printf("The number %ld is odd\n", test);
    }
    
    Im getting and error because it says that the else statement: printf( "\n Half of %ld is odd\n", test); has no matching if. Is there a way I can still keep that else branch nested within the bigger if-else?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Α bracket '}' is missing at line after printf("\nThat's interesting isn't it?\n");
    That is what you want?
    Last edited by std10093; 07-24-2012 at 04:23 PM.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Actually with a second look i suggest this

    Code:
    int main(void)
    {
        
    long test = 0L;         /*stores the integer to be checked*/
        printf("Enter an integer less than %ld:", LONG_MAX);
        scanf("%ld", &test);
        
    /*test for odd or even by checking the remainder after dividing by 2*/
        
    if(test % 2L == 0l)
        {
            printf("The number %ld is even", test);
            
    /*now check whether half the number is also even*/
            
    if( (test/2L) % 2L == 0L)
            {
                printf("\nHalf of %ld is even", test);
                printf("\nThat's interesting isn't it?\n");
    }
            
    else
                printf("\n Half of %ld is odd\n", test);
            
        }
        
    else
            printf("The number %ld is odd\n", test);
    return 0;
    }
    Is it ok?

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    18
    yes, thats good sloves my problem. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question from a newbie regarding nested function
    By Sharifhs in forum C Programming
    Replies: 19
    Last Post: 08-10-2010, 06:44 AM
  2. Quick question on nested for loops
    By GCNDoug in forum C++ Programming
    Replies: 12
    Last Post: 10-17-2007, 08:54 PM
  3. nested class question
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2007, 03:29 PM
  4. Question With Nested If's
    By jarvis in forum C Programming
    Replies: 7
    Last Post: 07-26-2006, 08:06 AM
  5. Nested classes question...
    By alvifarooq in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2004, 07:39 AM