Di Posting Oleh : Simple Learning
Kategori : GCD GCD c++ program Greatest Common Divisor
What is GCD of two numbers?
Numbers which divides both are 1, 2, 3 and 6 in which greatest number is 6
So 6 is the GCD of 18 and 24
Concept used: for loop, if statement
Logic Explanation:
Image view of code:
Sample output image view:
Recommended: Change the logic and do experiment with code
for better understanding.
It means a greatest number which divides both numbersFor example: Two numbers are 18 and 24
Numbers which divides both are 1, 2, 3 and 6 in which greatest number is 6
So 6 is the GCD of 18 and 24
Concept used: for loop, if statement
Post contains:
- C++ code
- Program logic explanation
- Variable values after each iteration (Dry run the code)
- image view of code
- sample output
#include<iostream> |
Logic Explanation:
- Program takes input two numbers
- A for loop which terminates if loop variable which is 'i'
is greater than one of two numbers and operator is used to
maintain this condition - Within for loop an if condition is used to check if variable 'i'divides both of them it will be saved in GCD variable as value of 'i' is changing during each iteration in increasing order if another number divides both of them it will replace the previous value of GCD
- After the loop termination our program will show the GCD of two number as the result
Image view of code:
find GCD of two numbers c++ program |
Sample input with dry run of code:
Let our sample input is 9 and 24
Values before loop
first_number=9 second_number=24
for i=1
i<=9&&i<=24 condition is true
9%1==0 && 24%1==0 true
GCD=1
for i=2
i<=9&&i<=24 condition is true
9%2!=0 && 24%2==0 false
GCD=1
for i=3
i<=9&&i<=24 condition is true
9%3==0 && 24%8==0 true
GCD=3
Let our sample input is 9 and 24
Values before loop
first_number=9 second_number=24
for i=1
i<=9&&i<=24 condition is true
9%1==0 && 24%1==0 true
GCD=1
for i=2
i<=9&&i<=24 condition is true
9%2!=0 && 24%2==0 false
GCD=1
for i=3
i<=9&&i<=24 condition is true
9%3==0 && 24%8==0 true
GCD=3
for i=4
i<=9&&i<=24 condition is true
9%4!=0 && 24%4==0 false
GCD=3
for i=5
i<=9&&i<=24 condition is true
9%5!=0 && 24%5!=0 false
GCD=3
for i=6
i<=9&&i<=24 condition is true
9%6!=0 && 24%6==0 false
GCD=3
for i=7
i<=9&&i<=24 condition is true
9%7!=0 && 24%7!=0 false
GCD=3
for i=8
i<=9&&i<=24 condition is true
9%8!=0 && 24%8==0 false
GCD=3
for i=9
i<=9&&i<=24 condition is true
9%9==0 && 24%!9=0 true
GCD=3
for i=10
i<=9&&i<=24 CONDITION IS FALSE
Final output :
Greatest Common Divison (GCD): 3
i<=9&&i<=24 condition is true
9%4!=0 && 24%4==0 false
GCD=3
for i=5
i<=9&&i<=24 condition is true
9%5!=0 && 24%5!=0 false
GCD=3
for i=6
i<=9&&i<=24 condition is true
9%6!=0 && 24%6==0 false
GCD=3
for i=7
i<=9&&i<=24 condition is true
9%7!=0 && 24%7!=0 false
GCD=3
for i=8
i<=9&&i<=24 condition is true
9%8!=0 && 24%8==0 false
GCD=3
for i=9
i<=9&&i<=24 condition is true
9%9==0 && 24%!9=0 true
GCD=3
for i=10
i<=9&&i<=24 CONDITION IS FALSE
Final output :
Greatest Common Divison (GCD): 3
Recommended: Change the logic and do experiment with code
for better understanding.
0 Response to "Find Greatest Common Divisor (GCD) of two numbers c++ program"
Post a Comment