0% found this document useful (0 votes)
1 views5 pages

Java Programs

The document outlines five essential Java programs for placement interviews, including Fibonacci series, prime number check, minimum and maximum in an array, palindrome check, and character frequency count. Each program includes code snippets and sample input/output for clarity. These programs are designed to help candidates prepare for technical interviews by demonstrating fundamental programming concepts.

Uploaded by

maheswaris634
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

Java Programs

The document outlines five essential Java programs for placement interviews, including Fibonacci series, prime number check, minimum and maximum in an array, palindrome check, and character frequency count. Each program includes code snippets and sample input/output for clarity. These programs are designed to help candidates prepare for technical interviews by demonstrating fundamental programming concepts.

Uploaded by

maheswaris634
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programs — Interview Prep

5 Essential Programs for Placement Interviews

1. Fibonacci Series
Prints first N Fibonacci numbers starting from 0 and 1.
import [Link];

public class Fibonacci {

static void printFibonacci(int n) {

int a = 0, b = 1;

for (int i = 0; i < n; i++) {

[Link](a + " ");

int next = a + b;

a = b;

b = next;

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int n = [Link]();

printFibonacci(n);

Sample I/O: Input: 7 Output: 0 1 1 2 3 5 8

2. Prime Number Check


Checks whether a given number is Prime or Not Prime.
import [Link];
public class PrimeNumber {

static boolean isPrime(int n) {

if (n <= 1) return false;

for (int i = 2; i * i <= n; i++) {

if (n % i == 0) return false;

return true;

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int n = [Link]();

if (isPrime(n))

[Link](n + " is Prime");

else

[Link](n + " is Not Prime");

Sample I/O: Input: 7 → Output: 7 is Prime | Input: 9 → Output: 9 is Not Prime

3a. Minimum in Array


Finds the minimum element in an array.
import [Link];

public class MinArray {

static int findMin(int[] arr) {

int min = arr[0];

for (int i = 1; i < [Link]; i++) {

if (arr[i] < min)

min = arr[i];
}

return min;

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int n = [Link]();

int[] arr = new int[n];

for (int i = 0; i < n; i++)

arr[i] = [Link]();

[Link]("Min: " + findMin(arr));

Sample I/O: Input: 5 → 10 3 8 1 5 Output: Min: 1

3b. Maximum in Array


Finds the maximum element in an array.
import [Link];

public class MaxArray {

static int findMax(int[] arr) {

int max = arr[0];

for (int i = 1; i < [Link]; i++) {

if (arr[i] > max)

max = arr[i];

return max;

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


int n = [Link]();

int[] arr = new int[n];

for (int i = 0; i < n; i++)

arr[i] = [Link]();

[Link]("Max: " + findMax(arr));

Sample I/O: Input: 5 → 10 3 8 1 5 Output: Max: 10

4. Palindrome Check
Checks whether a given string is a Palindrome or Not.
import [Link];

public class Palindrome {

static boolean isPalindrome(String s) {

int start = 0, end = [Link]() - 1;

while (start < end) {

if ([Link](start) != [Link](end))

return false;

start++;

end--;

return true;

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

String s = [Link]();

if (isPalindrome(s))

[Link](s + " is Palindrome");

else
[Link](s + " is Not Palindrome");

Sample I/O: Input: madam → Output: madam is Palindrome | Input: hello → Output: hello is Not
Palindrome

5. Count Frequency of Characters


Counts how many times each character appears in a string.
import [Link];

public class CharFrequency {

static void countFrequency(String s) {

int[] freq = new int[26];

for (int i = 0; i < [Link](); i++) {

freq[[Link](i) - 'a']++;

for (int i = 0; i < 26; i++) {

if (freq[i] > 0)

[Link]((char)(i + 'a') + " = " + freq[i]);

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

String s = [Link]();

countFrequency(s);

Sample I/O: Input: hello Output: e=1 h=1 l=2 o=1

You might also like