write program to find numbers in java

Simple Code to Find Prime Numbers Between 1-100 Easily

In this tutorial we are going to write a script for prime numbers 10-99 which you can use in your code easily. We might need such scripts for many things. So here is this whole program will work. We write a program first to print prime numbers from 1-100 and then the nextprgraom will take the ‘n’ value and if there are any prime numbers it would print them. We will run a loop for the prime numbers and gradually in a list of 1-100 we would have all these prime numbers sorted through our script.

class UniqNoDemo
{
public static void main (String[] args)
{
int i =0;
int num =0;
//Empty String
String UniqNoDemo = "";

for (i = 1; i <= 100; i++) { int myFlag=0; for(num =i; num>=1; num--)
{
if(i%num==0)
{
myFlag = myFlag + 1;
}
}
if (myFlag ==2)
{
//Appended the Prime number to the String
UniqNoDemo = UniqNoDemo + i + " ";
}
}
System.out.println("Prime numbers from 1 to 100 are :");
System.out.println(UniqNoDemo);
}
}

Output:
Prime numbers from 1 to 100 are :
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

Our next piece of code will be showing the prime numbers from 1 up to N this way we will learn how we can execute everything seprately and still it helps us get it done quickly.

Script To Find Prime Numbers From 1 To N

import java.util.Scanner;
class UniqNoDemo2
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
int i =0;
int num =0;
//Empty String
String UniqNoDemo = "";
System.out.println("Enter the value of n:");
int n = scanner.nextInt();
scanner.close();
for (i = 1; i <= n; i++) { int myFlag=0; for(num =i; num>=1; num--)
{
if(i%num==0)
{
myFlag = myFlag + 1;
}
}
if (myFlag ==2)
{
//Appended the Prime number to the String
UniqNoDemo = UniqNoDemo + i + " ";
}
}
System.out.println("Prime numbers from 1 to n are :");
System.out.println(UniqNoDemo);
}
}

Program To Find Prime Numbers Between Given Interval We can easily code this in Java and find the Prime Numbers between the intervals with the help fo static method.

Script to Find Prime Numbers 1 To N Using Static Method 

class UniqNoDemo
{
public static void main(String arg[])
{

System.out.println("Enter a number ");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
UniqNoDemoCal(n);
}
static void UniqNoDemoCal(int num)
{
int flag=0;
for(int i=1;i<=num;i++)
{
if(num%i==0)
{
flag++;
}
}
if(flag==2)
System.out.println("UniqNoDemo number ");
else
System.out.println("Not a UniqNoDemo number ");
}
}

The next we need an other script for to find the prime numbers from ‘1 to n’ numbers which will be written in Java.

class UniqNoDemo
{
public static void main(String arg[])
{
int i,flag;
System.out.print("Enter n value : ");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println("Prime numbers between 1 to "+n+" are ");
for(int j=2;j<=n;j++)
{
flag=0;
for(i=1;i<=j;i++)
{
if(j%i==0)
{
flag++;
}
}
if(flag==2)
System.out.print(j+" ");
}
}
}

Here we are using Java to script our code in which we will use Recursion and also ‘For and While Loops’ methods. Please read the code meticulously to find the difference and how to get it done without errors.

Here we will the recursion method and find the prime numbers we are looking for.

(1) Finding Prime Numbers Using Recursion

class UniqNoDemo
{
static int flag=0,i=1;
int UniqNoDemoOrNot(int num)
{
if(i<=num)
{
if(num%i==0)
{
flag++;
}
i++;
UniqNoDemoOrNot(num);
}
return flag;
}
public static void main(String arg[])
{
System.out.println("Enter a number ");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
UniqNoDemo p=new UniqNoDemo();
int c=p.UniqNoDemoOrNot(n);
if(c==2)
System.out.println("prime number ");
else
System.out.println("Not a prime number ");
}
}

(2) First we will test our script using a While Loop. Here is the code exampe:

class FindUniqNoDemo

{
FindUniqNoDemo(int num)
{
int flag=0,i=1;
while(i<=num)
{
if(num%i==0)
{
flag++;
}
i++;
}
if(flag==2)
System.out.println(num+" is a prime number ");
else
System.out.println(num+" is a Not a prime number ");
}
}
class UniqNoDemo
{
public static void main(String arg[])
{
System.out.println("Enter a number ");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
new FindUniqNoDemo(n);
}
}

The next we will try the same trick but this time we will use For Loop to find these prime numbers.

(3) Finding Prime Numbers Using For Loop

class UniqNoDemo
{
public static void main(String arg[])
{
int flag=0;
System.out.println("Enter a number ");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
flag++;

}
}
if(flag==2)
System.out.println("prime number ");
else
System.out.println("Not a prime number ");
}
}

So this is what exactly the program is all about. We can easily manipulate it however we want. It’s just the example. We can easily find prime numbers between 1 to 100 using this simple code.