PROGRAM NO - Docxvk
PROGRAM NO - Docxvk
ALGORITHM:
START
· Initialize Arrays: Create three single-dimensional arrays to store Physics,
Chemistry, and Maths marks for 5 students.
· Input Marks: For each student, input the marks for Physics, Chemistry, and Maths
and store them in the respective arrays.
· Initialize Counters: Set counters for students securing 80% and above, and 30%
and below.
· Calculate Aggregate:For each student, calculate the aggregate of marks from the
three subjects.Compute the percentage from the aggregate.
· Categorize Students:
PROGRAM CODE:
import java.util.Scanner;
// Number of students
final int NUM_STUDENTS = 5;
int[] physicsMarks = new int[NUM_STUDENTS];
int[] chemistryMarks = new int[NUM_STUDENTS];
int[] mathsMarks = new int[NUM_STUDENTS];
// Initialize counters
int count80AndAbove = 0;
int count30AndBelow = 0;
// Display results
System.out.println("Number of students securing 80% and above in aggregate: "
+ count80AndAbove);
System.out.println("Number of students securing 30% and below in aggregate: "
+ count30AndBelow);
scanner.close();
}
}
INPUT AND OUTPUT:
Return
Method Name Description Parameters
Type
Calculates the aggregate int[] marks (array of
calculateAggregate double
percentage of marks marks)
PROGRAM NO:2
PROBLEM DEFINATION:Write a program to accept 20 integer numbers in single
Dimensional
Array. Using menu driven approach display the following, as per user's choice:
i. All the perfect numbers store in the array.
ii. All the Buzz numbers store in the array.
A number whose sum of factors (including 1 and excluding the number itself) is the
same is said to be a perfect number.
A number that is divisible by 7 or has last digit as 7, is said to be a buzz number.
ALGORITHM:
· Start
· Declare an array numbers of size 20.
· Prompt the user to enter 20 integer numbers and store them in the array numbers.
· Display a menu with options:
a. "Display all perfect numbers in the array"
b. "Display all buzz numbers in the array"
c. "Exit"
· Accept the user's choice.
· If choice is 1:
a. Call displayPerfectNumbers() to display all perfect numbers in the array.
· If choice is 2:
a. Call displayBuzzNumbers() to display all buzz numbers in the array.
· If choice is 3:
a. Exit the program.
· If choice is invalid, display an error message and repeat steps 4 to 5.
· End
PROGRAM CODE:
import java.util.Scanner;
while (true) {
// Displaying menu to the user
System.out.println("\nMenu:");
System.out.println("1. Display all Perfect numbers");
System.out.println("2. Display all Buzz numbers");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
// Displaying perfect numbers
System.out.println("Perfect numbers in the array:");
boolean foundPerfect = false;
for (int num : numbers) {
if (isPerfect(num)) {
System.out.print(num + " ");
foundPerfect = true;
}
}
if (!foundPerfect) {
System.out.println("None");
}
break;
case 2:
// Displaying buzz numbers
System.out.println("Buzz numbers in the array:");
boolean foundBuzz = false;
for (int num : numbers) {
if (isBuzz(num)) {
System.out.print(num + " ");
foundBuzz = true;
}
}
if (!foundBuzz) {
System.out.println("None");
}
break;
case 3:
// Exit the program
System.out.println("Exiting...");
sc.close();
return;
default:
System.out.println("Invalid choice! Please try again.");
}
}
}
}
Variable Data
Purpose
Name Type
numbers int[] Array to store 20 integer numbers entered by the user.
choice int Stores the user's menu choice.
num int Represents the number to check if it is perfect or buzz.
Used to calculate the sum of factors for perfect number
sum int
checking.
i int Loop control variable used for iteration.
ALGORITHM:
· Start
· Declare an array numbers of size 10.
· Prompt the user to enter 10 integer numbers and store them in the array numbers.
· Display a menu with options:
a. "Sort in ascending order using Bubble Sort"
b. "Sort in descending order using Selection Sort"
c. "Exit"
· Accept the user's choice.
· If choice is 1:
a. Call bubbleSortAscending() to sort the array in ascending order.
b. Display the sorted array.
· If choice is 2:
a. Call selectionSortDescending() to sort the array in descending order.
b. Display the sorted array.
· If choice is 3:
a. Exit the program.
· If choice is invalid, display an error message and repeat steps 4 to 5.
· End
PROGRAM CODE:
import java.util.Scanner;
while (true) {
// Displaying menu to the user
System.out.println("\nMenu:");
System.out.println("1. Ascending order sorting using Bubble Sort");
System.out.println("2. Descending order sorting using Selection Sort");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
// Ascending order sorting using Bubble Sort
bubbleSortAscending(numbers);
System.out.println("Array sorted in ascending order:");
displayArray(numbers);
break;
case 2:
// Descending order sorting using Selection Sort
selectionSortDescending(numbers);
System.out.println("Array sorted in descending order:");
displayArray(numbers);
break;
case 3:
// Exit the program
System.out.println("Exiting...");
sc.close();
return;
default:
System.out.println("Invalid choice! Please try again.");
}
}
}
Variable Data
Purpose
Name Type
numbers int[] Array to store 10 integer numbers entered by the user.
choice int Stores the user's menu choice.
n int Length of the array in sorting methods.
temp int Temporary variable used for swapping elements.
Index of the largest element in the unsorted portion of the
maxIndex int
array for selection sort.
i, j int Loop control variables.
PROGRAM:4
PROBLEM DEFINATION:Write a program to accept 20 double type numbers in
single Dimensional Array. Using menu
driven approach display the following, as per user's choice:
Case 1: searching an element using linear search algorithm
Case 2: searching an element using binary search algorithm on a sorted array (apply
any sorting on the array)
ALGORITHM:
· Start
· Declare an array numbers of size 20 to store double-type numbers.
· Prompt the user to enter 20 double-type numbers and store them in the array
numbers.
· Display a menu with options:
a. "Search an element using Linear Search"
b. "Search an element using Binary Search on a sorted array"
c. "Exit"
· Accept the user's choice.
· If choice is 1:
a. Prompt the user to enter the element to search using linear search.
b. Call linear Search() to search for the element in the array.
c. If the element is found, display its index; otherwise, display "Element not found."
· If choice is 2:
a. Sort the array using any sorting method (e.g., Arrays.sort()).
b. Prompt the user to enter the element to search using binary search.
c. Call binary Search() to search for the element in the sorted array.
d. If the element is found, display its index; otherwise, display "Element not found."
· If choice is 3:
a. Exit the program.
· If choice is invalid, display an error message and repeat steps 4 to 5.
· End
PROGRAM CODE:
import java.util.Scanner;
import java.util.Arrays;
while (true) {
// Displaying menu to the user
System.out.println("\nMenu:");
System.out.println("1. Search an element using Linear Search");
System.out.println("2. Search an element using Binary Search (on a sorted
array)");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
// Linear search
System.out.print("Enter the element to search (Linear Search): ");
double elementToSearch = sc.nextDouble();
int linearSearchResult = linearSearch(numbers, elementToSearch);
if (linearSearchResult == -1) {
System.out.println("Element not found in the array.");
} else {
System.out.println("Element found at index: " + linearSearchResult);
}
break;
case 2:
// Binary search after sorting the array
System.out.println("Sorting the array for Binary Search...");
bubbleSort(numbers);
System.out.print("Enter the element to search (Binary Search): ");
double elementToSearchBinary = sc.nextDouble();
int binarySearchResult = binarySearch(numbers,
elementToSearchBinary);
if (binarySearchResult == -1) {
System.out.println("Element not found in the array.");
} else {
System.out.println("Element found at index: " + binarySearchResult);
}
break;
case 3:
// Exit the program
System.out.println("Exiting...");
sc.close();
return;
default:
System.out.println("Invalid choice! Please try again.");
}
}
}
PROGRAM NO:5
ALGORITHM:
· Start
· Declare two arrays: states and capitals, both of size 10 to store state names and
their corresponding capitals.
· Prompt the user to enter 10 states and their capitals and store them in the arrays.
· Prompt the user to enter the state whose capital is to be searched.
· Call the searchStateIndex() method to find the index of the entered state in the
states array.
· If the index is not -1:
a. Display the corresponding capital from the capitals array.
· Else:
a. Display a message indicating that the state is not present in the list.
· End
PROGRAM CODE:
import java.util.Scanner;
Data
Variable Name Purpose
Type
states String[] Array to store the names of 10 states entered by the user.
capitals String[] Array to store the corresponding capitals of the 10 states.
The state whose capital is to be searched, entered by the
searchState String
user.
Index of the searched state in the states array, or -1 if not
index int
found.
i int Loop control variable for iteration.
Variable Data
Purpose
Name Type
cities String[] Array to store 10 city names entered by the user.
city String Holds each city name during iteration over the array.
Stores the first character of a city name in lowercase for
firstChar char
checking.
Stores the last character of a city name in lowercase for
lastChar char
checking.
i int Loop control variable for iteration.
PROGRAM NO:7
ALGORITHM:
· Start
· Accept a word from the user and store it in the variable word.
· Convert the word to lowercase and store it in lowerCaseWord.
· Call the method replaceVowels(lowerCaseWord) to replace each vowel in the word
with the letter following it in the alphabet.
a. Initialize a StringBuilder named result.
b. Iterate through each character of the word:
If the character is a vowel ('a', 'e', 'i', 'o', 'u'), append the next letter in the
alphabet to result.
Else, append the character itself to result.
c. Return the modified string from result.
PROGRAM CODE:
import java.util.Scanner;
// Method to replace vowels in the word with the letter following them
public static String replaceVowels(String word) {
StringBuilder result = new StringBuilder();
PROGRAM CODE: 8
PROGRAM CODE:
import java.util.Scanner;
PROGRAM NO:9
· Start
· Accept a sentence from the user and store it in the variable sentence.
· Call the method findLongestWord(sentence) to find the longest word in the
sentence:
a. Split the sentence into words using spaces as a delimiter and store them in the
array words.
b. Initialize an empty string longest to store the longest word found.
c. Iterate through each word in the words array:
If the length of the current word is greater than the length of longest, update
longest with the current word.
d. Return the longest word.
PROGRAM CODE:
import java.util.Scanner;
PROGRAM NO: 10
ALGORITHM:
· Start
· Accept a sentence from the user and store it in the variable sentence.
· Accept an alphabet from the user whose frequency is to be checked and store it in
the variable alphabet.
· Call the method findFrequency(sentence, alphabet) to find the frequency of the
given alphabet in the sentence:
a. Convert the alphabet to lowercase for case-insensitive comparison.
b. Convert the sentence to lowercase for case-insensitive comparison.
c. Initialize a counter count to 0.
d. Iterate through each character in the sentence:
PROGRAM CODE:
import java.util.Scanner;