مرحبا في عندي سؤال بدي ابرمجه بال c++
والي هو ايجاد القاسم المشترك الاكبر GCD
انا برمجته واشتغل معي
وهاي الكود
#include<iostream>
using namespace std;
int main()
{
int num1;
int num2;
int gcd;
int i;
cout<<"enter two numbers : ";
cin>>num1>>num2;
for(i=1; i<=num1;i++)
{
if (num1%i ==0 && num2%i==0)
gcd=i;
}
cout<<"\n gcd = "<<gcd<<endl;
return 0;
}يعني سهل كتير بس بدي اعطيكم كود تاني علة نفس السؤال بس فيه عليه كمان اسئله بدي تساعدوني فيها
- Write the following program and run it for x=120 and y=80.
<DIV class=smallfont style="MARGIN-BOTTOM: 2px">#include <iostream>
int GCD(int x, int y);
using namespace std;
int main()
{
int x,y;
cout<<"enter x value :"; cin>>x;
cout<<"enter y value :"; cin>>y;
cout<<"the GCD for"<<x<<"and"<<y<<"is"<<GCD(x,y)<<endl;
int test;
cin>>test;
return 1;
}
int GCD(int x, int y){
while (y!=0){
int t=x%y;
x=y;
y=t;
}
return x;
}</DIV>- Modify the code to print out the number of steps taken in the while loop.
- Write a new function (called GCD2) that implements the recursive version of Euclid's algorithm.
- Modify your function GCD2 to print out the number of the recursive calls made.
- Call the two functions for the same inputs x and y to verify that both gives similar answers.
- repeat the runs for different x and y values
Tasks:
1)Hand in the function GCD as explained in step 2.
2)Hand in the function GCD2 as explained in step 3.
3)Hand in the function GCD2 as explained in step 4.
4)Show sample runs for steps 5 and 6 (screen shots).
