0% found this document useful (0 votes)
3 views

prime all examples java

Uploaded by

Hemanth Kumar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

prime all examples java

Uploaded by

Hemanth Kumar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

*;
import java.lang.Math;

//check a number is prime or not


// public class Main
// {
// public static void main(String[] args)
// {
// Scanner sc=new Scanner(System.in);
// int n=sc.nextInt();
// boolean isprime=true;
// for(int i=2;i<=n-1;i++)
// {
// if(n%i==0)
// isprime=false;
// }
// if(isprime==true)
// System.out.println("true");
// else
// System.out.println("false");
// }
// }

//
<----------------------------------------------------------------------------------
-----

// prime number using functions


// import java.util.*;

// public class Main


// {
// public static boolean isprime(int n)
// {
// boolean isprime=true;
// for(int i=2;i<=n-1;i++)
// {
// if(n%i==0)
// {
// isprime=false;
// break;
// }
// }
// return isprime;
// }
// public static void main(String[] args)
// {
// Scanner sc=new Scanner(System.in);
// int n=sc.nextInt();
// boolean pr=isprime(n);
// System.out.println(pr);
// }
// }

//
<----------------------------------------------------------------------------------
-----

// prime number using functions alternate method


// import java.util.*;

// public class Main


// {
// public static boolean isprime(int n)
// {
// boolean isprime=true;
// for(int i=2;i<=Math.sqrt(n);i++)
// {
// if(n%i==0)
// {
// isprime=false;
// break;
// }
// }
// return isprime;
// }
// public static void main(String[] args)
// {
// Scanner sc=new Scanner(System.in);
// int n=sc.nextInt();
// boolean pr=isprime(n);
// System.out.println(pr);
// }
// }

//
<----------------------------------------------------------------------------------
-----

// prime number in a range


import java.util.*;

public class Main


{
public static boolean isprime(int n)
{
boolean isprime=true;
for(int i=2;i<=Math.sqrt(n);i++)
{
if(n%i==0)
{
isprime=false;
break;
}
}
return isprime;
}
public static void primes_range(int n)
{
for(int i=2;i<=n;i++)
{
if(isprime(i))
{
System.out.println(i);
}
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
primes_range(n);

}
}

You might also like