Tutorial Questions 1
Tutorial Questions 1
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();
}
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;
scanner.close();
int left = 0;
charArray[left] = charArray[right];
charArray[right] = temp;
left++;
right--;
OUTPUT:
Code:
import java.util.Scanner;
int digitCount = 0;
char ch = input.charAt(i);
if (Character.isLetter(ch)) {
letterCount++;
} else if (Character.isDigit(ch)) {
digitCount++;
scanner.close();
}
OUTPUT
Letters 14 Digits 2
Sample output
Input your password srmist@2017
Not a Valid Password
Input your password Srmist@2022
Valid Password
Code:
import java.util.Scanner;
if (isValidPassword(password)) {
System.out.println("Valid Password");
} else {
scanner.close();
return false;
if (Character.isLowerCase(ch)) {
hasLowerCase = true;
} else if (Character.isUpperCase(ch)) {
hasUpperCase = true;
} else if (Character.isDigit(ch)) {
hasDigit = true;
hasSpecialChar = true;
return true;
}
return false;
OUTPUT:
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(", ");
}
}
}
}
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