INSTRUCTIOKS ARE GIVEN BELOW.
KINDLY WRITE A PROGRAM THAT WOULD:
1. Allow user to enter the size and element of an integer array. Display the sum and
average of all the elements.
2. Allow user to enter the size and element of a string array. Then ask the user to
give a string and display whether that string is present in array or not. (Letter case
doesn’t matter)
3. Allow user to enter the size and element of an integer array. Display all evenindexed elements (assume 0 is even)
4. Allow user to enter the size and element of a string array. Display the longest
string found in the array.
5. Allow user to enter the size and element of a string array. Display the shortest
string found in the array.
6. Allow user to enter the size and element of an integer array. Display all oddindexed elements (assume 0 is even) in reverse order.
7. Allow user to enter the size and element of an integer array. Display the sum of
all even-indexed elements and sum of all odd of all even-indexed elements
(assume 0 is even)
8. Allow user to enter the size and element of character array. Display all vowels
followed by all consonants
9. Allow user to enter the size and element of character array. Then ask the user to
give another character. Check if that character is present in the array, if yes,
replace it with symbol “@”.
10. Allow user to enter the size and element of a string array. Display all strings that
starts with a consonant.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//1
int[] sumAvg;
System.out.println("1. Enter the size of array:");
sumAvg = new int[Integer.parseInt(in.nextLine())];
System.out.println("Enter elements:");
int total = 0;
for (int i = 0; i < sumAvg.length; i++) {
sumAvg[i] = Integer.parseInt(in.nextLine());
total += sumAvg[i];
}
System.out.println("The sum is " + total + ", average of all the elements " + total / sumAvg.length);
//2
String[] sArray;
System.out.println("2. Enter the size of array:");
sArray = new String[Integer.parseInt(in.nextLine())];
System.out.println("Enter elements:");
for (int i = 0; i < sArray.length; i++) {
sArray[i] = in.nextLine();
}
System.out.println("Enter a string: ");
String toFind = in.nextLine();
boolean isPresent = false;
for (int i = 0; i < sArray.length; i++) {
if (sArray[i].equalsIgnoreCase(toFind)) {
isPresent = true;
break;
}
}
System.out.println(isPresent ? "Present" : "Not present");
//3
int[] evenArray;
System.out.println("3. Enter the size of array:");
evenArray = new int[Integer.parseInt(in.nextLine())];
System.out.println("Enter elements:");
for (int i = 0; i < evenArray.length; i++) {
evenArray[i] = Integer.parseInt(in.nextLine());
if (i % 2 == 0) {
System.out.print(evenArray[i] + " ");
}
}
System.out.println();
//4
String[] longArray;
System.out.println("4. Enter the size of array:");
longArray = new String[Integer.parseInt(in.nextLine())];
System.out.println("Enter elements:");
for (int i = 0; i < longArray.length; i++) {
longArray[i] = in.nextLine();
}
int maxLength = longArray[0].length();
int ind = 0;
for (int i = 1; i < longArray.length; i++) {
if (longArray[i].length() > maxLength) {
maxLength = longArray[i].length();
ind = i;
}
}
System.out.println("The longest string is " + longArray[ind]);
//5
String[] shortArray;
System.out.println("5. Enter the size of array:");
shortArray = new String[Integer.parseInt(in.nextLine())];
System.out.println("Enter elements:");
for (int i = 0; i < shortArray.length; i++) {
shortArray[i] = in.nextLine();
}
int minLength = shortArray[0].length();
int indS = 0;
for (int i = 1; i < shortArray.length; i++) {
if (shortArray[i].length() < minLength) {
minLength = shortArray[i].length();
indS = i;
}
}
System.out.println("The shortest string is " + shortArray[indS]);
//6
int[] oddArray;
System.out.println("6. Enter the size of array:");
oddArray = new int[Integer.parseInt(in.nextLine())];
System.out.println("Enter elements:");
for (int i = 0; i < oddArray.length; i++) {
oddArray[i] = Integer.parseInt(in.nextLine());
}
for (int i = oddArray.length - 1; i >= 0; i--) {
if (i % 2 != 0) {
System.out.print(oddArray[i] + " ");
}
}
System.out.println();
//7
int[] sumEOArray;
System.out.println("7. Enter the size of array:");
sumEOArray = new int[Integer.parseInt(in.nextLine())];
System.out.println("Enter elements:");
int totalE = 0;
int totalO = 0;
for (int i = 0; i < sumEOArray.length; i++) {
sumEOArray[i] = Integer.parseInt(in.nextLine());
if (i % 2 == 0) {
totalE += sumEOArray[i];
} else {
totalO += sumEOArray[i];
}
}
System.out.println("The evenindexed sum is" + totalE + ", the oddindexed sum is " + totalO);
//8
char[] charArray;
char[] vowels = {'a', 'e', 'i', 'o', 'u', 'y'};
System.out.println("8. Enter the size of array:");
charArray = new char[Integer.parseInt(in.nextLine())];
System.out.println("Enter elements:");
for (int i = 0; i < charArray.length; i++) {
charArray[i] = in.nextLine().charAt(0);
}
for (int i = 0; i < charArray.length; i++) {
for (int j = 0; j < vowels.length; j++) {
if (charArray[i] == vowels[j]) {
System.out.print(charArray[i] + " ");
break;
}
}
}
boolean isVowel;
for (int i = 0; i < charArray.length; i++) {
isVowel = false;
for (int j = 0; j < vowels.length; j++) {
if (charArray[i] == vowels[j]) {
isVowel = true;
break;
}
}
if (!isVowel) {
System.out.print(charArray[i] + " ");
}
}
System.out.println();
//9
char[] aArray;
System.out.println("9. Enter the size of array:");
aArray = new char[Integer.parseInt(in.nextLine())];
System.out.println("Enter elements:");
for (int i = 0; i < aArray.length; i++) {
aArray[i] = in.nextLine().charAt(0);
}
System.out.println("Enter a character: ");
char toReplace = in.nextLine().charAt(0);
for (int i = 0; i < aArray.length; i++) {
if (aArray[i] == toReplace) {
aArray[i] = '@';
}
}
//10
String[] cArray;
char[] vowelsC = {'a', 'e', 'i', 'o', 'u', 'y'};
System.out.println("10. Enter the size of array:");
cArray = new String[Integer.parseInt(in.nextLine())];
System.out.println("Enter elements:");
boolean isVowelC;
for (int i = 0; i < cArray.length; i++) {
cArray[i] = in.nextLine();
isVowelC = false;
for (int j = 0; j < vowels.length; j++) {
if (cArray[i].charAt(0) == vowelsC[j]) {
isVowelC = true;
break;
}
}
if(!isVowelC){
System.out.println(cArray[i]);
}
}
}
}
Comments
Leave a comment