0% found this document useful (0 votes)
3 views15 pages

Java Work

The document contains Java programming exercises focused on conditional statements, switch cases, and ternary operators. It includes various examples such as checking if a number is positive, finding the largest of two or three numbers, determining if a year is a leap year, and using switch statements for days of the week and months. Additionally, it demonstrates the use of ternary operators for grading and other conditions.

Uploaded by

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

Java Work

The document contains Java programming exercises focused on conditional statements, switch cases, and ternary operators. It includes various examples such as checking if a number is positive, finding the largest of two or three numbers, determining if a year is a leap year, and using switch statements for days of the week and months. Additionally, it demonstrates the use of ternary operators for grading and other conditions.

Uploaded by

Giri Vennapusa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA PROGRAMMING PRACTICE – CONDITIONAL STATEMENTS, SWITCH CASES, AND TERNARY

OPERATORS
Date: 19/05/2026

// 1. Check if a number is positive, negative, or zero.

package [Link];

public class PositiveOrNegative {

public static void main (String args[]) {

int a = -60;

if(a>0) {

[Link](a + " is positive Number");

} else if(a<0) {

[Link](a + " is Negative Number");

}else {

[Link](a + " is Zero");

String result = (a>0)?"positive":(a<0)?"Negative":"Zero";

[Link](result);

//2. Find the largest of two numbers.

package [Link]

public class LargestOfTwoNumbers {

public static void main(String[] args) {

int a =10;

int b =20;

if(a>b) {

[Link]("A is largest number");

} else {

[Link]("B is a Largest Number");


}

String result=(a>b)?"A is largest Number":"B is largest Number";

[Link](result);

//3. Find the largest of three numbers.


package [Link];

public class LargestOfThreeNumbers {

public static void main(String[] args) {

int a =10;

int b =20;

int c = 30;

if(a>b && a>c) {

[Link]("A is largest number");

} else if( b>a && b>c) {

[Link]("B is a Largest Number");

} else {

[Link]("C is a Largest Number");

}} }

//4. Check if a number is even or odd.


package [Link];

public class EvenOrOdd {

public static void main(String[] args) {

int a = 785;

if(a % 2 ==0) {

[Link](a + " Is a Even Number");

} else {

[Link](a + " Is a Odd Number");

}
//5. Check if a number is divisible by 5 and 11.
package [Link];

public class DivisibleBy {

public static void main(String[] args) {

int a = 56;

if(a%5==0 && a%11==0) {

[Link]("Divisible");

}else {

[Link]("Not Divisible");

//6. Check if a year is a leap year.


package [Link];

public class LeapYear {

public static void main(String args[]) {

int year = 1900;

if (year % 400 == 0) {

[Link](year + " IS LEAP YEAR");

} else if (year % 100 == 0) {

[Link](year + " IS NOT LEAP YEAR");

} else if (year % 4 == 0) {

[Link](year + " IS LEAP YEAR");

} else {

[Link](year + " IS NOT LEAP YEAR");

}
}

//7. Check if a character is a vowel or consonant.


package [Link];

public class VowelOrNot {

public static void main(String args[]) {

char a = 'I';

if(a== 'A' || a== 'E' ||a== 'I' ||a== 'O'|| a== 'U' ) {

[Link]("Vowel");

}else {

[Link]("Consonant");

}
//8. Check if a character is uppercase, lowercase, digit, or special symbol.
package [Link];

public class CheckTheCharacter {

public static void main (String args[]) {

int a = '$';

if(a>='A' && a<='Z') {

[Link]("Uppercase");

} else if( a>= 'a' && a<= 'z') {

[Link]("Lowercase");

} else if(a>=0 && a<=9) {

[Link]("Digit");

} else {

[Link]("Special Symbols");

}
//9. Check if a person is eligible to vote (age ≥ 18).
package [Link];

public class EligibleToVote {

public static void main(String[] args) {

String name = "Giri";

int age = 21;

if(age>=18) {

[Link](name + " is Eligible for Vote");

}else {

[Link](name + " is Not Eligible for Vote becase the age is " + age );

//11. Check if a number is a multiple of 3 and 7.


package [Link];

public class Checkifanumberisamultipleof3and7 {

public static void main(String args[]) {

int num = 21;

if (num % 3 == 0 && num % 7 == 0) {

[Link](num + " is a multiple of 3 and 7");

} else {

[Link](num + " is not a multiple of 3 and 7");

}
//12. Check if three sides can form a triangle.
package [Link];

public class ThreeSideTriangle {

public static void main (String args[]) {

int a = 2;

int b=3;

int c=10;

if(a+b>c) {

[Link]("Valid Triangle");

}else {

[Link]("Not a Valid Triangle");

//13. Check if a number is positive and even.


package [Link];

public class PositiveAndEven {

public static void main (String args[]) {

int num =2;

if(num>0 && num%2==0) {

[Link](num + " is a Postive and Even Number");

}else {

[Link](num + " is Not a Postive and Even Number");

}
//14. Print grade based on marks (A, B, C, D, F).
package [Link];

public class Grade {

public static void main(String[] args) {

int marks = 30;

if(marks >=90) {

[Link]("A");

} else if(marks >=80 && marks<90) {

[Link]("B");

}else if(marks>=70 && marks<80) {

[Link]("C");

}else if(marks>=60 && marks<70) {

[Link]("D");

}else if(marks>=50 && marks<60) {

[Link]("E");

}else {

[Link]("F");

}
Switch Case Practice
//16. Print day of week using number (1–7).
package one;

public class Weeks {

public static void main(String[] args) {

int n = 10;

switch(n) {

case 1:[Link]("Sunday");break;

case 2:[Link]("Monday");break;

case 3:[Link]("Tuesday");break;

case 4:[Link]("Wednesday");break;

case 5:[Link]("Thursday");break;

case 6:[Link]("Friday");break;

case 7:[Link]("Saturday");break;

default:[Link]("Invalid Input");break;

//17. Print month name using number (1–12).


package one;

public class Month {

public static void main(String[] args) {

int n = 10;

switch(n) {

case 1:[Link]("January");break;

case 2:[Link]("February");break;

case 3:[Link]("March");break;
case 4:[Link]("April");break;

case 5:[Link]("May");break;

case 6:[Link]("June");break;

case 7:[Link]("July");break;

case 8:[Link]("August");break;

case 9:[Link]("September");break;

case 10:[Link]("October");break;

case 11:[Link]("November");break;

case 12:[Link]("December");break;

default:[Link]("Invalid Input"); break;

}
//18. Simple calculator using switch (add, sub, mul, div).
package one;

public class Calculator {

public static void main (String args[]) {

int a =10;

int b =20;

char op = '+';

switch(op) {

case '+':

[Link](a+b);

break;

case '-':

[Link](a-b);

break;

case '*':

[Link](a*b);

break;

case '/':
[Link](a/b);

break;

default:

[Link]("invalid Operator");

break;

19. Check vowel/consonant using switch.


package [Link];

public class Practice {

public static void main(String args[]) {

char ch = 'a';

switch (ch) {

case 'a':

case 'e':

case 'i':

case 'o':

case 'u':

case 'A':

case 'E':

case 'I':

case 'O':

case 'U':

[Link]("Vowel");

break;

default:

[Link]("Consonant");

}
}

//20. Print "Weekday" or "Weekend" using switch.


package [Link];

public class Practice {

public static void main(String args[]) {

int day = 6;

switch (day) {

case 1:

case 2:

case 3:

case 4:

case 5:

[Link]("Weekday");

break;

case 6:

case 7:

[Link]("Weekend");

break;

default:

[Link]("Invalid Day");

26. Find the largest of two numbers using ternary


package [Link];

public class Practice {

public static void main(String args[]) {

int a = 10, b = 20;

int largest = (a > b) ? a : b;


[Link]("Largest = " + largest);

27. Find the largest of three numbers using nested ternary


package [Link];

public class Practice {

public static void main(String args[]) {

int a = 10, b = 25, c = 15;

int largest = (a > b)

? ((a > c) ? a : c)

: ((b > c) ? b : c);

[Link]("Largest = " + largest);

28. Check if number is even or odd using ternary


package [Link];

public class Practice {

public static void main(String args[]) {

int num = 8;

String result = (num % 2 == 0) ? "Even" : "Odd";

[Link](result);

29. Check if a year is leap or not using ternary


package [Link];

public class Practice {

public static void main(String args[]) {

int year = 2024;

String result = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

? "Leap Year"

: "Not a Leap Year";


[Link](result);

30. Assign grade using ternary based on marks


package [Link];

public class Practice {

public static void main(String args[]) {

int marks = 85;

String grade = (marks >= 90) ? "A"

: (marks >= 75) ? "B"

: (marks >= 50) ? "C"

: "Fail";

[Link]("Grade = " + grade);

31. Check if number is positive, negative, or zero using nested ternary


package [Link];

public class Practice {

public static void main(String args[]) {

int num = -5;

String result = (num > 0) ? "Positive"

: (num < 0) ? "Negative"

: "Zero";

[Link](result);

32. Check if character is vowel using ternary


package [Link];

public class Practice {

public static void main(String args[]) {

char ch = 'a';
String result = (ch == 'a' || ch == 'e' || ch == 'i' ||

ch == 'o' || ch == 'u' ||

ch == 'A' || ch == 'E' || ch == 'I' ||

ch == 'O' || ch == 'U')

? "Vowel"

: "Consonant";

[Link](result);

33. Compare two floats using ternary


package [Link];

public class Practice {

public static void main(String args[]) {

float a = 12.5f, b = 9.8f;

String result = (a > b) ? "a is Greater" : "b is Greater";

[Link](result);

34. Check if ASCII code of char is even or odd


package [Link];

public class Practice {

public static void main(String args[]) {

char ch = 'A';

if ((int) ch % 2 == 0) {

[Link]("ASCII code is Even");

} else {

[Link]("ASCII code is Odd");

35. Print “Pass” or “Fail” based on marks using ternary


package [Link];

public class Practice {

public static void main(String args[]) {

int marks = 40;

String result = (marks >= 35) ? "Pass" : "Fail";

[Link](result);

You might also like