Determine Positive or Negative Number using Goto statement

Q. WAP to ask two numbers. Diplay message "Either number is negative" if either number is negative; otherwise display message "Both numbers are positive.
 ==> Used to alter normal flow of program execution by unconditionally transferring control to some other part of program usually labeled statement somewhere.
 ==> Code goes like this : 

#include<stdio.h>
int main()
{
    int i,num1,num2;
    printf("\n Enter first number:");
    scanf("%d",&num1);
    if(num1<0)
    goto negative;
    printf("\n Enter second number:");
    scanf("%d",&num2);
    if(num2<0)
    goto negative;
    printf("\n both numbers are positive");
    return;
    negative:    //label for goto statement
    printf("Either number is negative");   
    return 0;   
}

Output: 











Output1:












I have used Dev CPP to compile this program.
If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you 

Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment