Use Eclipse or NetBeans to familiarize yourself with the menus, test project and class creation, running a program, auto suggestions and fill, formatter and refactoring features, and debugging a small program using if/else and for loop.
Experiment 1: Check Whether a Given Number is Prime or Not
Aim
Write a Java program to check whether a given number is prime or not.
Program
/* Sample java program to check given number is prime or not */
//Importing packages
import java.lang.System;
import java.util.Scanner;
// Creating Class
class Sample_Program {
// main method
public static void main(String args[]) {
int i, count = 0, n;
// creating scanner object
Scanner sc = new Scanner(System.in);
// get input number from user
System.out.print("Enter Any Number : ");
n = sc.nextInt();
// logic to check prime or not
for (i = 1; i <= n; i++) {
if (n % i == 0) {
count++;
}
}
if (count == 2)
System.out.println(n + " is prime");
else
System.out.println(n + " is not prime");
}
}
Sample Output
Enter Any Number : 23 23 is prime
Enter Any Number : 45 45 is not prime
💻 Run This Java Program
Use Eclipse or NetBeans to familiarize yourself with the menus, test project and class creation, running a program, auto suggestions and fill, formatter and refactoring features, and debugging a small program using if/else and for loop.
The selected Java program is loaded
automatically into the editor.
Edit the program, provide input if required
and click Run.