Thursday, 22 December 2016

C++ Program to display HCF and LCM of two numbers

Statement:C++ Program to display HCF and LCM of Two numbers
Code:
// C++ Program to display HCF and LCM of Two numbers
#include<iostream>
using namespace std;
int main()
{
       int num1, num2, tmp,g,n1,n2; //tmp is a temporary variable to count lcm by hcf
       cout << "\t\t\tLCM And HCF Of Two Numbers\n\n";
       cout << "Enter First No : ";
       cin >> num1;
       cout << "Enter Second Number : ";
       cin >> num2;
       n1 = num1; n2 = num2;
       tmp = num1*num2;
       while ( num1 != num2) //loop to count hcf
       {
              if (num1>num2) //num1 will be hcf if num1 is greator then num2
                     num1 = num1 - num2;
              else
                     num2 = num2 - num1; //if num2 is greatoe then num1 then num2-num1 = num2
       }
       cout << "HCF = " << num1 << endl; //HCF
       cout << "LCM = " << tmp / num1 ; // LCM==
       cout << "\n\n";
       system("pause");
       return 0;

}

Mohammad Tayyab

Author & Editor

Imagination is more important than knowledge.

0 comments:

Post a Comment