C++ program to find HCF (Highest Common Factor) of two numbers
Write a C++ program to find HCF (Highest Common Factor) of two numbers.
Flowchart of the program to find HCF of two numbers
Source Code of the program to find HCF of two numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<iostream> using namespace std; int main() { int n1,n2,hcf,j,i; hcf=1; cout<<"enter no:"<<endl; cin>>n1>>n2; j=(n1<n2)?n1:n2; i=1; do {
if(n1%i==0&&n2%i==0) { hcf=i; } i++; } while(i<=j); cout<<"HCF is:"<<hcf<<endl; } |
Output
enter no:
9
7
HCF is:1
Excercise
Find the possible mistakes in the following Shamil’s Flow Table of the program to find HCF (Highest Common Factor) of two numbers.
Loop | if | What lines will execute | Actual work to do |
n1=12,n2=24;
i=1; |
true | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. 12, 13A, 13B, 14, 15, 16, 17, 18, 19T, 13A, 13B, 14, 15, 16, 17, Â 18, 19F, 20, 21
|
n1%i==0&&n2%i==0
12%1==0&&24%1==0 0&&0 |
n1=13,n2=3;
i=2; |
false | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. 12, 13A(F),13B(F),17,18,19T,12,13A(f),13B(f),17, 18,19F,20,21
|
n1%i==0&&n2%i==0
13%1==0&&3%1==0 1&&1 |