C++ Program to Display Armstrong Number Between Two Intervals

In this article, we will understand how to display all the prime numbers from 1 to N in Java. All possible positive numbers from 1 to infinity are called natural numbers.

A number is a prime number if its only factors are 1 and itself and cannot be divided by any other number. 11 is a prime number. Its factors are 1 and 11 itself. Some examples of prime numbers are 2, 3, 5, 7, 11, 13, and so on. 2 is the only even prime number. All other prime numbers are odd numbers.

Below is a demonstration of the same -

<strong>Input: </strong>Suppose our input is ? Enter the value of n :10
<strong>Output: </strong>The output would be- 2 3 5 7

Approach 1:

In this approach, we check each number from 2 to n and count how many times it is divisible without a remainder. If a number is divisible exactly twice (only by 1 and itself), it is printed as a prime number.

Algorithm

<b>Step 1-</b> Start
<b>Step 2-</b> Declare an integer variable N and assign a value to it (e.g., N = 10)  
<b>Step 3-</b> Using a while loop from 1 to n, check if the 'i' value is divisible by any number from 2 to i.
<b>Step 4-</b> If yes, check the next number
<b>Step 5-</b> If no, store the number as a prime number
<b>Step 6-</b> Display the 'i' value as LCM of the two numbers
<b>Step 7-</b> Stop

Example 1

Here, the input is being entered by the user based on a prompt.

import java.util.Scanner;
public class PrimeNumbers{
   public static void main(String arg[]){
      int i,n,counter, j;
      Scanner scanner = new Scanner(System.in);
      System.out.print("Enter the n value : ");
      n=scanner.nextInt();
      System.out.print("Prime numbers between 1 to 10 are ");
      for(j=2; j<=n;j++){
         counter=0;
         for(i=1;i<=j;i++){
            if(j%i==0){
               counter++;
            }
         }
         if(counter==2)
         System.out.print(j+" ");
      }
   }
}

On compiling, the above program gives you the following output.

Enter the n value is 10
Prime numbers between 1 to 10 are 2 3 5 7

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class PrimeNumbers{
   public static void main(String arg[]){
      int i,n,counter, j;
      n= 10;
      System.out.printf("Enter the n value is %d ", n);
      System.out.printf("Prime numbers between 1 to %d are ", n);
      for(j=2;j<=n;j++){
         counter=0;
         for(i=1;i<=j;i++){
            if(j%i==0){
               counter++;
            }
         }
         if(counter==2)
         System.out.print(j+" ");
      }
   }
}

Output

Enter the n value is 10
Prime numbers between 1 to 10 are 2 3 5 7

Approach 2:

In this approach, instead of checking all numbers from 1 to x, it only checks divisibility from 2 up to underroot x (square root of x). If a number x has any factor greater than its square root, it also has a smaller factor less than or equal to underoot x, so checking up tounderroot x will find out if x is prime.

Algorithm

<b>Step 1 -</b> Start  
<b>Step 2 -</b> Declare an integer variable N and assign a value to it (e.g., N = 45)  
<b>Step 3 -</b> Declare integer variables: x (to check each number from 1 to N), y (to check divisibility for x), and flg (to mark whether x is prime or not).
<b>Step 4 -</b> Start a for loop from <b>x = 1 to x = N. </b>This loop checks each number between 1 and N one by one.
<b>Step 5 -</b> Inside the loop, check if x is 1. If yes, skip the current iteration (1 is not a prime number)  
<b>Step 6 -</b> <b>Set flg = 1</b>, assume the number x is prime for now.  
<b>Step 7 -</b> Start an inner for loop from <b>y = 2</b> to <b>y*y <= x</b>. 
<b>Step 8 -</b> Inside the inner loop, check if <b>x % y == 0</b>, If yes, then: Set flg = 0 (x is not a prime number) and exit the inner loop using <b>'break`</b>.  
<b>Step 10 -</b> After the inner loop ends, check if flg is still 1. If yes, then x is a prime number print x.  
<b>Step 11 -</b> Continue to the next value of x in the outer loop.
<b>Step 12 -</b> Repeat Steps 6 to 11 until x equals N.
<b>Step 13 -</b> Stop.

Example

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class PrimeNumbersFrom1ToN {
   public static void main(String[] args) {
      int N = 45;
      System.out.println("All the Prime numbers within 1 and " + N + " are:");
      int x, y, flg;
      for (x = 1; x <= N; x++) {
         if (x == 1) {
            continue;
         }
         flg = 1;
         for (y = 2; y * y <= x; y++) {
            if (x % y == 0) {
               flg = 0;
               break;
            }
         }
         if (flg == 1) {
            System.out.print(x + " ");
         }
      }
   }
}

On compiling, the above program gives you the following output.

All the Prime numbers within 1 and 45 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 
Nishu Kumari
Nishu Kumari

Technical Content Engineer

Updated on: 2025-05-15T19:43:05+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements