Skip to content

Commit 33d233c

Browse files
Conditional Statement
Duration 1 day 11/9/23
1 parent ef387be commit 33d233c

File tree

6 files changed

+205
-0
lines changed

6 files changed

+205
-0
lines changed

Conditional Statement/Assignment.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.Scanner;
2+
3+
public class Assignment {
4+
public static void main(String[] args) {
5+
Scanner sc = new Scanner(System.in);
6+
7+
// Get a number from the user and print whether it is positive or negative.
8+
System.out.print("Number : ");
9+
int num = sc.nextInt();
10+
System.out.println("+ve / -ve : "+((num>0)? "Positive" : "Negative"));
11+
12+
// You have a fever if your temperature is above 100 and otherwise prints You don't have a fever.
13+
double temp = 100.2;
14+
System.out.println((temp>100)? "Fever":"normalTemperature");
15+
16+
// Input week number(1-7) and print the day of week name
17+
System.out.print("Week number : ");
18+
int week = sc.nextInt();
19+
switch (week) {
20+
case 1 :
21+
System.out.println("Monday");
22+
break;
23+
case 2 :
24+
System.out.println("Tuesday");
25+
break;
26+
case 3 :
27+
System.out.println("Wednesday");
28+
break;
29+
case 4:
30+
System.out.println("Thusday");
31+
break;
32+
case 5:
33+
System.out.println("Friday");
34+
break;
35+
case 6:
36+
System.out.println("Saturday");
37+
break;
38+
case 7:
39+
System.out.println("Sunday");
40+
break;
41+
default:
42+
System.out.println("Didn't recognise");
43+
}
44+
45+
// Whether that year is leap year or not
46+
int year = 1999;
47+
if (year%4 == 0) {
48+
if (year%100 == 0) {
49+
if (year%400 == 0) {
50+
System.out.println("Leap Year");
51+
} else {
52+
System.out.println("Not Leap Year");
53+
}
54+
} else {
55+
System.out.println("Leap Year");
56+
}
57+
} else {
58+
System.out.println("Not a Leap Year");
59+
}
60+
}
61+
}

Conditional Statement/Calculator.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Scanner;
2+
3+
public class Calculator {
4+
public static void main(String[] args) {
5+
Scanner sc = new Scanner(System.in);
6+
System.out.print("Use Single Operator.\n-> ");
7+
String str = sc.next();
8+
9+
int i=0;
10+
for (i=0; i<str.length(); i++) {
11+
char ch = str.charAt(i);
12+
if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%') {
13+
int a = Integer.parseInt(str.substring(0, i));
14+
int b = Integer.parseInt(str.substring(i+1, str.length()));
15+
16+
switch (ch) {
17+
case '+' :
18+
System.out.println("Sum : "+(a+b));
19+
break;
20+
case '-' :
21+
System.out.println("Sub : "+(a-b));
22+
break;
23+
case '*' :
24+
System.out.println("Mul : "+(a*b));
25+
break;
26+
case '/' :
27+
float c = b;
28+
System.out.println("Div : "+(a/c));
29+
break;
30+
case '%' :
31+
System.out.println("Modulus : "+(a%b));
32+
break;
33+
default:
34+
System.out.println("Didn't find operator");
35+
}
36+
}
37+
}
38+
}
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.Scanner;
2+
3+
public class IncomeTaxCalculator {
4+
public static void main(String[] args) {
5+
Scanner sc = new Scanner(System.in);
6+
7+
System.out.print("Salary : ");
8+
int income = sc.nextInt(), tax = 0;
9+
10+
if (income < 500000) {
11+
tax = (income/100) * 0;
12+
} else if (income >= 500000 && income <= 1000000) {
13+
tax = (income/100) * 20;
14+
} else {
15+
tax = (income/100) * 30;
16+
}
17+
18+
System.out.println("Tax : "+tax);
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class LargestOf3Num {
2+
public static void main(String[] args) {
3+
4+
int a=12, b=12, c=11;
5+
6+
if (a >= b && a >= c) {
7+
System.out.println(a);
8+
} else if (b >= c && b>=a) {
9+
System.out.println(b);
10+
} else {
11+
System.out.println(c);
12+
}
13+
}
14+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
public class SwitchStatement {
2+
public static void main(String[] args) {
3+
/* Switch Cases*/
4+
// In switch case when one condition gets true then other condition also getting true
5+
// To prevent this we can use the break; keyword
6+
7+
// Integer
8+
int choice = 1;
9+
switch (choice) {
10+
case 1 :
11+
System.out.println("Samosa");
12+
break;
13+
case 2 :
14+
System.out.println("Burger");
15+
break;
16+
case 3 :
17+
System.out.println("Rabdi");
18+
break;
19+
default:
20+
System.out.println("Enter right choice");
21+
}
22+
23+
// Character
24+
char ch = 'b';
25+
switch (ch) {
26+
case 'a' :
27+
System.out.println("Samosa");
28+
break;
29+
case 'b' :
30+
System.out.println("Burger");
31+
break;
32+
case 'c' :
33+
System.out.println("Rabdi");
34+
break;
35+
default:
36+
System.out.println("Enter right choice");
37+
}
38+
39+
// String
40+
String str = "two";
41+
switch (str) {
42+
case "one" :
43+
System.out.println("Samosa");
44+
break;
45+
case "two" :
46+
System.out.println("Burger");
47+
break;
48+
case "three" :
49+
System.out.println("Rabdi");
50+
break;
51+
default:
52+
System.out.println("Enter right choice");
53+
}
54+
}
55+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class TernaryOperator {
2+
public static void main(String[] args) {
3+
/* Ternary Operator */
4+
// ((Condition)? true:false)
5+
// Also Known as "Short-hand if-else" or "Other type of if-else"
6+
7+
// If-else - One line of Code - Odd and Even
8+
System.out.println((4%2==0)? "Even" : "Odd");
9+
10+
// Else if by using ternary operator
11+
// Greater between three number
12+
int a=22, b=14, c=6;
13+
int result = (a>=b && a>=c)? a: (b>=a && b>=c)? b : c;
14+
System.out.println(result);
15+
}
16+
}

0 commit comments

Comments
 (0)