Skip to main content

Java program for prime numbers

A prime number is the number which is is  divisible by itself and not by other numbers.


A program to check prime number can be made by using for and while loop combination .

This article contains java program for finding prime number ,write a java program for prime number between 1- 100 ,simple program to find prime factorial of a number in java.

EXAMPLE 1 - program to check whether a number is prime number  or not.
import java.util.*;
public class pr
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter number");
int num=sc.nextInt();
int d=0;
for(int i=2;i<=num;i++)
{
if(num%i==0)
d++;
}
if(d==0)
System.out.println("it is a prime number");
else
System.out.println("it is not a prime number");
}
}

Output
enter number
23
it is a prime number


A prime factorial of a number is the products of all the prime numbers less than the number.


EXAMPLE 2 - program to get prime factorial of a number
import java.util.*;
public class primorial
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
int n=sc.nextInt();
int sum=1;
primorial m=new primorial();
for(int i=2;i<=n;i++)
{
if(m.checkPrime(i)==true)
{
sum=sum*i;
}
}
System.out.println("prime factorial of "+n+" is: "+sum);
}
boolean checkPrime(int a)
{
int d=0;
for(int i=2;i<a;i++)
{
if(a%i==0)
d++;
}
if(d==0)
return true;
else
return false;
}
}

Output
enter a number
23
prime factorial of 23 is : 111546435
EXAMPLE 3-java program to find prime 
numbers between 1 and 100
  
import java.util.*;
Public class primecheck
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("prime numbers are:");
primecheck m=new primecheck();
for(int i=2;i<=100;i++)
{
if(m.checkPrime(i)==true)
{
System.out.println(i);
}
}
}
boolean checkPrime(int a)
{
int d=0;
for(int i=2;i<a;i++)
{
if(a%i==0)
d++;
}
if(d==0)
return true;
else
return false;
}
}

Output
Prime number are: 3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
If you find this helpful please share

Comments

Popular posts from this blog

How to Learn whole syllabus of subject in 1 day? + some cool last minute tips

Hi everyone,  In  this article I am going to tell you how to complete the whole syllabus for exam in one day. I will tell you a full one day plan to complete all your syllabus without having any doubt and also prepare for that subject. To follow this, firstly you have to divide the syllabus of your subjects into 3 parts-  The first part is for easy chapters second part is for moderate chapters and third parties for difficult chapters in easy chapters you have to put it at topics for which you can answer all the question.  In moderate you have to put that chapters in which you have very less doubt. In the difficult you have to put that topic whose questions you are not able to answer. HERE IS THE TEMPLATE FOR YOUR PLANNING- PLAN starting in the morning  Prepare a chart of the whole day time table. Begin your day at 8:00 a.m.  In the first three hours revise or learn all the chapters which are easy for you remember after every 50 minutes you have to take 10 m...

Emirp number in java, program to check emirp number

Emirp is simply the reverse prononciation of word "prime". An emirp number is the number which is prime and whose reverse is also a prime number. EXAMPLE-1 17  Reverse of the number = 71 Both the numbers are prime so 17 is an emirp number. EXAMPLE-2 37 Reverse of the number = 73 Both the numbers are prime so 37 is an emirp number. EXAMPLE-3 13 Reverse of the number = 31 Both the numbers are prime so 13 is an emirp number. Emrip number can be easily made in java with the help of methods/functions. Firstly, take an input of number, then check it is prime or not, then reverse the number and again check it is prime or not, if both are true, print the suitable output. ALGORITHM- STEP 1 : start STEP 2 : print "enter a number" STEP 3 : take input of number in e STEP 4 : int n=num, int rev =0 STEP 5 : repeat STEP to STEP until n>0 STEP 6 : int d=n%10 STEP 7 : rev=(rev*10)+d STEP 8 : n=n/10 STEP 9 :  int p= input value from STEP 13 STEP 10 : int d=0, x=num STEP 11 : int i...

Niven number in java

A number is said to be niven or harshad if the number is divisible by the sum of its digits. EXAMPLE-135 The sum of digits are: 1 + 3 + 5                                                                  135/9=15 A simple java program to check whether a number is harshad / niven or not can be made with the help of while loop. For this, firstly we extract the digits of the number and then we sum up all the digits.After all the digits are summed then we check whether rhe number when divided by the sum of its digits leaves any remainder or not. If the remainder is zero then number is niven / harshad number else not. ALGORITHM - STEP 1: Start STEP 2 : print "enter a number" STEP 3 : taking input of integer in n STEP 4 : int s = 0 STEP 5 : int no = n STEP 6 : repeat STEP 7 to STEP 9 until no!=0 STEP 7 : int d=no%10 STEP 8 : s=s+d...