C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C programming language

Print numbers from 1 to N using goto statement in C language

In this post, we will learn how we can print the numbers from 1 to N without using any looping statements like for, while, and do while? Here we will use goto statement?

Prerequisite: syntax and example of goto statement in C, goto statement in C

As we know, goto is used to jump the program's execution to a label (which can be defined anywhere in the scope).

To write this program, we can use goto statement within a condition and can jump program’s execution to the beginning (where number is printing).

Consider the program:

#include <stdio.h>

int main()
{
	int counter=1;
	int n;
	
	//enter the value of n (range)
	printf("Enter the value of n: ");
	scanf("%d",&n);
	
	//define label
	START:
	printf("%d ",counter);
	counter++; //increment counter
	//condition & goto statement
	if(counter<=n)
		goto START;
	
	return 0;
}

Output

Enter the value of n: 10
1 2 3 4 5 6 7 8 9 10 








COMMENTS