-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicMath.java
More file actions
189 lines (164 loc) · 4.54 KB
/
BasicMath.java
File metadata and controls
189 lines (164 loc) · 4.54 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
//all Basic math problems
// 1)count digits in an integer
import java.util.*;
public class countdigit {
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number");
int n = sc.nextInt();
int count =0;
while(n>0){
n = n/10;
count = count+1;
}
System.out.println(count);
sc.close();
}
}
//2)reverse Num
class Solution {
public int reverse(int x) {
int revNum =0;
while(x!=0){
int lastdigit = x%10;
// check constarints conditon
if(revNum > Integer.MAX_VALUE/10 || revNum < Integer.MIN_VALUE/10){
return 0;
}
revNum = (revNum *10)+ lastdigit;//123=0*10+3=3 like that
x =x/10; // because it need to go like that 123/10 = 12 and 12/10 = 1
}
return revNum;
}
}
//3)check palindrome
class Solution {
public boolean isPalindrome(int x) {
int original = x;
int revNum =0;
// check neagative numbers cant beacome palindrome
if(x<0){
return false;
}
while(x>0){
int lastdigit = x%10;
// check constarints conditon
if(revNum > Integer.MAX_VALUE/10 || revNum < Integer.MIN_VALUE/10){
return false;
}
revNum = (revNum *10)+ lastdigit;//123=0*10+3=3 like that
x =x/10; // because it need to go like that 123/10 = 12 and 12/10 = 1
}
if(revNum == original){
return true;
}else{
return false;
}
}
}
//4) ArmstrongNum
import java.util.*;
public class ArmstrongNum {
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number ");
int n = sc.nextInt();
// armstrong -- like sum of powers -153 = 1+125+27 = 153
int temp = n; int sum = 0;
while(n>0){
int r = n%10;
sum = sum + r*r*r;
n = n/10;
}
if(sum == temp){
System.out.println("The given is Armstrong Number");
}else{
System.out.println("The given Number is not a Armstrong Number" );
}
sc.close();
}
}
//5) print all diviors
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number");
int n = sc.nextInt();
ArrayList<Integer> divisors = new ArrayList<>();
for(int i = 1; i <= n; i++){
if(n % i == 0){
divisors.add(i);
}
}
System.out.println(divisors);
}
}
//6)Prime Num check
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number:");
int n = sc.nextInt();
boolean isPrime = true;
if(n <= 1){
isPrime = false;
} else {
// We loop till √n because any non-prime number must have a factor between 2 and √n
for(int i = 2; i <= Math.sqrt(n); i++){
if(n % i == 0){
isPrime = false;
break;
}
}
}
if(isPrime){
System.out.println("Prime");
} else {
System.out.println("Not Prime");
}
}
}
// 7) GCD of a number
import java.util.*;
public class GCD {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers:");
int a = sc.nextInt();
int b = sc.nextInt();
int gcd = findGCD(a, b);
System.out.println("GCD of " + a + " and " + b + " is: " + gcd);
}
public static int findGCD(int a, int b) {
// Euclidean Algorithm
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}
// 8) LCM of a Number -->(a*b)/gcd = lcm
import java.util.*;
public class LCM {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers:");
int a = sc.nextInt();
int b = sc.nextInt();
int gcd = findGCD(a, b); // First find GCD
int lcm = (a * b) / gcd; // Then apply LCM formula
System.out.println("LCM of " + a + " and " + b + " is: " + lcm);
}
public static int findGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}