Answers for "print prime numbers in java"

2

Java program to print prime numbers in a given range

// Java program to print prime numbers in a given range
public class PrimeNumberRangeDemo 
{
   public static void main(String[] args) 
   {
      int lowerRange = 10, higherRange = 50;  
      while(lowerRange < higherRange) 
      {
         boolean flag = false;
         for(int a = 2; a <= lowerRange / 2; ++a) 
         {
            if(lowerRange % a == 0)
            {
               flag = true;
               break;
            }
         }
         if(!flag && lowerRange != 0 && lowerRange != 1)
             System.out.print(lowerRange + " ");
         ++lowerRange;
      }
   }
}
Posted by: Guest on February-19-2021
12

prime number program in java

public class Prime {
    public static void main(String[] args) {

        int num = 29;
        boolean flag = false;
        for(int i = 2; i <= num/2; ++i){
            if(num % i == 0){
                flag = true;
                break;
            }
        }

        if (!flag)
            System.out.println(num + " is a prime number.");
        else
            System.out.println(num + " is not a prime number.");
}}
Posted by: Guest on May-25-2020
1

Java program to print prime numbers upto n

// Java program to print prime numbers upto n
import java.util.Scanner;
public class PrimeNumberUptoN
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      int a = 0;
      int n = 0;
      String strPrime = "";
      System.out.println("Please enter the value of n: ");
      int number = sc.nextInt();
      for(a = 1; a <= number; a++)
      {
         int counter = 0;
         for(n = a; n >= 1; n--)
         {
            if(a % n == 0)
            {
               counter = counter + 1;
            }
         }
         if(counter == 2)
         {
            strPrime = strPrime + a + " ";
         }
      }
      System.out.println("prime numbers upto n are: ");
      System.out.println(strPrime);
      sc.close();
   }
}
Posted by: Guest on February-19-2021
1

prime in java

public class Prime {
    public static void main(String[] args) {

        int num = 29;
        boolean flag = false;
        for(int i = 2; i <= num/2; ++i){
            if(num % i == 0){
                flag = true;
                break;
            }
        }

        if (!flag)
            System.out.println(num + " is a prime number.");
        else
            System.out.println(num + " is not a prime number.");
}}
Source:www.progra
Posted by: Guest on September-08-2020
0

print prime numbers in java

import java.util.Scanner;
 
public class PrimeNumberChecker {
    public static void main(String[] args) {
        System.out.println("Enter a number to check Prime or Not");
        Scanner scanner = new Scanner(System.in);
        int number = scanner.nextInt();
        int i = 2, count = 0;
        while (i <= number / 2) {
            if (number % i == 0) {
                count++;
                break;
            }
            i++;
        }
        if (count == 0) {
            System.out.println(number + " is prime number");
        } else {
            System.out.println(number + " is not a prime number");
        }
    }
}
Posted by: Guest on December-02-2020
0

print prime numbers in java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class PrimeNumberChecker3 {
 
    public static List<Integer> generatePrimeInRange(int startRange, int endRange) {
        if (startRange >= endRange) {
            System.out.println("endRange must be greater then startRange");
            System.exit(0);
        }
        List<Integer> primeList = new ArrayList<>();
        while (startRange <= endRange) {
            int i = 2, count = 0;
            while (i <= startRange / 2) {
                if (startRange % i == 0) {
                    count++;
                    break;
                }
                i++;
 
            }
            if (count == 0) {
                primeList.add(startRange);
            }
            startRange++;
        }
        return primeList;
    }
 
    public static void main(String[] args) {
        System.out.println("Enter a number range to generate prime numbers in between");
        Scanner scanner = new Scanner(System.in);
        int number1 = scanner.nextInt();
        int number2 = scanner.nextInt();
        List<Integer> primeNumbers = generatePrimeInRange(number1, number2);
        System.out.println("Prime Numbers are");
        System.out.println(primeNumbers);
 
    }
}
Posted by: Guest on December-02-2020

Code answers related to "print prime numbers in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language