int m = m1;
int n = n1;
int temp;
while (m >0) {
if(n > m) {temp =n; n=m; m=temp; }
m = m – n }
return n; }
void main( ) {
cout<< gcd(24,18); }
}
OUTPUT: 6
Method 3
void main( )
{
int a ,b, gcd;
a =b=gcd=0;
printf(“\n\t Enter two numbers:”);
scanf(“%d %d, &a, &b);
while(a!=0)
{
gcd =a;
a= b% a;
b=gcd;
}
printf(“GCD = %d”,gcd);
}
}
].
Q5.
Enter a number from the keyboard and find out the Fibonacci series using a while loop.
[Hint:
void main( )
{
int a=0, b=1, c=0, size;
printf(“\n\t Enter a range”);
size = s;
printf(“\n”);
printf(“Enter a and b:”);
while(c <= size)
{
c = a +b;
if(c<=size)
printf(“%d\n”, c);
a=b;
b=c;
}
}
}
Do'stlaringiz bilan baham: