Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7fb72fe

Browse files
Add GCD and LCM calculation using Euclid's algorithm; implement reverse number functionality
1 parent d4a6efd commit 7fb72fe

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

‎Basic/Digits/count_digits.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static int coutDigits(int n) {
2525
}
2626

2727
static int lastDigit(int num) {
28-
return num % 10;
28+
return num % 10;
2929
}
3030

3131
static int sumOfDigit(int num) {

‎Basic/gcd.java‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
import java.util.Scanner;
3+
4+
// euclid algorithm for find gcd
5+
6+
public class gcd {
7+
8+
public static void main(String[] args) {
9+
Scanner sc = new Scanner(System.in);
10+
System.out.print("Enter first Numbers: ");
11+
int a = sc.nextInt();
12+
System.out.print("Enter second number: ");
13+
int b = sc.nextInt();
14+
int gcd = find_gcd(a, b);
15+
System.out.println(a + " & " + b + " gcd is : " + gcd);
16+
17+
18+
int lcm = find_lcm(a, b);
19+
System.out.println("The LCM is : " +lcm);
20+
21+
sc.close();
22+
}
23+
24+
static int find_gcd(int a, int b) {
25+
while (a > 0 && b > 0) {
26+
if (a > b) {
27+
a = a % b;
28+
} else {
29+
b = b % a;
30+
}
31+
}
32+
if (a == 0)
33+
return b;
34+
return a;
35+
}
36+
37+
static int find_lcm(int a, int b) {
38+
return (a * b) / find_gcd(a, b);
39+
}
40+
}

‎Basic/reverse.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
import java.util.Scanner;
3+
4+
public class reverse {
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
System.out.print("Enter the number: ");
8+
int num = sc.nextInt();
9+
10+
int reverse = find_reverse(num);
11+
System.out.print("Before reverse: " + num);
12+
System.out.println(" ");
13+
System.out.println("After reverse: " +reverse);
14+
}
15+
16+
static int find_reverse(int num) {
17+
int reverse = 0;
18+
while (num != 0) {
19+
int digits = num % 10;
20+
reverse = reverse * 10 + digits;
21+
num = num / 10;
22+
}
23+
return reverse;
24+
}
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /