Advertisements

Aatul Palandurkar

Posts Tagged ‘Hibernate Training

Objectives :

  • Write a Java program to find prime number upto N number
  • Write a Java program to print prime number upto N number
  • Write a Java program to find prime number upto N number using Command Line Arguments
  • Write a Java Program to find prime number upto N number via Command Line Arguments
  • Write a Java program to find prime number upto N number using Scanner class

 

Java Program / Code :

Method 1 : Java Program to find Prime Number upto N number using Scanner class


import java.util.Scanner;
class PrimeNumber
{
public static void main(String[] args)
{
int n,p;
Scanner s=new Scanner(System.in);
System.out.println(“Enter number : ”);
n=s.nextInt();
for(int i=2;i<n;i++)
{
p=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
p=1;
}
if(p==0){
System.out.println(i);
}
}
}
}

Method 2 : Java Program to find Prime Number upto N number using Scanner class and writing function to find prime number

Method isPrime() for checking if a number is prime or not

public class PrimeNumber{
 public boolean isPrime(int num) {
 if ( num < 2 ){
 return false;
 }
 for (int i = 2; i <= Math.sqrt(num); i++) {
 if ( num % i == 0 ) {
 return false;
 }
 }
 return true;
 }
}


// Mock Test Class to test above code
import java.util.Scanner;

public class Demo
{
 public static void main(String[] args) 
 {
 Scanner scanner = new Scanner(System.in);

 System.out.println("Please enter a number: ");
 int num = scanner.nextInt();
 
 if ( num < 2 ) {
 System.out.println("\n There are no Prime Numbers available");
 System.exit(0);
 }
 System.out.println("\n Prime Numbers from 1 to "+ num);
 PrimeNumber primeNum = new PrimeNumber();

 for (int i = 3; i <= num; i++) {
 if ( primeNum.isPrime(i) ) {
 System.out.print(", " + i);
 }
 }
 } 
}

Method 3 : Java Program to find Prime Number upto N number using Command line input


class PrimeNumber
{
public static void main(String[] args)
{
int n,p;
n=Integer.parseint(args[0]);
for(int i=2;i&lt;n;i++)
{
p=0;
for(int j=2;j&lt;i;j++)
{
if(i%j==0)
p=1;
}
if(p==0){
System.out.println(i);
}
}
}
}

Steps to run above program via command line :

  1. Compilation : C:\JavaPrograms>javac PrimeNumber.java
  2. Interpretation : C:\JavaPrograms>java PrimeNumber 20

Output :

2

3

5

7

11

13

17

19

Advertisements
Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
CDAC-GTU-BISAG WIMC Batch

BISAG-CDAC-GTU WIMC Batch

Conducted Java Training at CDAC for M.Tech. students at Bhaskaracharya Institute of Space Applications and Geo-Informatics (BISAG), Info City, Gandhinagar, Gujrat under Gujrat Technical University (GTU). Seen a great collaboration between BISAG, CDAC and GTU, never seen such program for students where are students getting chance to work on research projects and all.

During the training covered Java, Java EE, Struts, Hibernate, Spring, EJB, RMI, Networking, etc. all in 5 days daily 8 hours. It helps a lot to cover that much if you use NetBeans for training Java and other Java related technologies like JavaEE, Struts, Hibernate, Spring, HTML5, etc. Students were good.

I feel honored after receiving Diwali Gift and Dinner party from students. I would also like to thanks them for Akshardham visit and really love the fountain and water show at Akshardham. There are only 4 water shows like that all over the world. I really enjoyed a lot guys thanks a lot. And I would like to share the thing that you are doing for physically and mentally challenged people, I have never seen this from students.


Advertisements

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 13,202 other followers

Tweets

YouTube

Archives

Advertisements
%d bloggers like this: