Program Computer Application
Program Computer Application
The Program:
Java
import [Link];
public class LongestWord {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String a[] = new String[10];
String maxWord = "";
[Link]("Enter 10 words:");
for (int i = 0; i < 10; i++) {
a[i] = [Link]();
// Checking for the longest word
if (a[i].length() > [Link]()) {
maxWord = a[i];
}
}
[Link]("Longest Word: " + maxWord);
[Link]("Length: " + [Link]());
}
}
Java
import [Link];
public class Frequency {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String a[] = new String[10];
[Link]("Enter 10 words:");
for (int i = 0; i < 10; i++) {
a[i] = [Link]();
}
[Link]("Enter word to search frequency: ");
String searchWord = [Link]();
int count = 0;
for (int i = 0; i < 10; i++) {
// Using equals() for exact match comparison
if (a[i].equalsIgnoreCase(searchWord)) {
count++;
}
}
[Link]("Frequency of " + searchWord + " = " + count);
}
}
The Program:
Java
import [Link];
public class SpecialWord {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String a[] = new String[10];
[Link]("Enter 10 words:");
for (int i = 0; i < 10; i++) {
a[i] = [Link]();
}
[Link]("Special words are:");
int count = 0;
for (int i = 0; i < 10; i++) {
String w = a[i].toUpperCase();
char first = [Link](0);
char last = [Link]([Link]() - 1);
if (first == last) {
[Link](a[i]);
count++;
}
}
[Link]("Total Special Words: " + count);
}
}
The Program:
Java
import [Link];
public class CharFrequency {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence: ");
String s = [Link]();
[Link]("Enter character to count: ");
char ch = [Link]().charAt(0);
int count = 0;
for (int i = 0; i < [Link](); i++) {
if ([Link](i) == ch) {
count++;
}
}
[Link]("Frequency of '" + ch + "' is: " + count);
}
}
The Program:
Java
import [Link];
public class ToggleCase {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence: ");
String s = [Link]();
String toggled = "";
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if ([Link](ch)) {
toggled += [Link](ch);
} else if ([Link](ch)) {
toggled += [Link](ch);
} else {
toggled += ch;
}
}
[Link]("Toggled String: " + toggled);
}
}
6. PigLatin Conversion
The Question: Write a program to input a word and convert it to PigLatin form.
(Logic: Find the first vowel. Move everything before the first vowel to the end and add "AY".)
The Program:
Java
import [Link];
public class PigLatin {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a word: ");
String w = [Link]().toUpperCase();
int pos = -1;
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if ("AEIOU".indexOf(ch) != -1) {
pos = i;
break;
}
}
if (pos != -1) {
String pigLatin = [Link](pos) + [Link](0, pos) + "AY";
[Link]("PigLatin Form: " + pigLatin);
} else {
[Link]("No vowels found.");
}
}
}
The Program:
Java
import [Link];
public class CharStats {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter sentence: ");
String s = [Link]();
int v = 0, uc = 0, lc = 0, d = 0, ws = 0;
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if ("AEIOUaeiou".indexOf(ch) != -1) v++;
if ([Link](ch)) uc++;
else if ([Link](ch)) lc++;
if ([Link](ch)) d++;
if ([Link](ch)) ws++;
}
[Link]("Vowels: " + v);
[Link]("Uppercase: " + uc);
[Link]("Lowercase: " + lc);
[Link]("Digits: " + d);
[Link]("Spaces: " + ws);
}
}
1. Character Array Frequency Analysis
The Question: Write a program in Java to create a character array of size $n$ and find the
frequency of:
1. Vowels
2. Upper case letters
3. Lower case letters
4. Digits
5. White spaces
The Program:
Java
import [Link];
public class CharArrayStats {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter array size: ");
int n = [Link]();
char a[] = new char[n];
int v = 0, uc = 0, lc = 0, d = 0, ws = 0;
[Link]("Enter " + n + " characters:");
for (int i = 0; i < n; i++) {
a[i] = [Link]().charAt(0);
// Checking logic
char ch = a[i];
if ("AEIOUaeiou".indexOf(ch) != -1) v++;
if ([Link](ch)) uc++;
else if ([Link](ch)) lc++;
if ([Link](ch)) d++;
if ([Link](ch)) ws++;
}
[Link]("Vowels: " + v);
[Link]("Uppercase: " + uc);
[Link]("Lowercase: " + lc);
[Link]("Digits: " + d);
[Link]("Spaces: " + ws);
}
}
The Program:
Java
import [Link];
public class WordVowels {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String arr[] = new String[5];
[Link]("Enter 5 words:");
for (int i = 0; i < 5; i++) {
arr[i] = [Link]();
}
[Link]("Word\tVowels");
for (int i = 0; i < 5; i++) {
int count = 0;
String word = arr[i].toUpperCase();
for (int j = 0; j < [Link](); j++) {
char ch = [Link](j);
if ("AEIOU".indexOf(ch) != -1) {
count++;
}
}
[Link](arr[i] + "\t" + count);
}
}
}
The Program:
Java
import [Link];
public class VowelWords {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence: ");
String s = [Link]();
// Split sentence into words using split() or a loop
String words[] = [Link](" ");
int count = 0;
[Link]("Words starting with vowels:");
for (String w : words) {
char first = [Link]().charAt(0);
if ("AEIOU".indexOf(first) != -1) {
[Link](w + " ");
count++;
}
}
[Link]("\nTotal frequency: " + count);
}
}
4. Palindrome Word Check
The Question: Write a program in Java to input a word and check if it is a Palindrome or not.
Convert the word to uppercase before checking.
The Program:
Java
import [Link];
public class PalindromeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a word: ");
String word = [Link]().toUpperCase();
String rev = "";
for (int i = [Link]() - 1; i >= 0; i--) {
rev += [Link](i);
}
if ([Link](rev)) {
[Link]("Palindrome");
} else {
[Link]("Not a Palindrome");
}
}
}
Java
import [Link];
public class ToggleCase {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence: ");
String s = [Link]();
String result = "";
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if ([Link](ch)) {
result += [Link](ch);
} else if ([Link](ch)) {
result += [Link](ch);
} else {
result += ch;
}
}
[Link]("Toggled string: " + result);
}
}
The Program:
Java
import [Link];
public class DoubleLetter {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a word: ");
String word = [Link]().toUpperCase();
int count = 0;
for (int i = 0; i < [Link]() - 1; i++) {
if ([Link](i) == [Link](i+1)) {
count++;
}
}
if (count > 0) {
[Link]("Double pair letters found: " + count);
} else {
[Link]("No double pair letters.");
}
}
}
The Program:
Java
import [Link];
public class WordLengthArray {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String arr[] = new String[5];
[Link]("Enter 5 words:");
for (int i = 0; i < 5; i++) {
arr[i] = [Link]();
}
[Link]("Word\tLength");
for (int i = 0; i < 5; i++) {
[Link](arr[i] + "\t" + arr[i].length());
}
}
}
1. Largest, Smallest, and Sum of Array Elements
The Question: Write a program to input 20 integer elements into an array. Find and display the
Largest number, the Smallest number, and the Sum of all elements.
The Program:
Java
import [Link];
public class ArrayStats {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a[] = new int[20];
int sum = 0;
[Link]("Enter 20 elements:");
for (int i = 0; i < 20; i++) {
a[i] = [Link]();
}
int max = a[0];
int min = a[0];
for (int i = 0; i < 20; i++) {
if (a[i] > max) max = a[i];
if (a[i] < min) min = a[i];
sum += a[i];
}
[Link]("Largest Number: " + max);
[Link]("Smallest Number: " + min);
[Link]("Sum of All Elements: " + sum);
}
}
2. Character Array Frequency (Vowels, Case, Digits)
The Question: Create a character array of size $n$. Input characters and count the frequency
of Vowels, Upper Case letters, Lower Case letters, Digits, and White Spaces.
The Program:
Java
import [Link];
public class CharFreq {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter size of array: ");
int n = [Link]();
char a[] = new char[n];
int v = 0, uc = 0, lc = 0, d = 0, ws = 0;
[Link]("Enter " + n + " characters:");
for (int i = 0; i < n; i++) {
a[i] = [Link]().charAt(0);
char ch = a[i];
if ("AEIOUaeiou".indexOf(ch) != -1) v++;
if ([Link](ch)) uc++;
else if ([Link](ch)) lc++;
if ([Link](ch)) d++;
if ([Link](ch)) ws++;
}
[Link]("Vowels: " + v);
[Link]("Uppercase: " + uc);
[Link]("Lowercase: " + lc);
[Link]("Digits: " + d);
[Link]("Spaces: " + ws);
}
}
3. Linear Search (Name Search)
The Question: Input the names of 10 students in an array. Accept a name to be searched and
check for its existence using Linear Search. If found, display its position; otherwise, print
"Name not found."
The Program:
Java
import [Link];
public class LinearSearchNames {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String names[] = new String[10];
[Link]("Enter 10 names:");
for (int i = 0; i < 10; i++) names[i] = [Link]();
[Link]("Enter name to search: ");
String searchName = [Link]();
int pos = -1;
for (int i = 0; i < 10; i++) {
if (names[i].equalsIgnoreCase(searchName)) {
pos = i;
break;
}
}
if (pos != -1) {
[Link]("Found at position: " + (pos + 1));
} else {
[Link]("Name not found.");
}
}
}
4. Binary Search (Integer Array)
The Question: Search for an integer element in a sorted array using the Binary Search
technique.
The Program:
Java
import [Link];
public class BinarySearch {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; // Pre-sorted array
[Link]("Enter number to search: ");
int x = [Link]();
int low = 0, high = [Link] - 1, mid, pos = -1;
while (low <= high) {
mid = (low + high) / 2;
if (a[mid] == x) {
pos = mid;
break;
} else if (x > a[mid]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (pos != -1) [Link]("Element found at index: " + pos);
else [Link]("Element not present.");
}
}
5. Bubble Sort (Ascending Order)
The Question: Write a program to sort an array of 10 integers in ascending order using the
Bubble Sort technique.
The Program:
Java
import [Link];
public class BubbleSort {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a[] = new int[10];
[Link]("Enter 10 integers:");
for (int i = 0; i < 10; i++) a[i] = [Link]();
// Bubble Sort Logic
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9 - i; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
[Link]("Sorted Array:");
for (int x : a) [Link](x + " ");
}
}
The Program:
Java
import [Link];
public class SelectionSort {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a[] = new int[10];
[Link]("Enter 10 integers:");
for (int i = 0; i < 10; i++) a[i] = [Link]();
// Selection Sort Logic
for (int i = 0; i < 9; i++) {
int maxIdx = i;
for (int j = i + 1; j < 10; j++) {
if (a[j] > a[maxIdx]) {
maxIdx = j;
}
}
int temp = a[i];
a[i] = a[maxIdx];
a[maxIdx] = temp;
}
[Link]("Sorted Array (Descending):");
for (int x : a) [Link](x + " ");
}
}
1. Rules for Naming Identifiers (Variables)
The Question: What is an identifier? What are the rules for naming variables in Java? Write a
program to show valid and invalid declarations.
The Program:
Java
The Program:
Java
public class EscapeSequenceDemo {
public static void main(String[] args) {
// \n for new line, \t for tab space, \b for backspace
[Link]("Hello\nWorld"); // Prints on two lines
[Link]("Name:\tTarun"); // Inserts a tab space
[Link]("Java\b Programming"); // Deletes the 'a' in Java
[Link]("He said, \"Hello!\""); // To print double quotes
}
}
The Program:
Java
The Program:
Java
import [Link];
public class TernaryDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter two numbers: ");
int a = [Link]();
int b = [Link]();
// condition ? expression1 : expression2
int max = (a > b) ? a : b;
[Link]("Maximum value is: " + max);
}
}
The Program:
Java
public class MathExpressions {
public static void main(String[] args) {
double a = 3, b = 4;
double x = 10, y = 25;
double s = 27;
// (i) Square root of sum of squares
double z = [Link]([Link](a, 2) + [Link](b, 2));
// (ii) Absolute difference
double d = [Link](x - y);
// (iii) Cube root
double v = [Link](s);
[Link]("z: " + z); // 5.0
[Link]("d: " + d); // 15.0
[Link]("v: " + v); // 3.0
}
}
The Program:
Java
The Program:
Java
The Program:
Java
2. Function Overloading
The Question: Explain function overloading with an example. Write a program with multiple
methods named area() to calculate the area of a square, a rectangle, and a circle.
The Program:
Java
The Program:
Java
The Program:
Java
The Program:
Java
The Program:
Java
The Program:
Java
Plaintext
1
12
123
1 2 3 4
The Program:
Java
The Program:
Java
import [Link];
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
long fact = 1;
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
[Link]("Factorial of " + n + " is: " + fact);
}
}
The Program:
Java
import [Link];
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int count = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
count++;
}
}
if (count == 2) {
[Link](num + " is a Prime Number.");
} else {
[Link](num + " is not a Prime Number.");
}
}
}
6. Sum of Series
The Question: Write a program to compute and display the sum of a mathematical series (e.g.,
$1 + 2 + 3 + ... + n$).
The Program:
Java
import [Link];
public class SeriesSum {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of terms (n): ");
int n = [Link]();
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
[Link]("The sum of the series is: " + sum);
}
}
1. Square Star Pattern
The Question: Write a program to print a 4x4 square pattern of stars using nested loops.
Plaintext
* * * *
* * * *
* * * *
* * * *
The Program:
Java
*
* *
* * *
* * * *
The Program:
Java
The Program:
Java
import [Link];
public class PyramidNum {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter how many lines: ");
int n = [Link]();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
[Link]("* ");
}
[Link]();
}
}
}
Java
import [Link];
public class CenteredPyramid {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int r = [Link]();
for (int i = 1; i <= r; i++) {
// Loop for spaces to shift stars to the right
for (int k = 1; k <= r - i; k++) {
[Link](" ");
}
// Loop for stars
for (int j = 1; j <= i; j++) {
[Link]("* ");
}
[Link]();
}
}
}
Plaintext
A P P L E
A P P L
A P P
A P
A
The Program:
Java
The Program:
Java
The Program:
Java
The Program:
Java
The Program:
Java
public class BreakDemo {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Terminates the loop immediately
}
[Link](i + " ");
}
// Output: 1 2 3 4
}
}
The Program:
Java
The Program:
Java
import [Link];
public class OddEvenCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
// Modulo operator (%) returns the remainder
if (num % 2 == 0) {
[Link](num + " is Even.");
} else {
[Link](num + " is Odd.");
}
}
}
1. Basic Constructor (Default Constructor)
The Question: What is a constructor? Write a program to demonstrate a default constructor
that initializes a variable x to a specific value when an object is created.
The Program:
Java
2. Parameterized Constructor
The Question: Explain parameterized constructors. Write a program to initialize an object's
state (like student marks or roll number) using values passed during object creation.
The Program:
Java
3. Constructor Overloading
The Question: What is constructor overloading? Write a program for a Student class that has
one constructor to initialize only the roll number and another to initialize both the roll number
and marks.
The Program:
Java
The Program:
Java
5. Copy Constructor
The Question: What is a copy constructor? Write a program where one object is initialized
using the values of another existing object of the same class.
The Program:
Java
The Program:
Java
Java
import [Link];
public class MathOperations {
int a, b, sum;
// Method to accept input
void accept() {
Scanner sc = new Scanner([Link]);
[Link]("Enter value for a: ");
a = [Link]();
[Link]("Enter value for b: ");
b = [Link]();
}
// Method for processing
void calculate() {
sum = a + b;
}
// Method for output
void display() {
[Link]("The total sum is: " + sum);
}
public static void main(String[] args) {
MathOperations obj = new MathOperations();
[Link](); // Step 1: Input
[Link](); // Step 2: Process
[Link](); // Step 3: Output
}
}
The Program:
Java
import [Link];
public class SimpleMath {
// Single method to handle input, process, and output
public void subtract() {
Scanner sc = new Scanner([Link]);
int n1, n2, result;
[Link]("Enter first number: ");
n1 = [Link]();
[Link]("Enter second number: ");
n2 = [Link]();
result = n1 - n2;
[Link]("The result of subtraction is: " + result);
}
public static void main(String[] args) {
// Creating the object
SimpleMath sm = new SimpleMath();
// Calling the method
[Link]();
}
}
1. Basic User-Defined Method (Addition/Subtraction)
The Question: What is a user-defined method? Write a program to create a method named
subtraction() that takes two integers as parameters and returns their difference.
The Program:
Java
2. Method Overloading
The Question: Demonstrate method overloading. Create a class with multiple methods named
result() that have different parameter lists (e.g., one taking two integers and another taking
three integers).
The Program:
Java
3. Call by Value
The Question: Explain "Call by Value" with a program. Show how changes made to a parameter
inside a method do not affect the original variable passed from the main method.
The Program:
Java
4. Call by Reference
The Question: Explain "Call by Reference" using a program. Show how passing an object as an
argument allows the method to modify the original object's state.
The Program:
Java
The Program:
Java
Java
Java
The Program:
Java
import [Link].*;
class PalindromeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number: ");
int num = [Link]();
// Save a copy because num will become 0 during processing
int copy = num;
int rv = 0; // To store reversed number
while (num > 0) {
int digit = num % 10; // Extract last digit
rv = (rv * 10) + digit; // Build reverse
num = num / 10; // Remove last digit
}
if (copy == rv) {
[Link]("Given number is a Palindrome Number.");
} else {
[Link]("Given number is NOT a Palindrome Number.");
}
}
}
2. Even-Pal Number Program (2024 Board Question)
The Question: Write a program to accept a number and check whether it is an Even-Pal
Number or not.
An Even-Pal number must satisfy two conditions:
1. It must be a Palindrome Number (equal to its reverse).
2. The sum of its digits must be an Even Number.
(Example: 121 is a Palindrome and its digit sum $1+2+1=4$ is even. So, 121 is an
Even-Pal number).
The Program:
Java
import [Link].*;
class EvenPalNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number: ");
int num = [Link]();
int copy = num;
int sum = 0; // To store sum of digits
int rv = 0; // To store reverse of number
while (num > 0) {
int digit = num % 10;
sum = sum + digit; // Calculating sum of digits
rv = (rv * 10) + digit; // Calculating reverse
num = num / 10;
}
// Checking both conditions: Palindrome AND sum of digits is even
if (copy == rv && sum % 2 == 0) {
[Link]("It is an Even-Pal Number.");
} else {
[Link]("It is NOT an Even-Pal Number.");
}
}
}
The Question: Pronic Number Check
Write a program to input a number and check whether it is a Pronic Number or not.
● Definition: A Pronic Number is a number which is the product of two consecutive integers.
● Examples:
○ $12 = 3 \times 4$ (Product of consecutive integers 3 and 4)
○ $20 = 4 \times 5$ (Product of consecutive integers 4 and 5)
○ $42 = 6 \times 7$ (Product of consecutive integers 6 and 7)
Java