Java program to print prime numbers in java
import java.util.Scanner;
public class
PrimeNumberExample
{
public static void main(String args[])
{
System.out.println("Enter the number till which prime number to
be printed: ");
int limit = new Scanner(System.in).nextInt();
int limit = new Scanner(System.in).nextInt();
System.out.println("Printing prime number from 1 to " +
limit);
for(int number = 2; number<=limit; number++)
for(int number = 2; number<=limit; number++)
{
if(isPrime(number)){
System.out.println(number);
}
}
}
System.out.println(number);
}
}
}
public static boolean isPrime(int number)
{
for(int i=2; i
for(int i=2; i
{
if(number%i == 0)
if(number%i == 0)
{
return false;
}
}
return true;
}
}
return false;
}
}
return true;
}
}
Java program for prime number
import java.util.*;
class PrimeNum
{
public static void main(String args[])
{
int n;
int i;
System.out.println(" enter the number to evaluate for prime number: ");
Scanner sc=new Scanner(System.in);
n = sc.nextInt();
for(i=2; i<=n/2; i++)
{
if(n%i==0)
{
System.out.println(n + " is not a Prime Number ");
return;
}
}
System.out.println(n + " is a Prime Number ");
}
}
Java
program to check if an integer is prime or not
import java.util.Scanner;
public class Testing
{
public static void main(String args[])
{
Scanner scnr = new Scanner(System.in);
int number = Integer.Max_value;
System.out.println("Enter number to check if prime or not ");
while (number != 0)
{
number = scnr.nextInt();
System.out.printf("Does %d is prime? %s %s %s %n", number,
isPrime(number), isPrimeOrNot(number), isPrimeNumber(number));
}
}
public static boolean isPrime(int number)
{
int sqrt = (int) Math.sqrt(number) + 1;
for (int i = 2; i < sqrt; i++)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
public static boolean isPrimeNumber(int number)
{
if (number == 2 || number == 3)
{
return true;
}
if (number % 2 == 0)
{
return false;
}
int sqrt = (int) Math.sqrt(number) + 1;
for (int i = 3; i < sqrt; i += 2)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
public static String isPrimeOrNot(int num)
{
if (num < 0)
{
return "not valid";
}
if (num == 0 || num == 1)
{
return "not prime";
}
if (num == 2 || num == 3) {
return "prime number";
}
if ((num * num - 1) % 24 == 0)
{
return "prime";
}
else
{
return "not prime";
}
}
}
What is method overloading and overriding in java
public class OverloadingOverridingTest
{
public static void main(String[] args)
{
Loan cheapLoan = Loan.createLoan("HSBC");
Loan veryCheapLoan = Loan.createLoan("Citibank");
Loan personalLoan = new PersonalLoan();
personalLoan.toString();
}
}
public class Loan
{
private double interestRate;
private String customer;
private String lender;
public static Loan createLoan(String lender)
{
Loan loan = new Loan();
loan.lender = lender;
return
loan;
}
public static Loan createLoan(String lender,
double interestRate)
{
Loan loan = new Loan();
loan.lender = lender;
loan.interestRate =
interestRate;
return
loan;
}
public String toString()
{
return
"This is Loan by Citibank";
}
}
public class PersonalLoan extends Loan
{
public String toString()
{
return
"This is Personal Loan by Citibank";
}
}
Find
hidden files in java
import java.io.File;
public class FileHiddenExample
{
public static void main(String[] args)throws
SecurityException ,IOException
{
File file = new File("C:/HiddenTest.txt");
if (file.isHidden())
{
System.out.println("This
file is Hidden file: ");
}
else
{
System.out.println("File is not Hidden
");
}
}
}
No comments:
Post a Comment