Recursion | GCD | Euclids Algorithm | Lecture 31 | Java & DSA Course

J    A    V     A               C    O     D     E

  1. Write a program to find Greatest Common Deviser (GCD) by using Recursion  

    </>   RecGcd1.java    

  1. // BRUTE FORCE APPROACH

  2. package Recursion;
  3. import java.util.Scanner;

  4. public class RecGcd1 {
  5. public static int find(int x,int n1,int n2){
  6. if(n1%x==0 && n2%x==0)
  7. return x;
  8. return find(x-1,n1,n2);
  9. }
  10. public static void main(String[] args) {
  11. Scanner sc=new Scanner(System.in);
  12. System.out.println("Enter n1 and n2");
  13. int n1=sc.nextInt();
  14. int n2=sc.nextInt();
  15. int x;
  16. if(n1<=n2) x=n1;
  17. else x=n2;
  18. int ans=find(x,n1,n2);
  19. System.out.println(ans);

  20. sc.close();
  21. }
  22. }


   2. Writing this GCD program by Second Method    

   </>  RecGCDbyLongDivision.java   
  1. // BY LONG DIVISION CONCEPT :

  2. package Recursion;
  3. import java.util.Scanner;

  4. public class RecGCDbyLongDivision {
  5. public static int gcd(int s,int b){

  6. if(s==0) return b;
  7. return gcd(b%s,s);

  8. }
  9. public static void main(String[] args) {
  10. Scanner sc=new Scanner(System.in);
  11. System.out.println("Enter x and y: ");
  12. int x=sc.nextInt();
  13. int y=sc.nextInt();
  14. int s,b;
  15. if(x<y){
  16. s=x;
  17. b=y;
  18. }
  19. else{
  20. s=y;
  21. b=x;
  22. }

  23. int ans=gcd(s,b);
  24. System.out.println("gcd of "+x+" and"+y+" is :"+ans);
  25. sc.close();
  26. }
  27. }




    3. Writing previous program by using EUCLID ALGORITHM     

   </>    RecGCDbyEuclidAlgorithm.java    
  1. // BY LONG DIVISION CONCEPT :

  2. package Recursion;
  3. import java.util.Scanner;

  4. public class RecGCDbyEuclidAlgorithm {
  5. public static int gcd(int x,int y){

  6. if(y==0) return x;
  7. return gcd(y,x%y);

  8. }
  9. public static void main(String[] args) {
  10. Scanner sc=new Scanner(System.in);
  11. System.out.println("Enter x and y: ");
  12. int x=sc.nextInt();
  13. int y=sc.nextInt();

  14. int ans=gcd(x,y);
  15. System.out.println("gcd of "+x+" and"+y+" is :"+ans);
  16. sc.close();
  17. }
  18. }



      4. Write a program to finding LCM  by using Recursion      

     </>   RecLCM.java     
  1. // WE KNOW THAT : lcm(x,y)= (x*y)/gcd(x,y)

  2. package Recursion;
  3. import java.util.Scanner;

  4. public class RecLCM {
  5. public static int lcm(int x,int y){
  6. if(y==0) return x;
  7. return lcm(y,x%y);
  8. }
  9. public static void main(String[] args) {
  10. Scanner sc=new Scanner(System.in);
  11. System.out.println("Enter x and y: ");
  12. int x=sc.nextInt();
  13. int y=sc.nextInt();
  14. int ans=lcm(x,y);
  15. System.out.println("LCM of "+x+" and "+y+" is: "+(x*y)/ans);

  16. sc.close();
  17. }
  18. }
      5. Write a program to check  ArmStrong Number      

    </>   RecArmStrongNum.java   
  1. package Recursion;
  2. import java.util.Scanner;

  3. public class RecArmStrongNum {

  4. // Methods for finding POWER :
  5. public static int power(int d,int i){
  6. if(i==0) return 1;
  7. int x=power(d,i/2);
  8. if(i%2==0)
  9. return x*x;
  10. else
  11. return d*x*x;

  12. }

  13. // Methods for finding ARMstrong :
  14. public static int checking(int num,int i,int count){
  15. if(count==0) return 0;
  16. return power(num%10,i)+checking(num/10,i,count-1);
  17. }

  18. // MAIN METHOD :
  19. public static void main(String[] args) {
  20. Scanner sc=new Scanner(System.in);
  21. System.out.println("Enter a number : ");
  22. int n=sc.nextInt();
  23. int num=n;
  24. // finding number of digits ( 'i' denotes no. of digit )
  25. int i=0;
  26. for(;n>0;i++) n/=10;
  27. int count=i;
  28. int ans=checking(num,i,count);
  29. if(num==ans)
  30. System.out.println("Y E S , "+num+" is an ARM Strong number");
  31. else
  32. System.out.println("N O , "+num+" is not a ARM Strong number");
  33. sc.close();

  34. }
  35. }

6. Write a program to find a Simple Product

</> RecSimpleMultiply.java
  1. // SIMPLE PRODUCT :

  2. package Recursion;
  3. import java.util.Scanner;

  4. public class RecSimpleMultiply {
  5. public static int multi(int a,int b){
  6. if(b==0) return 0;
  7. return a+multi(a,b-1);
  8. }
  9. public static void main(String[] args) {
  10. Scanner sc=new Scanner(System.in);
  11. System.out.println("Enter a and b: ");
  12. int a=sc.nextInt();
  13. int b=sc.nextInt();
  14. int ans=multi(a,b);

  15. sc.close();
  16. }
  17. }

- SEE YOU AGAIN !!

Comments

Popular posts from this blog

Recursion | Linear Search | Find all indices | Lecture 33 | Java and DSA Course

S O M E | B A S I C | M E T H O D S | O F | S t r i n g | I N | J A V A :