Write a C program to find GCD of each of the two consecutive elements of an array. write a function GCD(a,b) that would take two consecutive elements of the array as arguments and return the GCD

Posted by Tushar Bedekar

#include(stdio.h)                                  // Note place '<' & '>' in place of '(' & ')'
int gcd(int,int);
void main()
{
  int a[10],n,i,b[10];
  int x,y;
  clrscr();
  printf("Enter the size of the array:");
  scanf("%d",&n);
  printf("\nEnter the array elements:");
  for(i=0; i< n ;i++)
    scanf("%d",&a[i]);
 for(i=0; i< n-1 ;i++)
  {
     x=a[i];
     y=a[i+1];
     b[i]=gcd(x,y);
    printf("\nG.C.D of %d and %d is : %d",x,y,b[i]);
  }
getch();
}
int gcd(int a,int b)
{
  int rem;
  for(;;)
  {
    rem=a%b;
    if(rem==0)
       break;
    a=b;
    b=rem;
  }
 return b;
}

0 comments:

Post a Comment

back to top