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

OOPS WITH JAVA LAB FILE

The document contains multiple Java programs demonstrating basic programming concepts such as printing messages, handling primitive data types, calculating sums of odd and even numbers, finding factorials, reading integers, identifying prime numbers, handling command line arguments, and sorting arrays using various algorithms. Each program includes code snippets and prompts for user input, showcasing fundamental programming techniques. The document serves as a practical guide for beginners learning Java programming.
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

OOPS WITH JAVA LAB FILE

The document contains multiple Java programs demonstrating basic programming concepts such as printing messages, handling primitive data types, calculating sums of odd and even numbers, finding factorials, reading integers, identifying prime numbers, handling command line arguments, and sorting arrays using various algorithms. Each program includes code snippets and prompts for user input, showcasing fundamental programming techniques. The document serves as a practical guide for beginners learning Java programming.
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 11

Program 1: WAP in java to print “Hello World.

” WAP in
java to print primitive data type.

class Hello{

public static void main(String[] args)

System.out.println("hello world");

For preemptive data type:

class First2 {

public static void main(String[] args) {

int intvalue = 5;

float floatvalue = 3;

double doublevalue = 32;

long longvalue = 3456L;

char charvalue = 'S';

boolean booleanvalue = true;

Short Shortvalue = 35;

byte bytevalue = 12;

System.out.println("intvalue "+ intvalue);

System.out.println("floatvalue "+ floatvalue);

System.out.println("doublevalue "+ doublevalue);

System.out.println("longvalue "+ longvalue);

System.out.println("charvalue "+ charvalue);

System.out.println("booleanvalue "+ booleanvalue);


System.out.println("shortvalue " + Shortvalue);

System.out.println("bytevalue " + bytevalue);

Output:

(1)

(2)
Program 2: WAP in java to Sum of Odd and Even
Number.
import java.util.*;

class Sum2 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.print("Enter Start and End point of range\n");

System.out.print("Start: ");

int start = s.nextInt();

System.out.print("End : ");

int end = s.nextInt();

int evenSum = 0;

int oddSum = 0;

for (int i = start; i <= end; i++) {

if (i % 2 == 0) {

evenSum += i;

} else {

oddSum += i;

System.out.println("Sum of even numbers: " + evenSum);

System.out.println("Sum of odd numbers: " + oddSum);

}
Output:
Program 3: WAP in java to Find Factorial of any
given number.
import java.util.*;

public class Factorial {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = s.nextInt();

int factorial = calculate(num);

System.out.println("Factorial of "+ num +":"+ factorial);

public static int calculate(int num) {

if (num == 0) {

return 1;

else

return num * calculate(num - 1);

Output:
Program 4: WAP in java to Read an Integer Value
Through Scanner Class and Find prime Number
within given range.

import java.util.Scanner;
public class ReadInteger {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = s.nextInt();
System.out.println("You are entered" + num);
}
}

Output:

 Find prime number:

import java.util.Scanner;

public class Prime {


public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.print("Enter lower limit : ");

int lowerLimit = s.nextInt();

System.out.print("Enter upper limit : ");

int upperLimit = s.nextInt();

System.out.println("Prime numbers :");

for (int i = lowerLimit; i <= upperLimit; i++) {

if (isPrime(i)) {

System.out.print(i + ", ");

public static boolean isPrime(int number) {

if (number <= 1) {

return false;

for (int i = 2; i <= Math.sqrt(number); i++) {

if (number % i == 0) {

return false;

return true;

Output:
Program 5: WAP in java that uses length
property for displaying any Number of command
line arguments.
class Command{
public static void main(String args[]){
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}

Output:
Program 6: WAP in java to sort n numbers using bubble
sort, selection sort and insertion sort.
import java.util.Scanner;
public class Sorting {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
} }
} }

public static void selectionSort(int[] arr) {


int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter elements:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println();
bubbleSort(arr);
System.out.println(" Bubble Sort:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
selectionSort(arr);
System.out.println(" Selection Sort:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
insertionSort(arr);
System.out.println(" Insertion Sort:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();

}}

Output:

You might also like