Priniting Even Numbers using Goto Statement in C++


This program prints all even numbers between a given starting and ending value using a goto statement. Here's how the program works:

  • It prompts the user to enter a starting value.
  • It reads the starting value into the variable i.
  • It prompts the user to enter an ending value.
  • It reads the ending value into the variable n.
  • It uses a goto statement to create a loop that checks if the current value of i is even. If it is, it prints the value of i.
  • It increments i by 1 and checks if it is less than or equal to n. If it is, it goes back to the start label and repeats the loop. If i is greater than n, the loop exits.
  • The program ends.

While this program works correctly, using a goto statement is generally not considered good programming practice because it can make the code difficult to read and maintain. A better alternative would be to use a while or for loop to print the even numbers.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int i,n;
    cout<<"\n Enter the starting value:";
    cin>>i;
    cout<<"\n Enter the Ending value:";
    cin>>n;
    start:
        if(i%2==0)
        {
            cout<<"\n"<<i;
        }
        i++;
    if(i<=n)
    {
        goto start;
    }
    return 0;
}
To download raw file Click Here

Output

Enter the starting value:1
Enter the Ending value:10
2
4
6
8
10

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples