In this post, I am writing a straightforward C program to find whether the entered number is Even or Odd as well as Positive or Negative.
Here is the entire program, copy-paste the code and run it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <stdio.h> int main() { int n; //get the value from user printf("Enter a number:"); scanf("%d", &n); // using goto statements if (n % 2 == 0 && n > 0) goto a; else if (n % 2 != 0 && n < 0) goto b; else if (n % 2 == 0 && n < 0) goto c; else if (n % 2 != 0 && n > 0) goto d; else goto e; a: printf("Number is Even and Positive"); goto stop; b: printf("Number is Odd and Negative"); goto stop; c: printf("Number is Even and Negative"); goto stop; d: printf("Number is Odd and Positive"); goto stop; e: printf("It's a zero"); goto stop; stop: return 0; } |
Sample Outputs:
1 2 3 4 5 |
Enter a number:50 Number is Even and Positive -------------------------------- Process exited after 2.284 seconds with return value 0 Press any key to continue . . . |
1 2 3 4 5 |
Enter a number:-2 Number is Even and Negative -------------------------------- Process exited after 3.433 seconds with return value 0 Press any key to continue . . . |
I believe this post would have helped you to understand how to use the goto statement.