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.
thank you for this help...........
ReplyDeleteYour Welcome.
Deletethanx
ReplyDeletethanx for help
ReplyDeleteWelcome Mukesh
DeleteHow i can do this with break statement
Deletethanks,it's really helpful :)
ReplyDeleteWelcome. :)
DeleteThnx for Clarification..
ReplyDeleteThanks for this logic
ReplyDeleteWonderful, beautiful program brotha.
ReplyDeleteYou can see my c++ program to find the greatest common divisor
ReplyDeletehttp://www.mycodingland.com/2015/11/find-greatest-common-divisor-in-cpp-programming.html
#include
using namespace std;
#include
int main()
{
int x,y,m,i;
cout<<"Insert Any Two Number: ";
cin>>x>>y;
if(x>y)
m=y;
else
m=x;
for(i=m;i>=1;i--)
{
if(x%i==0&&y%i==0)
{
cout<<"\nGCD of two number is : "<<i;
break;
}
}
getch ( );
return 0;
}
// www.mycodingland.com //
plz give me this solution in break statement;;
ReplyDeletewow
Deleteplz give me this solution in break statement;;
ReplyDeleteAnyone can give an output like this please
ReplyDeleteEnter first number:10
Enter second number:8
The gceof 10 and 8 is 2
Great work
ReplyDelete