16/03/2019

Find g.c.d of two integers in C Programming using for-loop

Find g.c.d of two integers in C-Programming using for-loop:

First of all, what are g.c.d means? g.c.d means Greatest common divisor, it is also known as h.c.f i.e. Highest common factor. For example, g.c.d of 9 and 6 is 3 since 3 is the greatest integer which divides both 9 and 6. Again g.c.d of 5 and 3 is 1.
Find g.c.d of two integers in C Programming using for loop

You can see also:

2. Design * triangle in C-Programming

Important Tools:

1. for-loop
2. if-else

Code:

Find g.c.d of two integers in C-Programming using for-loop

#include<stdio.h>
int main()
{
  int a,b,g,i;
  printf("Enter the two integer numbers:");
  scanf("%d%d",&a,&b);
{
  for(i=1;i<=a && i<=b;++i)
  if(a%i==0 && b%i==0)
  g=i;
}
  printf("The gcd of %d and %d is %d",a,b,g);
}

Output: 

The output is given below.
Find g.c.d of two integers in C-Programming using for-loop


Explanation:

1. We use Header Package <stdio.h> here.
2. We use 4 integers variables a,b,g and i. a and b are used to storing the value of given numbers from users. g is for getting value of g.c.d and i is for for-loops.
3. How does the for-loop works here:
If any users give two integers say a=4 and b=10 then it will entered in the loop with this value of a and b and start with initial condition i =1 and the condition is I should be less than both a and b and pre-increment the value of i by 1. After entering in the loop it will check if condition i.e. after dividing both a and b with I, the residue should be 0 then it will give the value of g.c.d.

You can see also:

2. Find the transpose of a matrix in C-Programming
3. Find the largest number in C-Programming

3 comments: