General Java Programs
Common exam-style Java programs built on top of Chapter 01 concepts.
Program 1: Sum and Average of an Integer Array
The following program stores a fixed list of integers in an array, uses an enhanced for -loop to compute the sum, and then divides by the length of the array to find the average.
// WAP to calculate total/sum and average of integer array
public class SumAvgOfArray {
public static void main(String arr[]) {
int sum = 0, avg;
int array[] = {10, 20, 30, 40, 50};
for (int num : array) {
sum = sum + num;
}
int n = array.length;
avg = sum / n;
System.out.println("Total: " + sum);
System.out.println("Average: " + avg);
}
}Output:
Total: 150
Average: 30
Program 2: Check Number is Prime or Not
This program reads an integer from the user and checks whether it is a prime number by testing divisibility from 2 up to num - 1 . If no divisor is found, the number is prime.
// WAP to check whether a number is prime or not
import java.util.Scanner;
public class PrimeOrNot {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number:");
int num = sc.nextInt();
int temp = num;
int i;
for (i = 2; i < num; i++) {
if (num % i == 0) {
break;
}
}
if (temp == i) {
System.out.println("The Number " + temp + " is Prime.");
} else {
System.out.println("The Number " + temp + " is Not a Prime");
}
sc.close();
}
}Output:
Enter Number:5
The Number 5 is Prime.
Program 3: Pattern Printing - Right Angled Triangle
The following program uses nested for-loops to print a right-angled triangle pattern of asterisks. The outer loop controls the number of rows, while the inner loop prints the asterisks in each row.
public class BasicPattern01 {
public static void main(String arr[]) {
for(int i=0;i<10;i++) {
for(int k=0;k<i;k++){
System.out.print("* ");
}
System.out.println(" ");
}
}
}Output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
Program 4: Pattern Printing - Right Angled Triangle (Inverted)
This program prints an inverted right-angled triangle pattern of asterisks using nestedfor-loops. The outer loop controls the number of rows, while the inner loops handle the spaces and asterisks in each row to create the inverted effect.
public class BasicPattern02 {
public static void main(String arr[]) {
for(int i=0;i<10;i++) {
for(int k=0; k<i; k++){
System.out.print(" ");
}
for(int j=10;j>i;j--) {
System.out.print("* ");
}
System.out.println(" ");
}
}
}
Output:
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Program 5: A Simple Vowel Checker
This program prompts the user to enter an alphabet character and uses a switch-case statement to check if the entered character is a vowel (a, e, i, o, u). If it matches any of the vowel cases, it confirms that the character is a vowel; otherwise, it states that the character is a consonant.
// Simple Vowel Checker Program in Java
import java.util.Scanner;
public class VowelChecker {
public static void main(String arr[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Alphabet: ");
String alphabet = sc.next();
switch(alphabet) {
case "a":
System.out.println("Yes the alphabet is Vowel");
break;
case "e":
System.out.println("Yes the alphabet is Vowel");
break;
case "i":
System.out.println("Yes the alphabet is Vowel");
break;
case "u":
System.out.println("Yes the alphabet is Vowel");
break;
default :
System.out.println("The Alphabet is Consonant");
}
sc.close();
}
}
Output:
Enter Alphabet: u
Yes the alphabet is Vowel
Program 6: Fibonacci Series
This program generates and prints the Fibonacci series up to a specified number of terms (10 in this case). It initializes the first two terms and iteratively calculates the next terms by summing the previous two, printing each term in the series.
public class Fibonacci {
public static void main(String[] args) {
int n = 10;
int firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series up to " + n + " terms:");
for (int i = 1; i <= n; ++i) {
System.out.print(firstTerm + ", ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
Output:
Fibonacci Series up to 10 terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,