C Program to Demonstrate Backward Branching Using goto Statement

The goto statement is used to transfer control from one part of a program to another specified label. When control moves to a previous label, it is called backward branching.

C में goto statement का उपयोग program के control को किसी specified label पर transfer करने के लिए किया जाता है। जब control किसी पहले वाले label पर जाता है, तो इसे Backward Branching कहते हैं।

Syntax

Statement(s);
Label:
Statement(s);
goto Label;

Example: C Program to Calculate Square Root of a Positive Number

C Program

#include <stdio.h>
#include <math.h>
int main()
{
int n;
float s;
XYZ:
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n <= 0)
goto XYZ;
s = sqrt(n);
printf("Square root of %d = %f\n", n, s);
return 0;
}

Output

Enter a positive integer: -5
Enter a positive integer: 0
Enter a positive integer: 25
Square root of 25 = 5.000000

Explanation

The program asks the user to enter a positive integer. If the entered number is zero or negative, the goto XYZ; statement transfers control back to the XYZ label, prompting the user to enter the number again.

When a positive integer is entered, the program calculates its square root using the sqrt() function and displays the result.

यह program user से positive integer input करता है। यदि number zero या negative होता है, तो goto XYZ; statement control को वापस XYZ label पर भेज देता है और user से दोबारा number enter करने के लिए कहता है।

जब positive integer enter किया जाता है, तो sqrt() function की सहायता से उसका square root calculate करके display किया जाता है।

Backward branching using goto allows a program to return to a previous statement or label. In this example, it is used to repeatedly request valid positive input.

goto द्वारा Backward Branching program को किसी previous statement या label पर वापस भेजती है। इस example में इसका उपयोग valid positive input दोबारा प्राप्त करने के लिए किया गया है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025