mirror of https://github.com/evanferrao/dsa
day1: add basic dsa code
This commit is contained in:
commit
75ac8e30cd
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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 + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue