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

PIJ File

The document outlines multiple Java programming experiments, including FizzBuzz, Armstrong number check, prime number determination, perfect square and cube checks, and array operations such as sum, average, searching, and finding pairs. It also includes implementations for a menu-driven stack and queue. Each section provides problem statements, algorithms, code examples, and expected outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PIJ File

The document outlines multiple Java programming experiments, including FizzBuzz, Armstrong number check, prime number determination, perfect square and cube checks, and array operations such as sum, average, searching, and finding pairs. It also includes implementations for a menu-driven stack and queue. Each section provides problem statements, algorithms, code examples, and expected outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

EXPERIMENT 1

PROBLEM STATEMENT
WAP to :
a) Print FIZZ if no. is divisible by 3,if no. is divisible by 5 print BUZZ and FIZZBUZZ if
divisible by both. Take input from user from 1-20.

ALGORITHM

CODE
a) import java.util.Scanner;
public class FizzBuzz {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number between 1 and 20: ");
int num = scanner.nextInt();
if (num < 1 || num > 20) {
System.out.println("Please enter a valid number between 1 and 20.");
} else {
if (num % 3 == 0 && num % 5 == 0) {
System.out.println("FIZZBUZZ");
} else if (num % 3 == 0) {
System.out.println("FIZZ");
} else if (num % 5 == 0) {
System.out.println("BUZZ");
} else {
System.out.println(num);
}
}
scanner.close();
}
}

OUTPUT
LEARNING OUTCOME

b) Check if a number is Armstrong number or not.

ALGORITHM
CODE
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isArmstrong(num)) {
System.out.println(num + " is an Armstrong number.");
} else {
System.out.println(num + " is NOT an Armstrong number.");
}
scanner.close();
}
public static boolean isArmstrong(int num) {
int originalNum = num, sum = 0, digits = 0;
int temp = num;
while (temp > 0) {
temp /= 10;
digits++;
}
temp = num;
while (temp > 0) {
int digit = temp % 10;
sum += Math.pow(digit, digits);
temp /= 10;
}
return sum == originalNum;
}
}

LEARNING OUTCOME

c) Check if a number is Prime number or not using efficient technique.


ALGORITHM
CODE
import java.util.Scanner;
public class PrimeNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
scanner.close();
if (isPrime(num)) {
System.out.println(num + " is a Prime number.");
} else {System.out.println(num + " is not a Prime number.");
}
}
public static boolean isPrime(int n) {
if (n <= 1) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) { return false;
}
}
return true;
}
}
OUTPUT

LEARNING OUTCOME

d) Check if a number is perfect square or perfect cube.

ALGORITHM
CODE
import java.util.Scanner;
public class PerfectSquareCube {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
scanner.close();
boolean isPerfectSquare = checkPerfectSquare(num);
boolean isPerfectCube = checkPerfectCube(num);
if (isPerfectSquare && isPerfectCube) {
System.out.println(num + " is both a Perfect Square and a Perfect Cube.");
} else if (isPerfectSquare) {
System.out.println(num + " is a Perfect Square.");
} else if (isPerfectCube) {
System.out.println(num + " is a Perfect Cube.");
} else {
System.out.println(num + " is neither a Perfect Square nor a Perfect Cube.");
}}
public static boolean checkPerfectSquare(int n) {
int sqrt = (int) Math.sqrt(n);
return sqrt * sqrt == n;
}
public static boolean checkPerfectCube(int n) {
int cbrt = (int) Math.cbrt(n);
return cbrt * cbrt * cbrt == n;
}}

OUTPUT

LEARNING OUTCOME
EXPERIMENT-2

PROBLEM STATEMENT
a) Find the sum and average in an array
ALGORITHM

CODE
import java.util.Scanner;
public class SumAndAvg {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();}
scanner.close();
int sum = 0;
for (int num : arr) {
sum += num;}
double average = (double) sum / n;
System.out.println("Sum: " + sum);
System.out.println("Average: " + average);
}}

OUTPUT

LEARNING OUTCOME
b) Find the highest and nth highest element in an array.
ALGORITHM

CODE
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class NthHighestElement {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
Integer[] arr = new Integer[n];
System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) { arr[i] = scanner.nextInt();}
System.out.print("Enter the value of N (for Nth highest element): ");
int N = scanner.nextInt();
scanner.close();
Arrays.sort(arr, Collections.reverseOrder());
System.out.println("Highest element: " + arr[0]);
if (N > 0 && N <= n) { System.out.println(N + "th highest element: " + arr[N - 1]);
} else { System.out.println("Invalid input: N should be between 1 and " + n); }}}

OUTPUT

LEARNING OUTCOME
c) Find an element in an array using Linear search and Binary search.
ALGORITHM

