Document 1
Document 1
if (vowelPosition != -1) {
// Word contains a vowel
String part1 =
word.substring(vowelPosition);
String part2 = word.substring(0,
vowelPosition);
System.out.println("Word contains a
vowel: " + part1 + part2 + "c");
}
scanner.close();
}
scanner.close();
}
public static int findFirstVowelPosition(String
word) {
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (ch == 'A' || ch == 'E' || ch == 'I' || ch
== 'O' || ch == 'U') {
return i;
}
}
return -1; // No vowel found
}
}
Steps Explained:
1.Import the Scanner class to accept user inpu
t.
2.Create a VowelOperations class with a main
method.
3.Instantiate the Scanner object to read input
.
4.Prompt the user to enter a word in uppercas
e and read the input.
5.Find the position of the first occurring vowel
using the findFirstVowelPosition method.
6.Check if the word begins with a vowel. If so,
concatenate the word with 'y' and output the
result.
7.Check if the word contains a vowel. If so, cre
ate a new word by concatenating the part fro
m the vowel to the end followed by the part f
rom the beginning to the position of the vowe
l and add 'c', then output the result.
8.Check if the word does not contain a vowel. I
f so, output the original word.
9.Close the Scanner object to free up resource
s.
Example Terminal Output:
shell
Copy
Enter a word in uppercase: APPLE
Word begins with a vowel: APPLEy
// Initialize counters
int anCount = 0;
int andCount = 0;
scanner.close();
}
// Initialize flag
boolean hasCapitalWord = false;
scanner.close();
}
}
Steps Explained:
1.Import the Scanner class to accept user inpu
t.
2.Create a CapitalLetterCheck class with a mai
n method.
3.Instantiate the Scanner object to read input
.
4.Prompt the user to enter a sentence and rea
d the input.
5.Split the sentence into words using the split
method with space as the delimiter.
6.Initialize a boolean flag hasCapitalWord to f
alse.
7.Iterate through the array of words:
Check if the first character of each word i
s a capital letter using Character.isUpper
Case method.
Set hasCapitalWord to true and break the
loop if a capital letter is found.
8.Output the result based on the value of hasC
apitalWord.
9.Close the Scanner object to free up resource
s.
Example Terminal Output:
shell
Copy
Enter a sentence: This is a Test sentence.
The sentence contains a word beginning with a
capital letter.
// Initialize counter
int vowelCount = 0;
scanner.close();
}
// Initialize variables
int originalNumber = number;
int result = 0;
int n = String.valueOf(number).length();
scanner.close();
}
}
Steps Explained:
1.Import the Scanner class to accept user inpu
t.
2.Create an ArmstrongNumberCheck class wit
h a main method.
3.Instantiate the Scanner object to read input
.
4.Prompt the user to enter a number and read
the input.
5.Initialize variables originalNumber to store t
he original number, result to store the sum of
digits raised to the power of the number of di
gits, and n to store the number of digits in th
e input number.
6.Use a while loop to process each digit of the
number:
Extract the last digit using the modulus
operator %.
Raise the digit to the power of n using M
ath.pow and add it to result.
Remove the last digit from the number u
sing integer division /.
7.Compare result with originalNumber. If they
are equal, the number is an Armstrong numb
er; otherwise, it is not.
8.Output the result.
9.Close the Scanner object to free up resource
s.
Example Terminal Output:
shell
Copy
Enter a number: 153
153 is an Armstrong number.
scanner.close();
}
}
Steps Explained:
1.Import the Scanner class to accept user inpu
t.
2.Create a PerfectNumberCheck class with a m
ain method.
3.Instantiate the Scanner object to read input
.
4.Prompt the user to enter a number and read
the input.
5.Initialize a variable sumOfDivisors to store t
he sum of all divisors of the input number.
6.Use a for loop to check for divisors from 1 to
half of the input number:
Check if the current number is a divisor o
f the input number using the modulus op
erator %.
Add the divisor to sumOfDivisors if it divi
des the input number.
7.Compare sumOfDivisors with the input num
ber. If they are equal, the number is a perfect
number; otherwise, it is not.
8.Output the result.
9.Close the Scanner object to free up resource
s.
Example Terminal Output:
shell
Copy
Enter a number: 28
28 is a perfect number.
Enter a number: 10
10 is not a perfect number.
write a program in java to input A word in
uppercase and check the position for First
occuring vowel and perform following operations
(1) Word that begins with a Vowel and is
concatinated with y (2) Word that contains a
vowel. In between should have first part from the
position of the vowel till the end Followed by the
part of string from the beginning till the position
of the vowel is Concatenated by c (3)
Copilot
Sure! Let's break down the task into steps and wr
ite a Java program to perform the required operat
ions. We'll input a word in uppercase, check the p
osition of the first occurring vowel, and perform t
he specified operations.
Algorithm:
1.Start
2.Accept input from the user.
3.Find the position of the first occurring vowel i
n the input word.
4.Perform the following operations:
If the word begins with a vowel, concaten
ate it with 'y'.
If the word contains a vowel, create a ne
w word by concatenating the part from th
e vowel to the end followed by the part fr
om the beginning to the position of the v
owel and add 'c'.
5.Output the results.
6.End
Java Program:
java
Copy
import java.util.Scanner;
if (vowelPosition != -1) {
// Word contains a vowel
String part1 =
word.substring(vowelPosition);
String part2 = word.substring(0,
vowelPosition);
System.out.println("Word contains a
vowel: " + part1 + part2 + "c");
}
scanner.close();
}
scanner.close();
}
public static int findFirstVowelPosition(String
word) {
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (ch == 'A' || ch == 'E' || ch == 'I' || ch
== 'O' || ch == 'U') {
return i;
}
}
return -1; // No vowel found
}
}
Steps Explained:
1.Import the Scanner class to accept user inpu
t.
2.Create a VowelOperations class with a main
method.
3.Instantiate the Scanner object to read input
.
4.Prompt the user to enter a word in uppercas
e and read the input.
5.Find the position of the first occurring vowel
using the findFirstVowelPosition method.
6.Check if the word begins with a vowel. If so,
concatenate the word with 'y' and output the
result.
7.Check if the word contains a vowel. If so, cre
ate a new word by concatenating the part fro
m the vowel to the end followed by the part f
rom the beginning to the position of the vowe
l and add 'c', then output the result.
8.Check if the word does not contain a vowel. I
f so, output the original word.
9.Close the Scanner object to free up resource
s.
Example Terminal Output:
shell
Copy
Enter a word in uppercase: APPLE
Word begins with a vowel: APPLEy
// Initialize counters
int anCount = 0;
int andCount = 0;
scanner.close();
}
// Initialize flag
boolean hasCapitalWord = false;
scanner.close();
}
}
Steps Explained:
1.Import the Scanner class to accept user inpu
t.
2.Create a CapitalLetterCheck class with a mai
n method.
3.Instantiate the Scanner object to read input
.
4.Prompt the user to enter a sentence and rea
d the input.
5.Split the sentence into words using the split
method with space as the delimiter.
6.Initialize a boolean flag hasCapitalWord to f
alse.
7.Iterate through the array of words:
Check if the first character of each word i
s a capital letter using Character.isUpper
Case method.
Set hasCapitalWord to true and break the
loop if a capital letter is found.
8.Output the result based on the value of hasC
apitalWord.
9.Close the Scanner object to free up resource
s.
Example Terminal Output:
shell
Copy
Enter a sentence: This is a Test sentence.
The sentence contains a word beginning with a
capital letter.
// Initialize counter
int vowelCount = 0;
scanner.close();
}
// Initialize variables
int originalNumber = number;
int result = 0;
int n = String.valueOf(number).length();
scanner.close();
}
}
Steps Explained:
1.Import the Scanner class to accept user inpu
t.
2.Create an ArmstrongNumberCheck class wit
h a main method.
3.Instantiate the Scanner object to read input
.
4.Prompt the user to enter a number and read
the input.
5.Initialize variables originalNumber to store t
he original number, result to store the sum of
digits raised to the power of the number of di
gits, and n to store the number of digits in th
e input number.
6.Use a while loop to process each digit of the
number:
Extract the last digit using the modulus
operator %.
Raise the digit to the power of n using M
ath.pow and add it to result.
Remove the last digit from the number u
sing integer division /.
7.Compare result with originalNumber. If they
are equal, the number is an Armstrong numb
er; otherwise, it is not.
8.Output the result.
9.Close the Scanner object to free up resource
s.
Example Terminal Output:
shell
Copy
Enter a number: 153
153 is an Armstrong number.
scanner.close();
}
}
Steps Explained:
1.Import the Scanner class to accept user inpu
t.
2.Create a PerfectNumberCheck class with a m
ain method.
3.Instantiate the Scanner object to read input
.
4.Prompt the user to enter a number and read
the input.
5.Initialize a variable sumOfDivisors to store t
he sum of all divisors of the input number.
6.Use a for loop to check for divisors from 1 to
half of the input number:
Check if the current number is a divisor o
f the input number using the modulus op
erator %.
Add the divisor to sumOfDivisors if it divi
des the input number.
7.Compare sumOfDivisors with the input num
ber. If they are equal, the number is a perfect
number; otherwise, it is not.
8.Output the result.
9.Close the Scanner object to free up resource
s.
Example Terminal Output:
shell
Copy
Enter a number: 28
28 is a perfect number.
Enter a number: 10
10 is not a perfect number.
write a program in java to input A word in
uppercase and check the position for First
occuring vowel and perform following operations
(1) Word that begins with a Vowel and is
concatinated with y (2) Word that contains a
vowel. In between should have first part from the
position of the vowel till the end Followed by the
part of string from the beginning till the position
of the vowel is Concatenated by c (3)
Copilot
Sure! Let's break down the task into steps and wr
ite a Java program to perform the required operat
ions. We'll input a word in uppercase, check the p
osition of the first occurring vowel, and perform t
he specified operations.
Algorithm:
1.Start
2.Accept input from the user.
3.Find the position of the first occurring vowel i
n the input word.
4.Perform the following operations:
If the word begins with a vowel, concaten
ate it with 'y'.
If the word contains a vowel, create a ne
w word by concatenating the part from th
e vowel to the end followed by the part fr
om the beginning to the position of the v
owel and add 'c'.
5.Output the results.
6.End
Java Program:
java
Copy
import java.util.Scanner;
if (vowelPosition != -1) {
// Word contains a vowel
String part1 =
word.substring(vowelPosition);
String part2 = word.substring(0,
vowelPosition);
System.out.println("Word contains a
vowel: " + part1 + part2 + "c");
}
scanner.close();
}
scanner.close();
}
public static int findFirstVowelPosition(String
word) {
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (ch == 'A' || ch == 'E' || ch == 'I' || ch
== 'O' || ch == 'U') {
return i;
}
}
return -1; // No vowel found
}
}
Steps Explained:
1.Import the Scanner class to accept user inpu
t.
2.Create a VowelOperations class with a main
method.
3.Instantiate the Scanner object to read input
.
4.Prompt the user to enter a word in uppercas
e and read the input.
5.Find the position of the first occurring vowel
using the findFirstVowelPosition method.
6.Check if the word begins with a vowel. If so,
concatenate the word with 'y' and output the
result.
7.Check if the word contains a vowel. If so, cre
ate a new word by concatenating the part fro
m the vowel to the end followed by the part f
rom the beginning to the position of the vowe
l and add 'c', then output the result.
8.Check if the word does not contain a vowel. I
f so, output the original word.
9.Close the Scanner object to free up resource
s.
Example Terminal Output:
shell
Copy
Enter a word in uppercase: APPLE
Word begins with a vowel: APPLEy
// Initialize counters
int anCount = 0;
int andCount = 0;
scanner.close();
}
// Initialize flag
boolean hasCapitalWord = false;
scanner.close();
}
}
Steps Explained:
1.Import the Scanner class to accept user inpu
t.
2.Create a CapitalLetterCheck class with a mai
n method.
3.Instantiate the Scanner object to read input
.
4.Prompt the user to enter a sentence and rea
d the input.
5.Split the sentence into words using the split
method with space as the delimiter.
6.Initialize a boolean flag hasCapitalWord to f
alse.
7.Iterate through the array of words:
Check if the first character of each word i
s a capital letter using Character.isUpper
Case method.
Set hasCapitalWord to true and break the
loop if a capital letter is found.
8.Output the result based on the value of hasC
apitalWord.
9.Close the Scanner object to free up resource
s.
Example Terminal Output:
shell
Copy
Enter a sentence: This is a Test sentence.
The sentence contains a word beginning with a
capital letter.
// Initialize counter
int vowelCount = 0;
scanner.close();
}
// Initialize variables
int originalNumber = number;
int result = 0;
int n = String.valueOf(number).length();
scanner.close();
}
}
Steps Explained:
1.Import the Scanner class to accept user inpu
t.
2.Create an ArmstrongNumberCheck class wit
h a main method.
3.Instantiate the Scanner object to read input
.
4.Prompt the user to enter a number and read
the input.
5.Initialize variables originalNumber to store t
he original number, result to store the sum of
digits raised to the power of the number of di
gits, and n to store the number of digits in th
e input number.
6.Use a while loop to process each digit of the
number:
Extract the last digit using the modulus
operator %.
Raise the digit to the power of n using M
ath.pow and add it to result.
Remove the last digit from the number u
sing integer division /.
7.Compare result with originalNumber. If they
are equal, the number is an Armstrong numb
er; otherwise, it is not.
8.Output the result.
9.Close the Scanner object to free up resource
s.
Example Terminal Output:
shell
Copy
Enter a number: 153
153 is an Armstrong number.