Priniting Odd Numbers within limit using Goto Statement in C++


This program prints all the odd 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 value of i is odd. If it is, it prints the value of i and increments it by 1.
  • It checks if i is less than or equal to n. If it is, the loop goes back to the start label and repeats. If it is not, 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 for or while loop to print the odd 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==1)
        {
            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
1
3
5
7
9

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