commit 75ac8e30cd668ea8266a72061c249d3e0b42e9c0 Author: Evan Ferrao Date: Tue Aug 6 23:07:11 2024 +0530 day1: add basic dsa code diff --git a/Basic/Armstrong.java b/Basic/Armstrong.java new file mode 100644 index 0000000..3e6d1f5 --- /dev/null +++ b/Basic/Armstrong.java @@ -0,0 +1,25 @@ +// write code to check armstrong number + +import java.util.Scanner; + +class Armstrong{ + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("Enter a number to check if it is an Armstrong number or not"); + int num = scanner.nextInt(); + scanner.close(); + int temp = num; + int sum = 0; + while(num>0){ + int rem = num%10; + sum += rem*rem*rem; + num = num/10; + } + if(sum == temp){ + System.out.println("The number is an Armstrong number"); + } + else{ + System.out.println("The number is not an Armstrong number"); + } + } +} \ No newline at end of file diff --git a/Basic/CheckPallindrome.java b/Basic/CheckPallindrome.java new file mode 100644 index 0000000..0516810 --- /dev/null +++ b/Basic/CheckPallindrome.java @@ -0,0 +1,27 @@ +// write a code in java to check palindrome number + +import java.util.Scanner; +class CheckPalindrome{ + public static void main(String args[]){ + int n, sum = 0, r, temp; + Scanner scanner = new Scanner(System.in); + System.out.print("Enter a number:"); + n = scanner.nextInt(); + scanner.close(); + temp = n; + while(n > 0) + { + r = n % 10; + sum = (sum*10) + r; + n = n/10; + } + if(temp == sum) + { + System.out.println("Given number is Palindrome"); + } + else + { + System.out.println("Given number is not Palindrome"); + } + } +} \ No newline at end of file diff --git a/Basic/CheckPrime.java b/Basic/CheckPrime.java new file mode 100644 index 0000000..5cb29f2 --- /dev/null +++ b/Basic/CheckPrime.java @@ -0,0 +1,29 @@ +// write a code in java to check for prime numbers in o(n^1/2) time complexity using function + +import java.util.Scanner; + +class CheckPrime { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("Enter a number to check if it is prime or not: "); + int num = scanner.nextInt(); + scanner.close(); + if (isPrime(num)) { + System.out.println(num + " is a prime number"); + } else { + System.out.println(num + " is not a prime number"); + } + } + + static boolean isPrime(int num) { + if (num <= 1) { + return false; + } + for (int i = 2; i * i <= num; i++) { + if (num % i == 0) { + return false; + } + } + return true; + } +} \ No newline at end of file diff --git a/Basic/PrintAllDivisors.java b/Basic/PrintAllDivisors.java new file mode 100644 index 0000000..468830c --- /dev/null +++ b/Basic/PrintAllDivisors.java @@ -0,0 +1,23 @@ +// write a code in java to print all divisors in o(n ^ 1/2) time complexity using function + +import java.util.Scanner; + +class PrintAllDivisors{ + public static void main(String[] args){ + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + scanner.close(); + printDivisors(n); + } + + static void printDivisors(int n){ + for(int i = 1; i * i <= n; i++){ + if(n % i == 0){ + System.out.print(i + " "); + if(i != n / i){ + System.out.print(n / i + " "); + } + } + } + } +} \ No newline at end of file diff --git a/Basic/ReverseNumber.java b/Basic/ReverseNumber.java new file mode 100644 index 0000000..82b4476 --- /dev/null +++ b/Basic/ReverseNumber.java @@ -0,0 +1,21 @@ +// write a code to reverse a number in java +// input: 1234 +// output: 4321 + +import java.util.Scanner; + +class ReverseNumber { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("Enter a number: "); + int num = scanner.nextInt(); + scanner.close(); + int rev = 0; + while (num != 0) { + int digit = num % 10; + rev = rev * 10 + digit; + num = num / 10; + } + System.out.println("Reversed number: " + rev); + } +} \ No newline at end of file diff --git a/Basic/gcdORhcf.java b/Basic/gcdORhcf.java new file mode 100644 index 0000000..928fc75 --- /dev/null +++ b/Basic/gcdORhcf.java @@ -0,0 +1,21 @@ +// write a code to find gcd using euclidean algorithm in a function + +import java.util.Scanner; + +class gcdORhcf { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("Enter two numbers to find their GCD: "); + int num1 = scanner.nextInt(); + int num2 = scanner.nextInt(); + scanner.close(); + System.out.println("GCD of " + num1 + " and " + num2 + " is " + findGCD(num1, num2)); + } + + public static int findGCD(int num1, int num2) { + if (num2 == 0) { + return num1; + } + return findGCD(num2, num1 % num2); + } +} \ No newline at end of file