Consecutive Numbers Using Goto Statememt


This is a C program that prompts the user to enter two integers, 'i' and 'n' , and then uses the goto statement to print the numbers from 'i' to 'n' .

  • The #include<stdio.h> is a preprocessor directive that includes the contents of the standard input-output library in the program. The int main() function is the starting point of the program execution.
  • Inside the main function, two integers i and n are declared. printf() is used to display the prompt for the user to enter the start and end numbers, and scanf() is used to get the input and store it in the variables 'i' and 'n' respectively.
  • A label start is defined and a goto statement is used to jump to this label. The code between the label and the goto statement will execute repeatedly until the condition i<=n is false.
  • The first time the code is executed, the value of i is printed and the value of i is incremented by 1. Then the condition i<=n is checked, if it's true the control jumps back to the label 'start' and the same process is repeated until the condition is false.
  • It is worth noting that the use of the goto statement is generally discouraged in modern programming because it can lead to unstructured and hard-to-maintain code.
  • Alternative control structures such as for loops, while loops or do-while loops can be used for the same purpose and are considered more readable, maintainable and less prone to errors.

Finally, the return 0 statement is used to indicate the successful execution of the program. The return value of 0 is a convention used to indicate that the program has executed correctly

Source Code

#include <stdio.h>
int main()
{
  int i,n;
  printf("Enter the number start and end:");
  scanf("%d%d",&i,&n);
  start:
  printf("\n%d",i);
  i++;
  if(i<=n)
  {
    goto start;
  }
  return 0;
}
To download raw file Click Here

Output

Enter the number start and end : 5
10
5
6
7
8
9
10

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C