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 





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.