-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
230 lines (198 loc) · 5.52 KB
/
Main.java
File metadata and controls
230 lines (198 loc) · 5.52 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import java.util.Stack;
public class Main {
public static void swap1(int a, int b) {
System.out.println("Before Swap: a="+a+" b="+b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("After Swap: a="+a+" b="+b);
}
public static void swap2(int a, int b) {
System.out.println("Before Swap: a="+a+" b="+b);
int temp = a;
a = b;
b = temp;
System.out.println("After Swap: a="+a+" b="+b);
}
public static void evenOdd(int n) {
if (n % 2 == 0) {
System.out.println("Even");
}
else {
System.out.println("Odd");
}
}
public static void leapYear(int y) {
if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) {
System.out.println("Leap Year");
}
else {
System.out.println("Not Leap Year");
}
}
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static boolean isPalindrome(int n) {
if (n < 0) {
return false;
}
int num = n;
int rev = 0;
while (num != 0) {
int rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
}
return n == rev;
}
public static boolean isArmstrong(int n) {
int l = String.valueOf(n).length();
int num = n;
int sum = 0;
while(num != 0) {
int rem = num % 10;
sum += Math.pow(rem, l);
num /= 10;
}
return n == sum;
}
public static boolean strong(int n) {
int num = n;
int sum = 0;
while(num != 0) {
int rem = num % 10;
sum += factorial(rem);
num /= 10;
}
return n == sum;
}
public static boolean perfect(int n) {
if (n <= 0) {
return false;
}
int sum = 0;
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum += i;
}
}
return n == sum;
}
public static boolean niven(int n) {
if (n <= 0) {
return false;
}
int num = n;
int sum = 0;
while (num != 0) {
int rem = num % 10;
sum += rem;
num /= 10;
}
return n % sum == 0;
}
public static void fibonacci(int n) {
int i = 1, a = -1, b = 1;
System.out.print("Fibonacci Series Upto "+n+" Terms: ");
while ( i <= n) {
int c = a + b;
System.out.print(c+" ");
a = b;
b = c;
i++;
}
}
public static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
int fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
public static String reverse(String s) {
Stack<Character> st = new Stack<>();
StringBuilder result = new StringBuilder();
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) != ' ') {
st.push(s.charAt(i));
}
else {
while(!st.isEmpty()) {
result.append(st.pop());
}
result.append(" ");
}
}
while(!st.isEmpty()) {
result.append(st.pop());
}
return result.toString();
}
public static void reverseArray(int[] arr) {
int start = 0;
int end = arr.length - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static void matrix(int[][] A, int[][] B) {
int r1 = A.length;
int c1 = A[0].length;
int r2 = B.length;
int c2 = B[0].length;
if (c1 != r2) {
throw new IllegalArgumentException("Incompatible Matrix Dimensions");
}
int[][] C = new int[r1][c2];
for(int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
for (int k = 0; k < c1; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
for(int i = 0; i < C.length; i++) {
for (int j = 0; j < C[0].length; j++) {
System.out.print(C[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
swap1(7, 10);
swap2(10, 7);
evenOdd(33);
leapYear(2025);
System.out.println(isPrime(21));
System.out.println(isPalindrome(343));
System.out.println(isArmstrong(153));
System.out.println(strong(145));
System.out.println(perfect(28));
System.out.println(niven(18));
fibonacci(10); System.out.println();
System.out.println(factorial(5));
System.out.println(reverse("Hello World"));
int[][] A = { {3, -2, 5}, {3, 0, 4} }; int[][] B = { {2, 3}, {-9, 0}, {0, 4} };
matrix(A, B);
int[] arr = {1, 2, 3, 4, 5}; reverseArray(arr);
}
}