0% found this document useful (0 votes)
2 views8 pages

Java Programs 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Java Programs 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Date:- 06/08/25

Ques 1: - WAP in Java to find the greatest of three numbers using if-else.

public class LargestOfThree {


public static void main(String[] args) {
int a = 10, b = 20, c = 15;
if(a >= b && a >= c)
System.out.println(a + " is the largest");
else if(b >= a && b >= c)
System.out.println(b + " is the largest");
else
System.out.println(c + " is the largest");
}
}
Output:-
20 is the largest
Ques 2: - WAP in Java to check if a character is a vowel or consonant using
switch
public class VowelCheck {
public static void main(String[] args) {
char ch = 'e';
switch(ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
System.out.println(ch + " is a vowel");
break;
default:
System.out.println(ch + " is a consonant");
}
}
}
Output:-
e is a vowel
Ques 3: - WAP in Java to calculate factorial of a number using for loop

public class Factorial {


public static void main(String[] args) {
int num = 5;
long fact = 1;
for(int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial of " + num + " is " + fact);
}
}
Output:-
Factorial of 5 is 120
Ques 4: - WAP in Java to print multiplication table of a number using do-
while

public class MultiplicationTable {


public static void main(String[] args) {
int num = 4, i = 1;
do {
System.out.println(num + " x " + i + " = " + (num * i));
i++;
} while(i <= 10);
}
}
Output:-
4x1=4
4x2=8
...
4 x 10 = 40
Ques 5: - WAP in Java to calculate power of a number (base^exp)

public class PowerCalc {


public static void main(String[] args) {
int base = 2, exp = 4;
int result = 1;
for(int i = 1; i <= exp; i++) {
result *= base;
}
System.out.println(base + "^" + exp + " = " + result);
}
}
Output:-
2^4 = 16
Ques 6: - WAP in Java to check whether a number is a palindrome

public class PalindromeCheck {


public static void main(String[] args) {
int num = 121, rev = 0, temp = num;
while(temp > 0) {
int digit = temp % 10;
rev = rev * 10 + digit;
temp /= 10;
}
if(rev == num)
System.out.println(num + " is a Palindrome");
else
System.out.println(num + " is Not a Palindrome");
}
}
Output:-
121 is a Palindrome
Ques 7 : - WAP in Java to Count digits of a number

public class DigitCount {


public static void main(String[] args) {
int num = 8765, count = 0;
while(num > 0) {
num = num / 10;
count++;
}
System.out.println("Total digits = " + count);
}
}

Output:-
Total digits = 4
Ques 8:- WAP in Java to print stars triangle pattern using nested for loop

public class StarPattern {


public static void main(String[] args) {
int rows = 5;
for(int i = 1; i <= rows; i++) {
for(int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}

Output: -
*
**
***
****
*****

You might also like