#include<stdio.h>
int main()
{
int n, i=1;
printf("Enter a number: ");
scanf("%d",&n);
start:
printf("%d\t",i);
i++;
if(i<n) goto start;
return 0;
}
Output:-
Enter a number: 10
1 2 3 4 5 6 7 8 9
In this program, the goto works similarly to do-while loop. Assume the label start
as do
, and goto start
as while
, and if(i<n)
as the condition
of the do-while loop. First, the statements are executed and then the condition is checked.
Program2:- Write a program to find the sum of positive numbers in C programming using goto statements. The program should take input repeatedly until the user enters a negative number.