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

Home » C programs » C 'goto' programs

C program to print all numbers from 1 to N using goto statement

In this C program, we are going to read limit of the series (N) and print all numbers from 1 to N (limit) using goto statement.
Submitted by Manju Tomar, on November 07, 2017

Given the value of N and we have to print all number from 1 to N using C program.

goto is a jumping statement, which transfers the program’s control to specified label, in this program we will learn how to print all numbers from 1 to N, N is the limit of the series which will be given by the user.

Example:

Input:
Enter limit (value of N): 10
Output:
1 2 3 4 5 6 7 8 9 10

Program to print numbers from 1 to N using goto in C

#include <stdio.h>

int main()
{

	int count,n;

	//read value of N
	printf("Enter value of n: ");
	scanf("%d",&n);

	//initialize count with 1
	count =1;

	start: //label
	printf("%d ",count);
	count++;

	if(count<=n)
		goto start;

	return 0;
}

Output

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

Second run:
Enter value of n: 100 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 
89 90 91 92 93 94 95 96 97 98 99 100 

This is how we can print numbers from 1 to given range using goto statement? If you liked or have any issue with the program, please share your comment.





Sponsored Links




COMMENTS