Print Prime Numbers from 1 to 100 in C++

Hello enthusiasts! In this post, we will write a program to print prime numbers from 1-100 in C++. So without wasting time let’s proceed.

Introduction

In the given problem statement, we have to find and print all prime numbers between 1 and 100.

Any number is called a prime number if it has no factors other than one and the number itself. The rest of the numbers are called composite numbers. One is considered neither prime nor composite. You must always remember that the logic remains the same but you just code it in different languages.

Examples of prime numbers:

2, 3, 5, 7, 13

To print all the prime numbers between 1 and 100 we would have to start from 2 (since 1 is neither prime nor composite) and then check for each number’s factors.

We will use a for loop from 2 to 100 to set the number to be checked. Inside it, we will use another for loop from 2 to i to check for factors.

Note: The loop will run till i and not till i because if a number has no factors till i then it won’t have any factor. To prove this let’s presume a factor of i as j such that j>i then there must be another factor i/j which will be smaller than i since both the factors can’t be greater than i, therefore, if there are no factors of i less than i then the number doesn’t have any other factor.

Algorithm

The dry run of the program goes like:

  1. Initial value, chk=0.
  2. Now 1 gets initialized to i, and the condition i<=100 or 1<=100 evaluates to be true, therefore program flow goes inside the loop.
  3. Inside the loop, there is another for loop, so 2 gets initialized to j, and the condition j<i or 2<1 evaluates to be false, therefore program flow does not goes inside this loop’s body.
  4. Now the condition of if gets evaluated. There are two conditions, and between these two conditions, we’ve used && operator. So that, program flow only executes the statement of if’s body until both conditions evaluate to be true.
  5. But this time, its first condition that is chk==0 or 0==0 evaluates to be true, but its second condition, that is i!=1 or 1!=1 evaluates to be false, therefore program flow does not goes inside its body.
  6. Now 0 gets again initialized to chk and program flow goes to the updatation part of outer for loop and increments the value of i. So i=2.
  7. The condition i<=100 or 2<=100 again evaluates to be true, therefore program flow again goes inside the loop.
  8. 2 gets initialized to j and the condition j<i or 2<2 evaluates to be false.
  9. Now the condition of if, that is, chk==0 or 0==0 evaluates to be true, and its second condition, i!=1 or 2!=1 also evaluates to be true. Since its both condition evaluates to be true, therefore program flow goes inside its body and the value of i gets printed as a prime number, that is 2.
  10. Now 0 gets initialized to ch and program flow goes to updatation part of for loop and increments the value of i. So i=3.
  11. The condition i<=100 or 3<=100 again evaluates to be true, therefore program flow again goes inside the loop.
  12. This process continues until the condition evaluates to be false.
  13. In this way, the prime number gets printed one by one.

Code

#include<iostream>
using namespace std;
int main()
{
    int i, chk=0, j;
    cout<<"Prime Numbers Between 1 to 100 are:\n";
    for(i=1; i<=100; i++)
    {
        for(j=2; j<i; j++)
        {
           if(i%j==0)
           {
               chk++;
               break;
           }
        }
        if(chk==0 && i!=1)
            cout<<i<<endl;
        chk = 0;
    }
    cout<<endl;
    return 0;
}

Output

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97

Printing prime numbers from 1 to n

Suppose you need to print prime number from 1 to n where n is the input given by the user. In that case, the logic remains the same and you just ask for input from the user.

#include <iostream>
using namespace std;

int isPrimeNumber(int);

int main() {
   bool isPrime;
   int count;
   cout<<"Enter the value of n:";
   cin>>count;
   for(int n = 2; n < count; n++)
   {
       // isPrime will be true for prime numbers
       isPrime = isPrimeNumber(n);

       if(isPrime == true)
          cout<<n<<" ";
   }
   return 0;
}

// Function that checks whether n is prime or not
int isPrimeNumber(int n) {
   bool isPrime = true;

   for(int i = 2; i <= n/2; i++) {
      if (n%i == 0)
      {
         isPrime = false;
         break;
      }
   }
   return isPrime;
}

Output:

Enter the value of n:50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Conclusion

Hope you find print prime numbers from 1 to 100 in c++ helpful. We hope that you have now understood the program and can implement it on your own.

Stay tuned for more posts and updates. Until then, Happy Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *