×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

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 and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.