Printing prime between two numbers using While Loop in C++


This program finds and prints all the prime numbers between a given range of starting and ending numbers using a nested loop. Here is a brief explanation of the program:

  • The program first declares and initializes some variables.
  • It asks the user to enter the starting and ending number of the range.
  • It enters into a loop that runs through all numbers between the starting and ending number.
  • Within the outer loop, the program resets a counter variable called "prime" and another variable "n" to 2.
  • It enters into a nested loop that runs through all numbers between 2 and the current number being checked.
  • If the current number is divisible by any number other than 1 and itself, then the "prime" variable is incremented.
  • After the nested loop completes, if the "prime" variable is still 0, then the current number is a prime number and it is printed.
  • Finally, the program increments the current number and starts the next iteration of the outer loop.

This program uses a simple but inefficient algorithm to check for prime numbers. Instead of checking only odd numbers after 2, it checks all numbers between 2 and the current number. This leads to a lot of redundant checks, making the program slower than it needs to be.

Additionally, the program does not handle input validation, so it may fail or produce incorrect results if the user enters invalid input (such as non-numeric characters or negative numbers).

Overall, while this program works correctly and prints all prime numbers within a given range, there are some improvements that could be made to make it faster and more robust.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int i,n=2,a,b,prime=0;
    cout<<"\nEnter the starting number:";
    cin>>i;
    cout<<"\nEnter the ending number:";
    cin>>b;
    while(i<=b)
    {
        prime=0;
        n=2;
        while(n<i)
        {
            if(i%n==0)
            {
                prime++;
            }
            n++;
        }
        if(prime==0)
        {
            cout<<"\n"<<i;
        }
        i++;
    }
    return 0;
}
To download raw file Click Here

Output

Enter the starting number:1
Enter the ending number:20
1
2
3
5
7
11
13
17
19

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