Assignments » Flow Of Control » Set2 » Solution 8

Write a program to calculate HCF of Two given number.

 

#include<iostream>
using namespace std;

int main()
{
	int dividend, divisor, rem=1, hcf;
	cout<<"Enter two numbers : ";
	cin>>dividend>>divisor;

	while(rem!=0)
	{
		rem=dividend%divisor;
		if(rem==0)
			hcf=divisor;
		else
		{
			dividend=divisor;
			divisor=rem;
		}
	}

	cout<<"HCF is : "<<hcf;


	return 0;
}

 

 

Enter two numbers : 30 105
HCF is : 15

 

«back