Write a C++ program to find HCF (Highest Common Factor) of two numbers. .

#include <iostream>
using namespace std;
void main()
{
    int i, n1, n2, j, hcf=1;


     cout<<"\n  HCF of two numbers:\n ";
     printf("\n");


    cout<<"Input 1st number for HCF: ";
    cin>>n1;
    cout<<"Input 2nd number for HCF: ";
    cin>>n2;
 
    j = (n1<n2) ? n1 : n2;
 
    for(i=1; i<=j; i++)
    {

        if(n1%i==0 && n2%i==0)
        {
            hcf = i;
        }
    }
 
    cout<<"\nHCF of "<<n1 <<"and "<<n2 <<"is : "<<hcf<<endl;



      system ("pause");
}


Test Data : 
Input 1st number for HCF: 24 
Input 2nd number for HCF: 28 
Expected Output :
HCF of 24 and 28 is : 4 

Write a C++ program to find HCF (Highest Common Factor) of two numbers. .  Write a C++ program to find HCF (Highest Common Factor) of two numbers. . Reviewed by Unknown on June 11, 2017 Rating: 5

No comments:

Powered by Blogger.