Prime Number Program in Java

Prime Number Program in Java:
The prime number is a natural number who value is greater than 1 and can be divisible by 1 and by itself.
The number of counts to find the prime numbers is 2 because it can divisible only by 1 and by itself
For example:
6 - can be divisible by 1x6,2x3,3x2,6x1. where the count is 4 and it is not a prime number
7 - can be divisible by 1x7,7x1. where the count is 2 and it is a prime number

Prime number program in java with examples of fibonacci series, armstrong number, prime number, palindrome number, factorial number, bubble sort, selection sort, insertion sort, swapping numbers, prime number in java,prime number,java,how to print prime number in c,how to print prime numbers in java,prime number program in java,prime numbers,prime number program,java programs,check prime number in java,java program,prime number java,program,prime numbers program in java,prime number program in java youtube,java program to check prime number


Example 1:
Program to check whether the given number is prime number or not

import java.util.Scanner;
public class Primenumbers
{
public static void main(String[] args)
{
int i =2;
boolean flag = true;
System.out.print("Enter the number to find it is prime or not: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n<0)
{
System.out.println("The prime number cannot be negative");
}
else
{
while(i<n)
{
if(n%i==0)
{
flag = false;
break;
}
i++;
}
if(flag)
{
System.out.println(n + " is a prime number");
}
else
{
System.out.println(n + " is not a prime number");
}
}
}
}
Output:
Enter the number to find it is prime or not: 191
191 is a prime number



Example 2:
Program to display first n prime numbers

import java.util.Scanner;
public class Primenumbers
{
public static void main(String[] args)
{
int num=0,i=0;
int counter;
System.out.print("Enter the range of prime numbers: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n<0)
{
System.out.println("Prime numbers cannot be negative");
}
else
{
for(i=1;i<=n;i++)
{
counter = 0;
for(num=1;num<=i;num++)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if(counter==2)
{
System.out.print(i + ",");
}
}
}
}
}
Output:
Enter the range of prime numbers: 200
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,101,103,1
07,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,



Example 3:
Program to find the prime numbers between two numbers

import java.util.Scanner;
public class Primenumbers
{
public static void main(String[] args)
{
int counter;
System.out.print("Enter the starting number: ");
Scanner s = new Scanner(System.in);
int start = s.nextInt();
System.out.print("Enter the ending number: ");
Scanner e = new Scanner(System.in);
int end = e.nextInt();

if(start<0 || end<0)
{
System.out.println("The prime numbers cannot be negative");
}
else
{
for(int i = start; i<=end;i++)
{
counter = 0;
for(int num = 1; num<=i;num++)
{
if(i%num == 0)
{
counter = counter+1;
}
}
if(counter == 2)
{
System.out.print(i + ",");
}
}
}

}
}
Output:
Enter the starting number: 12
Enter the ending number: 80
13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,



Comments

Post a Comment

Popular posts from this blog

Multiple image upload to database and display from it

PHP Login and Registration script

String methods in java