Flowchart for GCD of two numbers

In this program, we’ll learn to find Greatest Common Divisor (GCD) of two numbers in Algortihm.

Pseudocode:

BEGIN NUMBER n1 , n2 , gcd = 1, i OUTPUT "Enter first Number:" INPUT n1 OUTPUT "Enter second Number:" INPUT n2 FOR i = 1; i <= n1 && i <= n2; ++i THEN IF n1 % i == 0 && n2 % i == 0 THEN gcd = i END IF END FOR OUTPUT " G.C.D of "+n1+"and "+n1+" is "+ gcd END

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

BEGIN

NUMBER n1 , n2 , gcd = 1, i

OUTPUT "Enter first Number:"

INPUT n1

OUTPUT "Enter second Number:"

INPUT n2

FOR  i = 1; i <= n1 && i <= n2; ++i THEN

IF n1 % i == 0 && n2 % i == 0 THEN

gcd = i

        END IF

END FOR

OUTPUT " G.C.D of "+n1+"and "+n1+" is "+ gcd

END

Flowchart:

Flowchart for GCD of two numbers

Java Code: Java Program to find gcd of two numbers using for loop

public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Create a Scanner object System.out.print("Enter a Number 1:"); int n1 = scanner.nextInt(); System.out.print("Enter a Number 2:"); int n2 = scanner.nextInt(); int gcd = 1; for(int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if(n1 % i==0 && n2 % i==0) gcd = i; } System.out.printf("G.C.D of %d and %d is %d \n", n1, n2, gcd); }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

   public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);  // Create a Scanner object

        System.out.print("Enter a Number 1:");

        int n1 = scanner.nextInt();

        System.out.print("Enter a Number 2:");

        int n2 = scanner.nextInt();

        int  gcd = 1;

        for(int i = 1; i <= n1 && i <= n2; ++i)

        {

            // Checks if i is factor of both integers

            if(n1 % i==0 && n2 % i==0)

                gcd = i;

        }

        System.out.printf("G.C.D of %d and %d is %d \n", n1, n2, gcd);

    }

Python Code: Write a program to Find GCD of Two Numbers in Python

# take input from the user num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) # choose the smaller number if num1 > num2: smaller = num2 else: smaller = num1 for i in range(1, smaller+1): if((num1 % i == 0) and (num2 % i == 0)): hcf = i print("The H.C.F. of", num1,"and", num2,"is", hcf)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

# take input from the user

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

# choose the smaller number

if num1 > num2:

    smaller = num2

else:

    smaller = num1

for i in range(1, smaller+1):

    if((num1 % i == 0) and (num2 % i == 0)):

        hcf = i

print("The H.C.F. of", num1,"and", num2,"is", hcf)

JavaScript Code: Write a program to Find GCD of Two Numbers in JavaScript

<body> <input type="text" id="b1" placeholder="Enter first number"> <input type="text" id="b2" placeholder="Enter second number"> <input type="button" value="Calculate" id="btnCalc"> <div id="result" style="width:250px; border: 1px solid #ddd;position: absolute;top:50px;left:50;padding:20px"> </div> <script> function area(){ var n1=document.getElementById("b1").value; var n2=document.getElementById("b2").value; var result=document.getElementById("result"); n1=Number(n1); n2=Number(n1); let gcd = 1; for(let i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if(n1 % i==0 && n2 % i==0) gcd = i; } result.textContent= "G.C.D of "+n1+" and "+n2+" is "+gcd+" \n"; } var btnCalc=document.getElementById("btnCalc"); btnCalc.onclick=area; </script> </body>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<body>

    <input type="text" id="b1" placeholder="Enter first number">

<input type="text" id="b2" placeholder="Enter second number">

<input type="button" value="Calculate" id="btnCalc">

<div id="result" style="width:250px; border: 1px solid #ddd;position: absolute;top:50px;left:50;padding:20px">

</div>

<script>

function area(){

    var n1=document.getElementById("b1").value;

    var n2=document.getElementById("b2").value;

    var result=document.getElementById("result");

    n1=Number(n1);

    n2=Number(n1);

    let  gcd = 1;

    for(let i = 1; i <= n1 && i <= n2; ++i)

    {

        // Checks if i is factor of both integers

        if(n1 % i==0 && n2 % i==0)

            gcd = i;

    }

    result.textContent= "G.C.D of "+n1+" and "+n2+" is "+gcd+" \n";

}

var btnCalc=document.getElementById("btnCalc");

btnCalc.onclick=area;

</script>

</body>

Output:

Flowchart for GCD of two numbers