Q:

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

0

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

Given the value of N and we have to print all number from N to 1 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 N to 1, N is the limit of the series which will be given by the user.

Example:

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

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to print numbers from N to 1 using goto

#include <stdio.h>

int main()
{

	int count,n;

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

	//initialize count with N
	count =n;

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

	if(count>=1)
		goto start;

	return 0;
}

Output

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

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

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now