Week 6 - Level 2 - 10 (Step) Rithish S
Week 6 - Level 2 - 10 (Step) Rithish S
Regno:RA2411003012057
Sec:L2
1. An organization took up the exercise to find the Body Mass Index (BMI) of all the per-
sons in a team of 10 members. For this create a program to find the BMI and display
the height, weight, BMI, and status of each individual.
import java.util.Scanner;
2. Find unique characters in a string using the charAt() method and display the result.
import java.util.Scanner;
if (isUnique) {
uniqueChars[uniqueCount] = current;
uniqueCount++;
}
}
return result;
}
scanner.close();
}
}
Output:
Enter the text:
hello world
Unique characters: h e l o w r d
3. Write a program to find the first non-repeating character in a string and show the
result
import java.util.Scanner;
public class FirstNonRepeatingCharacter {
int length = 0;
try {
while (true) {
char c = text.charAt(length);
freq[c]++;
length++;
}
} catch (IndexOutOfBoundsException e) {
}
return '\0';
}
scanner.close();
}
}
Output:
Enter the text:
Success
First non-repeating character: u
4. Write a program to find the frequency of characters in a string using the charAt()
method and display the result.
import java.util.Scanner;
int length = 0;
try {
while (true) {
char c = text.charAt(length);
freq[c]++;
length++;
}
} catch (IndexOutOfBoundsException e) {
}
return finalResult;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the text:");
String input = scanner.nextLine();
String[][] frequencies = findCharFrequencies(input);
System.out.println("Character | Frequency");
for (int i = 0; i < frequencies.length; i++) {
System.out.println(" " + frequencies[i][0] + " | " + frequencies[i][1]);
}
scanner.close();
}
}
Output:
Enter the text:
hello world
Character | Frequency
h | 1
e | 1
l | 3
o | 2
| 1
w | 1
r | 1
d | 1
5. Write a program to find the frequency of characters in a string using unique char-
acters and display the result.
import java.util.Scanner;
public class FrequencyUsingUniqueChars {
return result;
}
System.out.println("Character | Frequency");
for (int i = 0; i < frequencies.length; i++) {
System.out.println(" " + frequencies[i][0] + " | " + frequencies[i][1]);
}
scanner.close();
}
}
Output:
Enter the text:
Banana
Character | Frequency
b | 1
a | 3
n | 2
6. Write a program to find the frequency of characters in a string using nested loops and display
the result.
import java.util.Scanner;
int count = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] != '0') {
count++;
}
}
return result;
}
System.out.println("Character Frequencies:");
for (String entry : frequencies) {
System.out.println(entry);
}
scanner.close();
}
}
Output:
Enter the text:
Mississippi
Character Frequencies:
m=1
i=4
s=4
p=2
import java.util.Scanner;
scanner.close();
}
}
Output:
Enter the text:
Madam
Palindrome Check (Logic 1 - Iterative): true
Palindrome Check (Logic 2 - Recursive): true
Palindrome Check (Logic 3 - Char Array): true
8. Write a program to check if two texts are anagrams and display the result.
import java.util.Scanner;
return true;
}
9. Create a program to display a calendar for a given month and year. The program
should take the month and year as input from the user and display the calendar
for that month. E.g. for 07 2005 user input, the program should display the
calendar as shown below.
import java.util.Scanner;
displayCalendar(month, year);
scanner.close();
}
}
Output:
Enter month (1-12): 7
Enter year (e.g., 2025): 2005
July 2005
Sun Mon Tue Wed Thu Fri Sat
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
31
10. Write a program to create a deck of cards, initialize the deck, shuffle the deck, and
distribute the deck of n cards to x number of players. Finally, print the cards the
players have.
import java.util.Scanner;
public class CardGame {
// Initialize deck
public static String[] initializeDeck() {
String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"};
// Shuffle deck
public static void shuffleDeck(String[] deck) {
int n = deck.length;
for (int i = 0; i < n; i++) {
int randomCardNumber = i + (int)(Math.random() * (n - i));
String temp = deck[i];
deck[i] = deck[randomCardNumber];
deck[randomCardNumber] = temp;
}
}
// Distribute cards
public static String[][] distributeCards(String[] deck, int n, int players) {
if (n % players != 0) {
System.out.println("Cards cannot be evenly distributed among players.");
return null;
}
// Print cards
public static void printPlayersCards(String[][] playersCards) {
if (playersCards == null) return;
// Main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
if (n > deck.length) {
System.out.println("Not enough cards in the deck.");
} else {
String[][] playersCards = distributeCards(deck, n, players);
printPlayersCards(playersCards);
}
sc.close();
}
}
Output:
Enter number of cards to distribute: 20
Enter number of players: 4
Player 1:
3 of Spades
10 of Clubs
Ace of Hearts
5 of Diamonds
King of Spades
Player 2:
2 of Diamonds
7 of Clubs
6 of Spades
Jack of Hearts
3 of Hearts
...