Computer Science, asked by Zohda234, 5 months ago

The greatest common divisor (gcd) of two integers is calculated by the continued division method. Divide the larger number by the smaller the remainder reaches to zero. The last divisor results is gcd.

Please guys i will mark as brainlist answer ​

Answers

Answered by anindyaadhikari13
2

Required Answer:-

Question:

  • WAP to calculate hcf of two numbers.

Solution:

1. In Java.

import java.util.*;

public class HCF {

public static void main(String[] args) {

int a, b, r;

Scanner sc=new Scanner(System.in);

System.out.print("Enter first number: ");

a=sc.nextInt();

System.out.print("Enter second number: ");

b=sc.nextInt();

while (a%b!=0) {

r=a%b;

a=b;

b=r;

}

System.out.println("HCF: "+b);

sc.close();

}

}

2. In Python.

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

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

while not a%b==0:

r=a%b

a=b

b=r

print("HCF: ",b)

3. In C

#include <stdio.h>

int main() {

int a, b, r;

printf("Enter first number: ");

scanf("%d", &a);

printf("Enter second number: ");

scanf("%d", &b);

while (a%b!=0) {

r=a%b;

a=b;

b=r;

}

printf("HCF: %d", b);

return 0;

}

4. In C++

#include <iostream>

using namespace std;

int main() {

int a,b,r;

cout << "Enter first number: ";

cin >> a;

cout << "Enter second number: ";

cin >> b;

while (a%b!=0) {

r=a%b;

a=b;

b=r;

}

cout << "HCF: " << b;

return 0;

}

Dry Run:

Consider two numbers, say 18 and 24

a = 18 and b = 24

a%b == 0 is false. So,

r = a%b = 18

a = b = 24

b = r = 24

Again, a%b == 0 is false. So,

r = a%b = 6

a = b = 24

b = r = 6

Now, a%b == 0 is true as 24%6 is true. So,

HCF = b = 6

Therefore, HCF of 18 and 24 is 6.

See the attachment for output ☑.

Attachments:
Similar questions