HOW TO CHECK ALL COMMON FACTORS OF TWO NUMBERS IN JAVA

CHECKING ALL COMMON FACTORS OF TWO NUMBER

 

STEPS TO DO 

1: input a,b,n,i a and b is numbers to check n is the length and i is for loop

2: take a,b,n from user.

3:start the loop from 1

4: check if(a%i==0&&b%i==0) then print(i)

5:save and run

THE SOURCE CODE

import java.util.Scanner;
class common
{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int a,b,n,i;// a is first number b is second and n is for length and i is for loop
        System.out.println("enter the length");// taking length from user
        n=s.nextInt();
        System.out.println("enter the first number");//first number
        a=s.nextInt();
        System.out.println("enter the second number");//second number
        b=s.nextInt();
        for(i=1;i<=n;i++)
        {
            if(a%i==0&&b%i==0)
            {
                System.out.println("the common factors are :"+i);
            }
        }

    }

OUTPUT

  WATCH MY YOUTUBE CHANNEL


 

Comments

Popular Posts