Q:

Write C++ program to find HCF of two numbers

0

Write C++ program to find HCF of two numbers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
 
using namespace std;
 
int main()
{
    int i, num1, num2, min, HCF=1;
 
    //Read two numbers from user
    cout<<"Enter any two numbers:";
    cin>>num1;
    cin>>num2;
 
    // Find min number between two numbers
    min = (num1<num2) ? num1 : num2;
 
    for(i=1; i<=min; i++)
    {
        if(num1%i==0 && num2%i==0)
        {
            HCF = i;
        }
    }
 
    cout<<"HCF of "<<num1<< " and "<< num2<< " is: " <<HCF;
    return 0;
 
}

Result:

Enter any two numbers:54

24

HCF of 54 and 24 is: 6

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write C++ program to find LCM of two numbers... >>
<< Write C++ program to print number in words...