CODE
import java.util.Arrays;
import java.util.Scanner;
public class SearchInArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();}
System.out.print("Enter the element to search: ");
int target = scanner.nextInt();
System.out.println("Choose Search Method: \n1. Linear Search \n2. Binary Search");
int choice = scanner.nextInt();
scanner.close();
int index = -1;
if (choice == 1) {
index = linearSearch(arr, target);
} else if (choice == 2) {
Arrays.sort(arr);
index = binarySearch(arr, target);
} else {
System.out.println("Invalid choice! Please select 1 or 2.");
return;}
if (index != -1) {
System.out.println(target + " found at index " + index);
} else {
System.out.println(target + " not found in the array.");}}
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i; }}
return -1; }
public static int binarySearch(int[] arr, int target) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] > target) {
high = mid - 1;
} else {
low = mid + 1; }}
return -1; }}

OUTPUT

LEARNING OUTCOME
d)Find all pairs from an array following A[i]>A[j] where index i>index j
ALGORITHM

CODE
import java.util.Scanner;
public class FindPairs {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();}
scanner.close();
System.out.println("Pairs where A[i] > A[j] and i > j:");
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (arr[i] > arr[j]) {
System.out.println("(" + arr[i] + ", " + arr[j] + ")"); }}}}}

OUTPUT

LEARNING OUTCOME
EXPERIMENT-3

PROBLEM STATEMENT
a) Implement menu driven Stack using Java.
ALGORITHM

CODE
import java.util.Scanner;
class Stack {
private int[] stack;
private int top;
private int size;
public Stack(int size) {
this.size = size;
stack = new int[size];
top = -1;}
public void push(int value) {
if (top == size - 1) {
System.out.println("Stack Overflow! Cannot push " + value);
} else {
stack[++top] = value;
System.out.println(value + " pushed into the stack.");}}
public void pop() {
if (top == -1) {
System.out.println("Stack Underflow! No elements to pop.");
} else {
System.out.println(stack[top] + " popped from the stack.");
top--;}}
public void peek() {
if (top == -1) {
System.out.println("Stack is empty! No top element.");
} else {
System.out.println("Top element is: " + stack[top]);}}
public boolean isEmpty() {
return top == -1;}
public void display() {
if (top == -1) {
System.out.println("Stack is empty.");
} else {
System.out.print("Stack elements: ");
for (int i = top; i >= 0; i--) {
System.out.print(stack[i] + " ");}
System.out.println();}}}
public class Stack1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter stack size: ");
int size = scanner.nextInt();
Stack stack = new Stack(size);
while (true) {
System.out.println("\n--- Stack Menu ---");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Peek");
System.out.println("4. Check if Stack is Empty");
System.out.println("5. Display Stack");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter value to push: ");
int value = scanner.nextInt();
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
stack.peek();
break;
case 4:
System.out.println(stack.isEmpty() ? "Stack is empty." : "Stack is not empty.");
break;
case 5:
stack.display();
break;
case 6:
System.out.println("Exiting program...");
scanner.close();
return;
default:
System.out.println("Invalid choice! Please enter a valid option.");}}}}

OUTPUT
LEARNING OUTCOME
b)Implement menu driven Queue using Java.
ALGORITHM

CODE
import java.util.Scanner;
class Queue {
private int[] queue;
private int front, rear, size;
public Queue(int size) {
this.size = size;
queue = new int[size];
front = -1;
rear = -1;}
public void enqueue(int value) {
if (rear == size - 1) {
System.out.println("Queue Overflow! Cannot enqueue " + value);
} else {
if (front == -1) {
front = 0; }
queue[++rear] = value;
System.out.println(value + " added to the queue.");}}
public void dequeue() {
if (front == -1 || front > rear) {
System.out.println("Queue Underflow! No elements to dequeue.");
} else {
System.out.println(queue[front] + " removed from the queue.");
front++;
if (front > rear) {
front = -1;
rear = -1;}}}
public void peek() {
if (front == -1) {
System.out.println("Queue is empty! No front element.");
} else {
System.out.println("Front element is: " + queue[front]); }}
public boolean isEmpty() {
return front == -1;}
public void display() {
if (front == -1) {
System.out.println("Queue is empty.");
} else {
System.out.print("Queue elements: ");
for (int i = front; i <= rear; i++) {
System.out.print(queue[i] + " ");}
System.out.println();}}}
public class Queue1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter queue size: ");
int size = scanner.nextInt();
Queue queue = new Queue(size);
while (true) {
System.out.println("\n--- Queue Menu ---");
System.out.println("1. Enqueue");
System.out.println("2. Dequeue");
System.out.println("3. Peek");
System.out.println("4. Check if Queue is Empty");
System.out.println("5. Display Queue");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter value to enqueue: ");
int value = scanner.nextInt();
queue.enqueue(value);
break;
case 2:
queue.dequeue();
break;
case 3:
queue.peek();
break;
case 4:
System.out.println(queue.isEmpty() ? "Queue is empty." : "Queue is not empty.");
break;
case 5:
queue.display();
break;
case 6:
System.out.println("Exiting program...");
scanner.close();
return;
default:
System.out.println("Invalid choice! Please enter a valid option.");}}}}
OUTPUT

LEARNING OUTCOME

You might also like