Welcome to the World of Online Learning:
Hello Friends “This blog helps you to learn C++ programming concepts. You can learn C++ language at your own speed and time. One can learn concepts of C++ language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Write a C++ Program to Find HCF
1. PROGRAM: C++ Program to Find HCF using for loop
/* C++ Program to Find HCF*/
#include <iostream>
using namespace std;
int main() {
int n1, n2, hcf;
cout << “Enter two numbers: “;
cin >> n1 >> n2;
// swapping variables n1 and n2 if n2 is greater than n1.
if ( n2 > n1) {
int temp = n2;
n2 = n1;
n1 = temp;
}
for (int i = 1; i <= n2; ++i) {
if (n1 % i == 0 && n2 % i ==0) {
hcf = i;
}
}
cout << “HCF = ” << hcf;
return 0;
}
2. PROGRAM: C++ Program to Find HCF using while loop
/* C++ Program to Find HCF*/
#include <iostream>
using namespace std;
int main() {
int n1, n2;
cout << “Enter two numbers: “;
cin >> n1 >> n2;
while(n1 != n2) {
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
cout << “HCF = ” << n1;
return 0;
}