ISC COMPUTER SCIENCE PRACTICAL RECORD
30 Java Programs – Complete Set
Prepared for: Student Record
Date: 05 September 2025
Table of Contents
S.No. Program Title Page
1 To create Pascal’s Triangle
2 To display entered number in words
3 To display A.P. series and its sum
4 To display calendar of any month of any year
5 To calculate factorial using recursion
6 To display Fibonacci series using recursion
7 To calculate GCD using recursion
8 To display spiral matrix
9 To display magic square (odd order)
10 To search an array using Linear Search
11 To search an array using Binary Search
12 To sort an array using Selection sort
13 To sort an array using Bubble sort
14 To convert a decimal number into its binary equivalent
15 To display date from entered day number (given year)
16 To create a pattern from entered string (staircase)
17 To check if entered string is palindrome or not
18 To display frequency of each character in entered string
19 To find a word in entered string
20 To decode the entered string (Caesar shift by -1)
21 To display the entered string in alphabetical order
22 To create a string and count number of vowels and consonants
23 To create a string and count number of words
24 To create a string and replace all vowels with *
25 To create a double-dimensional array of 4×4 subscripts
26 To generate sum of all elements of a 5×5 array
27 To generate product of two arrays of 5 subscripts as a third array
28 To find sum of each column of a double dimensional array
29 To find sum of diagonal of a double dimensional array of 4×4 subscripts
30 To calculate the commission of a salesman
Program 1: To create Pascal’s Triangle
Java Code:
import java.util.Scanner;
public class PascalTriangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
for (int s = 0; s < n - i; s++) System.out.print(" ");
int num = 1;
for (int j = 0; j <= i; j++) {
System.out.print(num + " ");
num = num * (i - j) / (j + 1);
}
System.out.println();
}
sc.close();
}
}
Sample Output:
Enter number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Program 2: To display entered number in words
Java Code:
import java.util.Scanner;
public class NumberInWords {
static String[] ones = {
"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen"
};
static String[] tens = {
"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
};
public static String convert(int n) {
if (n == 0) return "Zero";
if (n < 20) return ones[n];
if (n < 100) return tens[n / 10] + (n % 10 != 0 ? " " + ones[n % 10] : "");
if (n < 1000) return ones[n / 100] + " Hundred" + (n % 100 != 0 ? " " + convert(n % 100)
if (n < 1000000) return convert(n / 1000) + " Thousand" + (n % 1000 != 0 ? " " + convert
if (n < 1000000000) return convert(n / 1000000) + " Million" + (n % 1000000 != 0 ? " " +
return "Number too large!";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
System.out.println("In words: " + convert(num));
sc.close();
}
}
Sample Output:
Enter a number: 4521
In words: Four Thousand Five Hundred Twenty One
Program 3: To display A.P. series and its sum
Java Code:
import java.util.Scanner;
public class APSeries {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first term (a): ");
int a = sc.nextInt();
System.out.print("Enter common difference (d): ");
int d = sc.nextInt();
System.out.print("Enter number of terms (n): ");
int n = sc.nextInt();
int sum = 0;
System.out.print("AP Series: ");
for (int i = 0; i < n; i++) {
int term = a + i * d;
System.out.print(term + (i < n - 1 ? ", " : ""));
sum += term;
}
System.out.println("\nSum = " + sum);
sc.close();
}
}
Sample Output:
Enter first term (a): 2
Enter common difference (d): 3
Enter number of terms (n): 5
AP Series: 2, 5, 8, 11, 14
Sum = 40
Program 4: To display calendar of any month of any year
Java Code:
import java.util.Scanner;
public class MonthCalendar {
static boolean isLeap(int y) {
return (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0);
}
static int dayOfWeek(int d, int m, int y) { // 0=Sunday..6=Saturday
if (m < 3) {
m += 12;
y -= 1;
}
int K = y % 100;
int J = y / 100;
int h = (d + (13 * (m + 1)) / 5 + K + K / 4 + J / 4 + 5 * J) % 7; // Zeller
int dow = ((h + 6) % 7);
return dow;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] months = {"","January","February","March","April","May","June","July","August",
int[] days = {0,31,28,31,30,31,30,31,31,30,31,30,31};
System.out.print("Enter month (1-12): ");
int m = sc.nextInt();
System.out.print("Enter year: ");
int y = sc.nextInt();
if (m == 2 && isLeap(y)) days[2] = 29;
System.out.println("\n " + months[m] + " " + y);
System.out.println("Su Mo Tu We Th Fr Sa");
int start = dayOfWeek(1, m, y);
for (int i = 0; i < start; i++) System.out.print(" ");
for (int d = 1; d <= days[m]; d++) {
System.out.printf("%2d ", d);
if ((d + start) % 7 == 0) System.out.println();
}
System.out.println();
sc.close();
}
}
Sample Output:
Enter month (1-12): 9
Enter year: 2025
September 2025
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Program 5: To calculate factorial using recursion
Java Code:
import java.util.Scanner;
public class FactorialRec {
static long fact(int n) {
if (n < 0) throw new IllegalArgumentException("Negative not allowed");
if (n == 0 || n == 1) return 1;
return n * fact(n - 1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
System.out.println("Factorial = " + fact(n));
sc.close();
}
}
Sample Output:
Enter n: 5
Factorial = 120
Program 6: To display Fibonacci series using recursion
Java Code:
import java.util.Scanner;
public class FibonacciRec {
static int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms: ");
int n = sc.nextInt();
System.out.print("Fibonacci: ");
for (int i = 0; i < n; i++) {
System.out.print(fib(i) + (i < n - 1 ? ", " : ""));
}
System.out.println();
sc.close();
}
}
Sample Output:
Enter number of terms: 7
Fibonacci: 0, 1, 1, 2, 3, 5, 8
Program 7: To calculate GCD using recursion
Java Code:
import java.util.Scanner;
public class GCDRec {
static int gcd(int a, int b) {
if (b == 0) return Math.abs(a);
return gcd(b, a % b);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a = sc.nextInt();
System.out.print("Enter b: ");
int b = sc.nextInt();
System.out.println("GCD = " + gcd(a, b));
sc.close();
}
}
Sample Output:
Enter a: 28
Enter b: 42
GCD = 14
Program 8: To display spiral matrix
Java Code:
import java.util.Scanner;
public class SpiralMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n (size of square matrix): ");
int n = sc.nextInt();
int[][] a = new int[n][n];
int top = 0, bottom = n - 1, left = 0, right = n - 1;
int num = 1;
while (top <= bottom && left <= right) {
for (int i = left; i <= right; i++) a[top][i] = num++;
top++;
for (int i = top; i <= bottom; i++) a[i][right] = num++;
right--;
if (top <= bottom) {
for (int i = right; i >= left; i--) a[bottom][i] = num++;
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) a[i][left] = num++;
left++;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) System.out.printf("%4d", a[i][j]);
System.out.println();
}
sc.close();
}
}
Sample Output:
Enter n (size of square matrix): 3
1 2 3
8 9 4
7 6 5
Program 9: To display magic square (odd order)
Java Code:
import java.util.Scanner;
public class MagicSquare {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter odd n: ");
int n = sc.nextInt();
if (n % 2 == 0) {
System.out.println("Only odd n supported.");
sc.close();
return;
}
int[][] a = new int[n][n];
int num = 1, i = 0, j = n / 2;
while (num <= n * n) {
a[i][j] = num++;
int ni = (i - 1 + n) % n;
int nj = (j + 1) % n;
if (a[ni][nj] != 0) {
i = (i + 1) % n;
} else {
i = ni; j = nj;
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) System.out.printf("%4d", a[i][j]);
System.out.println();
}
sc.close();
}
}
Sample Output:
Enter odd n: 3
8 1 6
3 5 7
4 9 2
Program 10: To search an array using Linear Search
Java Code:
import java.util.Scanner;
public class LinearSearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size: ");
int n = sc.nextInt();
int[] a = new int[n];
System.out.println("Enter elements:");
for (int i = 0; i < n; i++) a[i] = sc.nextInt();
System.out.print("Enter key: ");
int key = sc.nextInt();
int pos = -1;
for (int i = 0; i < n; i++) {
if (a[i] == key) { pos = i; break; }
}
if (pos == -1) System.out.println("Not found");
else System.out.println("Found at index " + pos);
sc.close();
}
}
Sample Output:
Enter size: 5
Enter elements:
3 7 2 9 5
Enter key: 9
Found at index 3
Program 11: To search an array using Binary Search
Java Code:
import java.util.Arrays;
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size: ");
int n = sc.nextInt();
int[] a = new int[n];
System.out.println("Enter elements:");
for (int i = 0; i < n; i++) a[i] = sc.nextInt();
Arrays.sort(a);
System.out.print("Enter key: ");
int key = sc.nextInt();
int l = 0, r = n - 1, pos = -1;
while (l <= r) {
int m = (l + r) / 2;
if (a[m] == key) { pos = m; break; }
else if (a[m] < key) l = m + 1;
else r = m - 1;
}
if (pos == -1) System.out.println("Not found");
else System.out.println("Found at sorted index " + pos);
sc.close();
}
}
Sample Output:
Enter size: 5
Enter elements:
9 2 5 7 3
Enter key: 7
Found at sorted index 3
Program 12: To sort an array using Selection sort
Java Code:
import java.util.Scanner;
public class SelectionSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size: ");
int n = sc.nextInt();
int[] a = new int[n];
System.out.println("Enter elements:");
for (int i = 0; i < n; i++) a[i] = sc.nextInt();
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++)
if (a[j] < a[min]) min = j;
int t = a[i]; a[i] = a[min]; a[min] = t;
}
System.out.print("Sorted: ");
for (int x : a) System.out.print(x + " ");
System.out.println();
sc.close();
}
}
Sample Output:
Enter size: 5
Enter elements:
5 1 4 2 3
Sorted: 1 2 3 4 5
Program 13: To sort an array using Bubble sort
Java Code:
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size: ");
int n = sc.nextInt();
int[] a = new int[n];
System.out.println("Enter elements:");
for (int i = 0; i < n; i++) a[i] = sc.nextInt();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;
}
}
}
System.out.print("Sorted: ");
for (int x : a) System.out.print(x + " ");
System.out.println();
sc.close();
}
}
Sample Output:
Enter size: 5
Enter elements:
5 1 4 2 3
Sorted: 1 2 3 4 5
Program 14: To convert a decimal number into its binary
equivalent
Java Code:
import java.util.Scanner;
public class DecimalToBinary {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter decimal number: ");
int n = sc.nextInt();
if (n == 0) {
System.out.println("Binary: 0");
sc.close();
return;
}
StringBuilder sb = new StringBuilder();
int x = n;
while (x > 0) {
sb.append(x % 2);
x /= 2;
}
System.out.println("Binary: " + sb.reverse());
sc.close();
}
}
Sample Output:
Enter decimal number: 10
Binary: 1010
Program 15: To display date from entered day number
(given year)
Java Code:
import java.util.Scanner;
public class DateFromDayNumber {
static boolean isLeap(int y) {
return (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter year: ");
int y = sc.nextInt();
System.out.print("Enter day number (1-365/366): ");
int dn = sc.nextInt();
int[] mdays = {31,28,31,30,31,30,31,31,30,31,30,31};
if (isLeap(y)) mdays[1] = 29;
int m = 0;
while (dn > mdays[m]) {
dn -= mdays[m];
m++;
}
System.out.println("Date: " + (dn) + "-" + (m+1) + "-" + y);
sc.close();
}
}
Sample Output:
Enter year: 2024
Enter day number (1-365/366): 60
Date: 29-2-2024
Program 16: To create a pattern from entered string
(staircase)
Java Code:
import java.util.Scanner;
public class StringPattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String s = sc.nextLine();
for (int i = 1; i <= s.length(); i++) {
System.out.println(s.substring(0, i));
}
for (int i = s.length() - 1; i >= 1; i--) {
System.out.println(s.substring(0, i));
}
sc.close();
}
}
Sample Output:
Enter a string: HELLO
H
HE
HEL
HELL
HELLO
HELL
HEL
HE
H
Program 17: To check if entered string is palindrome or not
Java Code:
import java.util.Scanner;
public class PalindromeString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String s = sc.nextLine().replaceAll("\\s+", "").toLowerCase();
StringBuilder rev = new StringBuilder(s).reverse();
if (s.equals(rev.toString())) System.out.println("Palindrome");
else System.out.println("Not Palindrome");
sc.close();
}
}
Sample Output:
Enter a string: Never Odd Or Even
Palindrome
Program 18: To display frequency of each character in
entered string
Java Code:
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class CharFrequency {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String s = sc.nextLine();
Map<Character, Integer> freq = new LinkedHashMap<>();
for (char c : s.toCharArray()) {
freq.put(c, freq.getOrDefault(c, 0) + 1);
}
for (Map.Entry<Character, Integer> e : freq.entrySet()) {
System.out.println("'" + e.getKey() + "' : " + e.getValue());
}
sc.close();
}
}
Sample Output:
Enter a string: apple
'a' : 1
'p' : 2
'p' : 2
'l' : 1
'e' : 1
Program 19: To find a word in entered string
Java Code:
import java.util.Scanner;
public class FindWord {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s = sc.nextLine();
System.out.print("Enter word to find: ");
String w = sc.nextLine();
int idx = s.toLowerCase().indexOf(w.toLowerCase());
if (idx == -1) System.out.println("Word not found");
else System.out.println("Word found at index " + idx);
sc.close();
}
}
Sample Output:
Enter a sentence: Java is fun to learn
Enter word to find: fun
Word found at index 8
Program 20: To decode the entered string (Caesar shift by
-1)
Java Code:
import java.util.Scanner;
public class DecodeString {
static char decodeChar(char c) {
if (c >= 'a' && c <= 'z') return (char)('a' + (c - 'a' - 1 + 26) % 26);
if (c >= 'A' && c <= 'Z') return (char)('A' + (c - 'A' - 1 + 26) % 26);
if (c >= '0' && c <= '9') return (char)('0' + (c - '0' - 1 + 10) % 10);
return c;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter encoded string: ");
String s = sc.nextLine();
StringBuilder out = new StringBuilder();
for (char c : s.toCharArray()) out.append(decodeChar(c));
System.out.println("Decoded: " + out);
sc.close();
}
}
Sample Output:
Enter encoded string: Bsfy 312!
Decoded: Arex 201!
Program 21: To display the entered string in alphabetical
order
Java Code:
import java.util.Arrays;
import java.util.Scanner;
public class SortStringChars {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String s = sc.nextLine().replaceAll("\\s+", "");
char[] c = s.toLowerCase().toCharArray();
Arrays.sort(c);
System.out.println("Sorted: " + new String(c));
sc.close();
}
}
Sample Output:
Enter a string: Banana
Sorted: aaabnn
Program 22: To create a string and count number of vowels
and consonants
Java Code:
import java.util.Scanner;
public class VowelConsonantCount {
static boolean isVowel(char c) {
return "aeiou".indexOf(Character.toLowerCase(c)) >= 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String s = sc.nextLine();
int v = 0, cons = 0;
for (char c : s.toCharArray()) {
if (Character.isLetter(c)) {
if (isVowel(c)) v++; else cons++;
}
}
System.out.println("Vowels: " + v);
System.out.println("Consonants: " + cons);
sc.close();
}
}
Sample Output:
Enter a string: Hello World
Vowels: 3
Consonants: 7
Program 23: To create a string and count number of words
Java Code:
import java.util.Scanner;
public class WordCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s = sc.nextLine().trim();
if (s.isEmpty()) {
System.out.println("Words: 0");
} else {
String[] parts = s.split("\\s+");
System.out.println("Words: " + parts.length);
}
sc.close();
}
}
Sample Output:
Enter a sentence: This is ISC CS
Words: 4
Program 24: To create a string and replace all vowels with *
Java Code:
import java.util.Scanner;
public class ReplaceVowels {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String s = sc.nextLine();
System.out.println("Result: " + s.replaceAll("(?i)[aeiou]", "*"));
sc.close();
}
}
Sample Output:
Enter a string: Education
Result: *d*c*t**n
Program 25: To create a double-dimensional array of 4×4
subscripts
Java Code:
import java.util.Scanner;
public class Array4x4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] a = new int[4][4];
System.out.println("Enter 16 integers:");
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
a[i][j] = sc.nextInt();
System.out.println("Matrix:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) System.out.printf("%4d", a[i][j]);
System.out.println();
}
sc.close();
}
}
Sample Output:
Enter 16 integers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Matrix:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Program 26: To generate sum of all elements of a 5×5 array
Java Code:
import java.util.Scanner;
public class Sum5x5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] a = new int[5][5];
System.out.println("Enter 25 integers:");
int sum = 0;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) {
a[i][j] = sc.nextInt();
sum += a[i][j];
}
System.out.println("Sum = " + sum);
sc.close();
}
}
Sample Output:
Enter 25 integers:
[...]
Sum = 325
Program 27: To generate product of two arrays of 5
subscripts as a third array
Java Code:
import java.util.Scanner;
public class ArrayProduct {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = 5;
int[] a = new int[n];
int[] b = new int[n];
int[] c = new int[n];
System.out.println("Enter 5 integers for array A:");
for (int i = 0; i < n; i++) a[i] = sc.nextInt();
System.out.println("Enter 5 integers for array B:");
for (int i = 0; i < n; i++) b[i] = sc.nextInt();
for (int i = 0; i < n; i++) c[i] = a[i] * b[i];
System.out.print("Product array C: ");
for (int x : c) System.out.print(x + " ");
System.out.println();
sc.close();
}
}
Sample Output:
Enter 5 integers for array A:
1 2 3 4 5
Enter 5 integers for array B:
5 4 3 2 1
Product array C: 5 8 9 8 5
Program 28: To find sum of each column of a double
dimensional array
Java Code:
import java.util.Scanner;
public class ColumnSums {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows: ");
int r = sc.nextInt();
System.out.print("Enter cols: ");
int c = sc.nextInt();
int[][] a = new int[r][c];
System.out.println("Enter elements:");
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
a[i][j] = sc.nextInt();
for (int j = 0; j < c; j++) {
int sum = 0;
for (int i = 0; i < r; i++) sum += a[i][j];
System.out.println("Sum of column " + j + " = " + sum);
}
sc.close();
}
}
Sample Output:
Enter rows: 2
Enter cols: 3
Enter elements:
1 2 3
4 5 6
Sum of column 0 = 5
Sum of column 1 = 7
Sum of column 2 = 9
Program 29: To find sum of diagonal of a double
dimensional array of 4×4 subscripts
Java Code:
import java.util.Scanner;
public class DiagonalSum4x4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] a = new int[4][4];
System.out.println("Enter 16 integers:");
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
a[i][j] = sc.nextInt();
int primary = 0, secondary = 0;
for (int i = 0; i < 4; i++) {
primary += a[i][i];
secondary += a[i][3 - i];
}
System.out.println("Primary diagonal sum = " + primary);
System.out.println("Secondary diagonal sum = " + secondary);
sc.close();
}
}
Sample Output:
Enter 16 integers:
[...]
Primary diagonal sum = 34
Secondary diagonal sum = 34
Program 30: To calculate the commission of a salesman
Java Code:
import java.util.Scanner;
public class SalesCommission {
static double commission(double sales) {
if (sales <= 5000) return 0.05 * sales;
else if (sales <= 10000) return 0.08 * sales;
else return 0.10 * sales + 500; // extra bonus on high sales
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter sales amount: ");
double s = sc.nextDouble();
double c = commission(s);
System.out.printf("Commission = %.2f%n", c);
sc.close();
}
}
Sample Output:
Enter sales amount: 12000
Commission = 1700.00