NAME: akash rv
RA2211026010376
X2
Tutorial Questions
3) 3. Write a JAVA program to construct the following pattern, using a
nested for loop.
*
**
***
****
*****
****
***
**
*
Code:
public class NestedLoopPattern {
public static void main(String[] args) {
int rows = 5; // Number of rows in the upper half
of the pattern
// Upper half of the pattern
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
// Lower half of the pattern
for (int i = rows - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output:
*
**
***
****
*****
****
***
**
*
4) Write a JAVA program that accepts a word from the user and reverse
it. ( should not use any functions)
Code:
import java.util.Scanner;
public class ReverseWord {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word: ");
String input = scanner.nextLine();
String reversed = reverseWord(input);
System.out.println("Reversed word: " + reversed);
scanner.close();
public static String reverseWord(String word) {
char[] charArray = word.toCharArray();
int left = 0;
int right = charArray.length - 1;
while (left < right) {
// Swap characters at left and right indices
char temp = charArray[left];
charArray[left] = charArray[right];
charArray[right] = temp;
// Move indices towards each other
left++;
right--;
return new String(charArray);
OUTPUT:
Enter a word: Java
Reversed word: avaJ
5) . Write a JAVA program that accepts a string and calculate the
number of digits and letters.
Code:
import java.util.Scanner;
public class CountDigitsAndLetters {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
int letterCount = 0;
int digitCount = 0;
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (Character.isLetter(ch)) {
letterCount++;
} else if (Character.isDigit(ch)) {
digitCount++;
System.out.println("Letters " + letterCount);
System.out.println("Digits " + digitCount);
scanner.close();
}
OUTPUT
Enter a string: Ashwin got 99 marks
Letters 14 Digits 2
6) Write a JAVA program to check the validity of password input by
users.
Validation :
● At least 1 letter between [a-z] and 1 letter between [A-Z].
● At least 1 number between [0-9].
● At least 1 character from [$#@].
● Minimum length 6 characters.
● Maximum length 16 characters.
Sample output
Input your password srmist@2017
Not a Valid Password
Input your password Srmist@2022
Valid Password
Code:
import java.util.Scanner;
public class PasswordValidator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input your password: ");
String password = scanner.nextLine();
if (isValidPassword(password)) {
System.out.println("Valid Password");
} else {
System.out.println("Not a Valid Password");
scanner.close();
public static boolean isValidPassword(String password) {
if (password.length() < 6 || password.length() > 16) {
return false;
boolean hasLowerCase = false;
boolean hasUpperCase = false;
boolean hasDigit = false;
boolean hasSpecialChar = false;
for (char ch : password.toCharArray()) {
if (Character.isLowerCase(ch)) {
hasLowerCase = true;
} else if (Character.isUpperCase(ch)) {
hasUpperCase = true;
} else if (Character.isDigit(ch)) {
hasDigit = true;
} else if (ch == '$' || ch == '#' || ch == '@') {
hasSpecialChar = true;
if (hasLowerCase && hasUpperCase && hasDigit &&
hasSpecialChar) {
return true;
}
return false;
OUTPUT:
Input your password: Srmist@2032
Valid Password
7) Write a JAVA program to find numbers between 100 and 400 (both
included) where each digit of a number is an even number. The
numbers obtained should be printed in a comma-separated sequence.
Code:
public class EvenDigitNumbers {
public static void main(String[] args) {
System.out.println("Numbers with all even digits between
100 and 400:");
for (int num = 100; num <= 400; num++) {
if (hasAllEvenDigits(num)) {
System.out.print(num);
if (num < 400) {
System.out.print(", ");
}
}
}
}
public static boolean hasAllEvenDigits(int num) {
while (num > 0) {
int digit = num % 10;
if (digit % 2 != 0) {
return false;
}
num /= 10;
}
return true;
}
}
OUTPUT:
Numbers with all even digits between 100 and 400:
200, 202, 204, 206, 208, 220, 222, 224, 226, 228, 240, 242, 244, 246,
248, 260, 262, 264, 266, 268, 280, 282, 284, 286, 288, 400