[Go to site: main page, start]

0% found this document useful (0 votes)
8 views3 pages

Java Programs

The document contains multiple Java programs demonstrating the use of conditional statements such as if, if-else, if-else-if ladder, and switch statements. Each program serves a specific purpose, including checking if a number is positive, determining even or odd numbers, finding the greater of two numbers, displaying grades based on marks, and implementing a simple calculator. Additionally, it includes a program to identify vowels from a character input.

Uploaded by

writetoabhiroop
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)
8 views3 pages

Java Programs

The document contains multiple Java programs demonstrating the use of conditional statements such as if, if-else, if-else-if ladder, and switch statements. Each program serves a specific purpose, including checking if a number is positive, determining even or odd numbers, finding the greater of two numbers, displaying grades based on marks, and implementing a simple calculator. Additionally, it includes a program to identify vowels from a character input.

Uploaded by

writetoabhiroop
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

1.

if statement
Program 1: Check if a number is positive
class PositiveCheck {
public static void main(String args[]) {
int n = 10;
if (n > 0) {
[Link]("The number is positive");
}
}
}

2. if–else statement
Program 2: Check even or odd
class EvenOdd {
public static void main(String args[]) {
int n = 7;
if (n % 2 == 0) {
[Link]("Even number");
} else {
[Link]("Odd number");
}
}
}

Program 3: Find greater of two numbers


class GreaterNumber {
public static void main(String args[]) {
int a = 15;
int b = 25;

if (a > b) {
[Link](a + " is greater");
} else {
[Link](b + " is greater");
}
}
}

3. if–else–if ladder
Program 4: Display grade based on marks
class Grade {
public static void main(String args[]) {
int marks = 82;
if (marks >= 90) {
[Link]("Grade A");
} else if (marks >= 75 && marks <90) {
[Link]("Grade B");
} else if (marks >= 50 && marks <75) {
[Link]("Grade C");
} else {
[Link]("Fail");
}
}
}

4. switch statement
Program 5: Display day of the week
class DayName {
public static void main(String args[]) {
int day = 4;

switch (day) {
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
case 3: [Link]("Wednesday"); break;
case 4: [Link]("Thursday"); break;
case 5: [Link]("Friday"); break;
case 6: [Link]("Saturday"); break;
case 7: [Link]("Sunday"); break;
default: [Link]("Invalid day");
}
}
}

Program 6: Simple calculator using switch (Cases like 1:Add, 2:Subtract, 3:Multiply, 4:Divide)
class Calculator {
public static void main(String args[]) {
int a = 20;
int b = 10;
int choice = 3;
switch (choice) {
case 1: [Link]("Sum = " + (a + b)); break;
case 2: [Link]("Difference = " + (a - b)); break;
case 3: [Link]("Product = " + (a * b)); break;
case 4: [Link]("Quotient = " + (a / b)); break;
default: [Link]("Invalid choice");
}
}
}
Program 7: Display the vowel

class WhichVowel {
public static void main(String args[]) {
char ch = 'o'; // compile-time input (lowercase only)

switch (ch) {
case 'a':
[Link]("It is vowel a");
break;
case 'e':
[Link]("It is vowel e");
break;
case 'i':
[Link]("It is vowel i");
break;
case 'o':
[Link]("It is vowel o");
break;
case 'u':
[Link]("It is vowel u");
break;
default:
[Link]("It is not a vowel");
}
}
}

You might also like