-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava
More file actions
212 lines (166 loc) · 5.69 KB
/
Copy pathjava
File metadata and controls
212 lines (166 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
1. Add two numbers, store in a third variable, and print it
public class AddNumbers {
public static void main(String[] args) {
int num1 = 10; // first number
int num2 = 20; // second number
int sum = num1 + num2; // store result in third variable
System.out.println("The sum is: " + sum);
}
}
2. Check if a number is even or odd
public class EvenOddCheck {
public static void main(String[] args) {
int num = 15; // you can change this number
if (num % 2 == 0) {
System.out.println(num + " is Even");
} else {
System.out.println(num + " is Odd");
}
}
}
3. Find the largest of two numbers using if-else
import java.util.Scanner;
public class LargestNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Taking input
System.out.print("Enter the first number: ");
int num1 = sc.nextInt();
System.out.print("Enter the second number: ");
int num2 = sc.nextInt();
// Using if-else
if (num1 > num2) {
System.out.println("The largest number is: " + num1);
} else if (num2 > num1) {
System.out.println("The largest number is: " + num2);
} else {
System.out.println("Both numbers are equal.");
}
sc.close();
}
}
4. . Check if a person is eligible to vote
import java.util.Scanner;
public class VotingEligibility {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Taking input
System.out.print("Enter your age: ");
int age = sc.nextInt();
// Checking eligibility
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
}
sc.close();
}
}
5. ATM Withdrawal System
import java.util.Scanner;
public class ATMWithdrawal {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int balance = 10000; // example balance
System.out.print("Enter withdrawal amount: ");
int withdraw = sc.nextInt();
if (balance >= withdraw) {
System.out.println("Transaction Successful");
} else {
System.out.println("Insufficient Balance");
}
sc.close();
}
}
6.Online Shopping Discount
import java.util.Scanner;
public class ShoppingDiscount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter total purchase amount: ");
double amount = sc.nextDouble();
double discount = 0;
if (amount >= 5000) {
discount = amount * 0.20;
} else if (amount >= 2000) {
discount = amount * 0.10;
}
double finalAmount = amount - discount;
System.out.println("Discount Applied: " + discount);
System.out.println("Final Amount to Pay: " + finalAmount);
sc.close();
}
}
7.Traffic Signal System
import java.util.Scanner;
public class TrafficSignal {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Signal Color (Red/Yellow/Green): ");
String color = sc.next().toLowerCase(); // convert to lowercase for consistency
if (color.equals("red")) {
System.out.println("Stop");
} else if (color.equals("yellow")) {
System.out.println("Get Ready");
} else if (color.equals("green")) {
System.out.println("Go");
} else {
System.out.println("Invalid Color");
}
sc.close();
}
}
8.Given two integers M and N, calculate the product of all integers from M and N inclusive .if M is greater than N , return 1 or display an appropriate message
import java.util.Scanner;
public class ProductOfRange {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input values
System.out.print("Enter M: ");
int M = sc.nextInt();
System.out.print("Enter N: ");
int N = sc.nextInt();
// Check if M > N
if (M > N) {
System.out.println("1 (Invalid Range: M is greater than N)");
} else {
long product = 1; // use long to avoid overflow for large numbers
for (int i = M; i <= N; i++) {
product *= i;
}
System.out.println("Product = " + product);
}
sc.close();
}
}
9.Given two integers M and N, calculate the product of all odd integers from M and N inclusive.if M is greater than N , return 1 or display an appropriate message
import java.util.Scanner;
public class ProductOfOddRange {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input values
System.out.print("Enter M: ");
int M = sc.nextInt();
System.out.print("Enter N: ");
int N = sc.nextInt();
// Check if M > N
if (M > N) {
System.out.println("1 (Invalid Range: M is greater than N)");
} else {
long product = 1;
boolean foundOdd = false;
for (int i = M; i <= N; i++) {
if (i % 2 != 0) { // check if odd
product *= i;
foundOdd = true;
}
}
if (foundOdd) {
System.out.println("Product of odd numbers = " + product);
} else {
System.out.println("No odd numbers in the given range.");
}
}
sc.close();
}
}