-
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?
-
Α bracket '}' is missing at line after printf("\nThat's interesting isn't it?\n");
That is what you want? :)
-
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?
-
yes, thats good sloves my problem. Thanks!