COMPUTER APPLICATIONS
PROJECT
TERM I
KAUSTUBH CHAGANTI
GRADE - X
SECTION - C
Program 1 Code
public class NumberPattern {
public static void main(String[] args) {
// Loop through odd numbers from 1 to 9
for (int i = 1; i <= 9; i += 2) {
// Inner loop to print numbers in descending order
for (int j = i; j >= 1; j -= 2) {
System.out.print(j + " ");
}
// Move to the next line after each row
System.out.println();
}
}
}
Program 2 Code
public class BinaryPattern {
public static void main(String[] args) {
// Outer loop to control the number of rows
for (int i = 1; i <= 5; i++) {
// Inner loop to print the binary numbers (1 and 0)
for (int j = 1; j <= i; j++) {
// Print 1 if j is odd, 0 if j is even
if (j % 2 == 1) {
System.out.print("1 ");
} else {
System.out.print("0 ");
}
}
// Move to the next line after each row
System.out.println();
}
}
}
Program 3 Code
public class WordPattern {
public static void main(String[] args) {
// Define the string to be printed
String word = "ICSE";
// Outer loop to control the number of rows
for (int i = 1; i <= word.length(); i++) {
// Inner loop to print characters from the string
for (int j = 0; j < i; j++) {
System.out.print(word.charAt(j) + " ");
}
// Move to the next line after each row
System.out.println();
}
}
}
Program 4 Code
import java.util.*;
public class LibraryFine {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner sc = new Scanner(System.in);
// Prompt the user to enter the number of late days
System.out.print("Enter the number of days the book is
late: ");
int daysLate = sc.nextInt();
double fine = 0; // Variable to store the total fine
// Calculate the fine based on the number of days late
if (daysLate <= 5) {
fine = daysLate * 0.40; // 40 paise per day for first 5
days
} else if (daysLate <= 10) {
fine = (5 * 0.40) + ((daysLate - 5) * 0.65); // 40 paise for
first 5 days, 65 paise for next 5 days
} else {
fine = (5 * 0.40) + (5 * 0.65) + ((daysLate - 10) * 0.80); //
40 paise for first 5, 65 paise for next 5, 80 paise for the rest
}
// Print the total fine
System.out.println("The total fine is: Rs " + fine);
}
}
Program 5 Code
import java.util.*;
public class NumberToWords {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner sc = new Scanner(System.in);
// Prompt the user to enter a number
System.out.print("Enter a number: ");
int number = sc.nextInt();
// Convert the number to a string
String numStr = Integer.toString(number);
// Loop through each character in the string and print the
corresponding word
for (int i = 0; i < numStr.length(); i++) {
char digit = numStr.charAt(i);
// Convert each digit to its corresponding word
switch (digit) {
case '0':
System.out.print("ZERO ");
break;
case '1':
System.out.print("ONE ");
break;
case '2':
System.out.print("TWO ");
break;
case '3':
System.out.print("THREE ");
break;
case '4':
System.out.print("FOUR ");
break;
case '5':
System.out.print("FIVE ");
break;
case '6':
System.out.print("SIX ");
break;
case '7':
System.out.print("SEVEN ");
break;
case '8':
System.out.print("EIGHT ");
break;
case '9':
System.out.print("NINE ");
break;
}
}
}
}
Program 6 Code
import java.util.*;
public class NeonNumber {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner sc = new Scanner(System.in);
// Prompt the user to enter a number
System.out.print("Enter a number: ");
int number = sc.nextInt();
// Calculate the square of the number
int square = number * number;
// Find the sum of the digits of the square
int sumOfDigits = 0;
while (square > 0) {
sumOfDigits += square % 10; // Add the last digit of the
square to sum
square = square/10; // Remove the last digit from the
square
}
// Check if the sum of digits is equal to the original number
if (sumOfDigits == number) {
System.out.println(number + " is a Neon Number.");
} else {
System.out.println(number + " is not a Neon Number.");
}
}
}
Program 7 code
import java.util.*;
public class VowelReplace {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner sc = new Scanner(System.in);
// Prompt the user to enter a word
System.out.print("Enter a word: ");
String word = sc.nextLine().toLowerCase(); // Convert to
lowercase
// Initialize an empty string to store the result
String result = "";
// Loop through each character in the word
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
// Check if the character is a vowel
if (ch == 'a') {
result += 'b';
} else if (ch == 'e') {
result += 'f';
} else if (ch == 'i') {
result += 'j';
} else if (ch == 'o') {
result += 'p';
} else if (ch == 'u') {
result += 'v';
} else {
// If it's not a vowel, just add the character to the result
result += ch;
}
}
// Display the new word
System.out.println("New word: " + result);
}
}
Program 8 Code
import java.util.*;
public class LetterFrequency {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner sc = new Scanner(System.in);
// Prompt the user to enter a sentence
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine().toUpperCase(); // Convert
to uppercase
// Array to store the frequency of each letter
int[] frequency = new int[26];
// Count the frequency of each letter
for (char ch : sentence.toCharArray()) {
if (ch >= 'A' && ch <= 'Z') { // Check if it's a letter
frequency[ch - 'A']++; // Increment the count for the
letter
}
}
// Display the frequency of each letter
for (char ch = 'A'; ch <= 'Z'; ch++) {
if (frequency[ch - 'A'] > 0) { // Print only if the letter
appears
System.out.println(ch + ": " + frequency[ch - 'A']);
}
}
}
}
Program 9 Code
import java.util.*;
public class WordFrequency {
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a sentence and a word to
search for
System.out.print("Enter a sentence: ");
String sentence = scanner.nextLine();
System.out.print("Word to be searched: ");
String wordToSearch = scanner.nextLine();
// Split the sentence into words and count the frequency of
the specified word
String[] words = sentence.split(" "); // Split the sentence
into an array of words
int frequency = 0;
// Loop through the array and count occurrences of the
word
for (int i = 0; i < words.length; i++) {
if (words[i].equalsIgnoreCase(wordToSearch)) { //
Compare ignoring case
frequency++;
}
}
// Print the frequency of the word in the sentence
System.out.println("Frequency: " + frequency);
}
}
Program 10 Code
import java.util.*;
public class PalindromeWords {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Scanner to
read user input
System.out.print("Enter a sentence: ");
String sentence = scanner.nextLine(); // Read the
sentence
String[] words = sentence.split(" "); // Split the sentence
into words
System.out.println("Palindrome words:");
for (int i = 0; i < words.length; i++) { // Standard for loop to
iterate through the words
String word = words[i]; // Get the current word
String reversed = ""; // Variable to hold the reversed
word
// Reverse the word manually
for (int j = word.length() - 1; j >= 0; j--) {
reversed += word.charAt(j); // Add each character to
reversed
}
// Check if the word is a palindrome
if (word.equalsIgnoreCase(reversed)) {
System.out.println(word); // Print the palindrome word
}
}
}
}
Program 11 Code
import java.util.*;
public class PigLatin {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Scanner to
read user input
System.out.print("Enter a word: ");
String word = scanner.nextLine(); // Read the word
// Convert the word to Pig Latin
String pigLatin = word.substring(1) + word.charAt(0) +
"AY"; // Move first letter to end and add "AY"
// Display the Pig Latin form
System.out.println("Pig Latin: " +
pigLatin.toUpperCase()); // Convert to uppercase and print
}
}
Program 12 Code
import java.util.*;
public class StudentMarks {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Scanner to
read user input
System.out.print("Enter the number of students: ");
int N = scanner.nextInt(); // Read the number of students
scanner.nextLine(); // Consume the newline character
String[] names = new String[N]; // Array to store names
int[] totalMarks = new int[N]; // Array to store total marks
int sum = 0; // Variable to store the sum of total marks
// Input names and total marks for each student
for (int i = 0; i < N; i++) {
System.out.print("Enter name of student " + (i + 1) + ":
");
names[i] = scanner.nextLine(); // Read name
System.out.print("Enter total marks of student " + (i + 1)
+ ": ");
totalMarks[i] = scanner.nextInt(); // Read total marks
sum += totalMarks[i]; // Add marks to sum
scanner.nextLine(); // Consume the newline character
}
// Calculate average
double average = (double) sum / N; // Calculate average
marks
System.out.println("Average marks: " + average);
// Calculate and print deviation for each student
System.out.println("Deviation from average marks:");
for (int i = 0; i < N; i++) {
double deviation = totalMarks[i] - average; // Calculate
deviation
System.out.println(names[i] + ": " + deviation);
}
}
}
Program 13 Code
import java.util.*;
public class SimpleBubbleSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Scanner to
read user input
int[] numbers = new int[20]; // Array to store 20 integers
// Input 20 integers from the user
System.out.println("Enter 20 integers:");
for (int i = 0; i < 20; i++) {
numbers[i] = scanner.nextInt(); // Read each integer
}
// Sort first 10 numbers in ascending order
for (int i = 0; i < 10 - 1; i++) {
for (int j = 0; j < 10 - 1 - i; j++) {
if (numbers[j] > numbers[j + 1]) {
// Swap numbers[j] and numbers[j + 1]
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
// Sort next 10 numbers in descending order
for (int i = 10; i < 20 - 1; i++) {
for (int j = 10; j < 20 - 1 - (i - 10); j++) {
if (numbers[j] < numbers[j + 1]) {
// Swap numbers[j] and numbers[j + 1]
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
// Print the complete list of integers
System.out.println("Sorted List:");
for (int i = 0; i < 20; i++) {
System.out.print(numbers[i] + " "); // Print each number
}
}
}
Program 14 Code
import java.util.*;
public class SimpleYearSearch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); //
Scanner to read user input
// Sorted array of graduation years
int[] graduationYears = {2000, 2001, 2002, 2003, 2004,
2005, 2006, 2007, 2008, 2009};
System.out.print("Enter your year of graduation: ");
int year = scanner.nextInt(); // Read the year of
graduation
// Perform binary search
boolean found = false; // Variable to track if year is
found
int left = 0; // Starting index
int right = graduationYears.length - 1; // Ending index
while (left <= right) {
int mid = (left + right) / 2; // Calculate middle index
if (graduationYears[mid] == year) {
found = true; // Year found
break; // Exit loop
} else if (graduationYears[mid] < year) {
left = mid + 1; // Search in the right half
} else {
right = mid - 1; // Search in the left half
// Output result
if (found) {
System.out.println("Record exists");
} else {
System.out.println("Record does not exist");
scanner.close(); // Close the scanner
Program 15 Code
import java.util.*;
public class StoreSales {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); //
Scanner to read user input
// Double-dimensional array to store monthly sales
double[][] sales = new double[5][6];
// Input sales data
System.out.println("Enter monthly sales for 5 stores
and 6 departments:");
for (int i = 0; i < 5; i++) { // Loop through stores
for (int j = 0; j < 6; j++) { // Loop through departments
System.out.print("Store " + (i + 1) + ", Department
" + (j + 1) + ": ");
sales[i][j] = scanner.nextDouble(); // Read sales
for each store-department pair
// Calculate and display total sales for each store
System.out.println("\nTotal Monthly Sales for Each
Store:");
for (int i = 0; i < 5; i++) {
double storeTotal = 0; // Initialize store total
for (int j = 0; j < 6; j++) {
storeTotal += sales[i][j]; // Sum up sales for each
department
System.out.println("Store " + (i + 1) + ": " +
storeTotal);
// Calculate and display total sales for each department
System.out.println("\nTotal Monthly Sales for Each
Department:");
for (int j = 0; j < 6; j++) {
double departmentTotal = 0; // Initialize department
total
for (int i = 0; i < 5; i++) {
departmentTotal += sales[i][j]; // Sum up sales for
each store
System.out.println("Department " + (j + 1) + ": " +
departmentTotal);
Program 16 Code
import java.util.*;
public class StoreSales {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Scanner to
read user input
// Double-dimensional array to store monthly sales
double[][] sales = new double[5][6];
// Input sales data
System.out.println("Enter monthly sales for 5 stores and 6
departments:");
for (int i = 0; i < 5; i++) { // Loop through stores
for (int j = 0; j < 6; j++) { // Loop through departments
System.out.print("Store " + (i + 1) + ", Department " +
(j + 1) + ": ");
sales[i][j] = scanner.nextDouble(); // Read sales for
each store-department pair
}
}
// Calculate and display total sales for each store
System.out.println("\nTotal Monthly Sales for Each
Store:");
for (int i = 0; i < 5; i++) {
double storeTotal = 0; // Initialize store total
for (int j = 0; j < 6; j++) {
storeTotal += sales[i][j]; // Sum up sales for each
department
}
System.out.println("Store " + (i + 1) + ": " + storeTotal);
}
// Calculate and display total sales for each department
System.out.println("\nTotal Monthly Sales for Each
Department:");
for (int j = 0; j < 6; j++) {
double departmentTotal = 0; // Initialize department total
for (int i = 0; i < 5; i++) {
departmentTotal += sales[i][j]; // Sum up sales for
each store
}
System.out.println("Department " + (j + 1) + ": " +
departmentTotal);
}
}
}
Program 17 Code
import java.util.*;
public class SelectionSortDescending {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Scanner to
read user input
int[] numbers = new int[20]; // Array to store 20 integers
// Input 20 integers from the user
System.out.println("Enter 20 integers:");
for (int i = 0; i < 20; i++) { // Loop to read integers
numbers[i] = scanner.nextInt(); // Read each integer
}
// Selection Sort in descending order
for (int i = 0; i < numbers.length - 1; i++) {
int maxIndex = i; // Assume the max is the first unsorted
element
for (int j = i + 1; j < numbers.length; j++) {
if (numbers[j] > numbers[maxIndex]) {
maxIndex = j; // Update maxIndex if a larger
element is found
}
}
// Swap the found maximum element with the first
unsorted element
if (maxIndex != i) {
int temp = numbers[i];
numbers[i] = numbers[maxIndex];
numbers[maxIndex] = temp;
}
}
// Print the sorted list of integers
System.out.println("Sorted List in Descending Order:");
for (int i = 0; i < 20; i++) { // Loop to print sorted integers
System.out.print(numbers[i] + " "); // Print each number
}
}
}
Program 18 Code
import java.util.*;
public class SelectionSortDescending {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Scanner to
read user input
int[] numbers = new int[20]; // Array to store 20 integers
// Input 20 integers from the user
System.out.println("Enter 20 integers:");
for (int i = 0; i < 20; i++) { // Loop to read integers
numbers[i] = scanner.nextInt(); // Read each integer
}
// Selection Sort in descending order
for (int i = 0; i < numbers.length - 1; i++) {
int maxIndex = i; // Assume the max is the first unsorted
element
for (int j = i + 1; j < numbers.length; j++) {
if (numbers[j] > numbers[maxIndex]) {
maxIndex = j; // Update maxIndex if a larger
element is found
}
}
// Swap the found maximum element with the first
unsorted element
if (maxIndex != i) {
int temp = numbers[i];
numbers[i] = numbers[maxIndex];
numbers[maxIndex] = temp;
}
}
// Print the sorted list of integers
System.out.println("Sorted List in Descending Order:");
for (int i = 0; i < 20; i++) { // Loop to print sorted integers
System.out.print(numbers[i] + " "); // Print each number
}
}
}
Program 19 Code
import java.util.*;
public class MatrixSums {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Scanner to
read user input
int[][] matrix = new int[4][4]; // Declare a 4x4 matrix
int rowSum, colSum; // Variables to store row and column
sums
int leftDiagonalSum = 0; // Sum for left diagonal
int rightDiagonalSum = 0; // Sum for right diagonal
// Input the elements of the matrix
System.out.println("Enter the elements of the 4x4 matrix:");
for (int i = 0; i < 4; i++) { // Loop through rows
for (int j = 0; j < 4; j++) { // Loop through columns
System.out.print("Element [" + i + "][" + j + "]: ");
matrix[i][j] = scanner.nextInt(); // Read each element
}
}
// Calculate sums of rows, columns, and diagonals
System.out.println("\nSum of each row:");
for (int i = 0; i < 4; i++) { // Loop through rows
rowSum = 0; // Reset row sum
for (int j = 0; j < 4; j++) {
rowSum += matrix[i][j]; // Sum the elements in the row
}
System.out.println("Sum of row " + i + ": " + rowSum); //
Print row sum
}
System.out.println("\nSum of each column:");
for (int j = 0; j < 4; j++) { // Loop through columns
colSum = 0; // Reset column sum
for (int i = 0; i < 4; i++) {
colSum += matrix[i][j]; // Sum the elements in the
column
}
System.out.println("Sum of column " + j + ": " + colSum);
// Print column sum
}
// Calculate sums of the diagonals
for (int i = 0; i < 4; i++) {
leftDiagonalSum += matrix[i][i]; // Sum for left diagonal
rightDiagonalSum += matrix[i][3 - i]; // Sum for right
diagonal
}
// Print the diagonal sums
System.out.println("\nSum of left diagonal: " +
leftDiagonalSum);
System.out.println("Sum of right diagonal: " +
rightDiagonalSum);
}
}
Program 20 Code
import java.util.*;
public class SymmetricMatrix {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Scanner to
read user input
int[][] matrix = new int[4][4]; // Declare a 4x4 matrix
boolean isSymmetric = true; // Flag to check if the matrix is
symmetric
// Input the elements of the matrix
System.out.println("Enter the elements of the 4x4 matrix:");
for (int i = 0; i < 4; i++) { // Loop through rows
for (int j = 0; j < 4; j++) { // Loop through columns
System.out.print("Element [" + i + "][" + j + "]: ");
matrix[i][j] = scanner.nextInt(); // Read each element
}
}
// Check if the matrix is symmetric
for (int i = 0; i < 4; i++) { // Loop through rows
for (int j = 0; j < 4; j++) { // Loop through columns
if (matrix[i][j] != matrix[j][i]) { // Compare elements
isSymmetric = false; // Set flag to false if not
symmetric
break; // Exit inner loop
}
}
if (!isSymmetric) { // Exit outer loop if not symmetric
break;
}
}
// Output result
if (isSymmetric) {
System.out.println("The matrix is symmetric."); // Print if
symmetric
} else {
System.out.println("The matrix is not symmetric."); //
Print if not symmetric
}
}
}
VARIABLE DESCRIPTION TABLES
1)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `i` | int | Loop variable for the rows. |
| `j` | int | Loop variable for the columns.
|
2)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `i` | int | Loop variable for the rows. |
| `j` | int | Loop variable for the columns.
3)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `i` | int | Loop variable for the rows. |
| `j` | int | Loop variable for the columns.
|
4)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------| |
`daysLate` | int | Number of days a book is returned late. | |
`fine` | double | Total fine calculated based on days late. |
5)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------| |
`num` | int | The current number being checked if it's a tech
number. | | `sum` | int | The sum of the two parts of the number.
6)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `number` | int | The number input by the user.
|
| `digit` | int | Each individual digit of the number.
7)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `number` | int | The input number to check if it is a
Neon number.
| `square` | int | Square of the input number.
8)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `inputWord` | String | The word input by the user.
|
| `outputWord` | String | The modified word after vowel
replacement. |
9)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `sentence` | String | The input sentence provided by the
user. |
| `frequency` | int[] | Array to store the frequency of each
letter. |
10)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `sentence` | String | The input sentence provided by the
user. |
| `word` | String | The word whose frequency is to be
counted. |
11)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `words` | String[]| Array to store individual words from the
input. |
| `word` | String | The current word being checked for
palindrome. |
12)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `word` | String | The input word provided by the user.
|
| `pigLatinWord` | String | The converted word in Pig Latin
form. |
13)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `names` | String[]| Array to store the names of students.
|
| `totalMarks` | int[] | Array to store the total marks of
students. |
14)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `numbers` | int[] | Array to store 20 integers.
|
| `temp` | int | Temporary variable used for swapping
elements. |
15)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `year` | int | The year of graduation input by the user.
|
| `records` | int[] | Array containing the sorted years of
graduation. |
16)
| Variable | Type | Description
|
|------------------|--------------|--------------------------------------------------|
| `m` | double[][] | 2D array to store monthly sales for
stores and departments. |
| `storeTotal` | double | Total sales for the current store.
|
17)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `array` | int[] | Array to store 20 integers to be sorted.
|
| `maxIndex` | int | Index of the maximum element in the
unsorted part of the array. |
18)
| Variable | Type | Description |
|------------------|---------|-----------------------------------------------------|
| `matrix` | int[][] | 2D array to store the 4x4 matrix.
|
| `rowSum` | int | Sum of the elements in the current row.
|
19)
| Variable | Type | Description
|
|------------------|---------------|-------------------------------------------------
----|
| `matrix` | int[][] | 2D array to store the 4x4 matrix.
|
| `isSymmetric` | boolean | indicate if the matrix is
symmetric. |