Brijmohan lal Sahu - Facebook
ads
ads

Tuesday, November 4, 2014

Program to find H.C.F. of two numbers in C++.


Code Box
---------------------------------------
/*Program to find H.C.F. of two numbers */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num1,num2,remainder,dividend,divisor;

cout<<"\n---------------H.C.F. Calculator --------------------";
cout<<"\nEnter two numbers..";
cout<<"\nEnter first number: ";
cin>>num1;

cout<<"\nEnter second number: ";
cin>>num2;

if(num1>num2)  //Checker for greater number
{
remainder=num1%num2;
dividend=num2;
}
else{
remainder=num2%num1;
dividend=num1;
}
//If remainder is zero
if(remainder==0)
{
cout<<"\nH.C.F. of "<<num1<<" & "<<num2<<" is :"<<dividend;
}
else
{

divisor=remainder;
while(remainder !=0)
{
remainder=dividend%divisor;
dividend=divisor;
divisor=remainder;
}

cout<<"\nH.C.F. of "<<num1<<" & "<<num2<<" is :"<<dividend;

}

cout<<"\n---------------H.C.F. Calculator --------------------";

getch();

}

1 comment:

  1. Thanks!! Hitesh, for sharing your Idea.
    Above program is my Idea or you can say my point of view.
    And that's the good thing about programming, for single problem multiple approach exist.

    ReplyDelete