Showing posts with label GCD. Show all posts
Showing posts with label GCD. Show all posts

Find g.c.d of two number using c program.

How To Find g.c.d of two number using c program.


#include<stdio.h>
int main(){
int n1,n2;
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
while(n1!=n2){
if(n1>=n2-1)
n1=n1-n2;
else
n2=n2-n1;
}
printf("\nGCD=%d",n1);
return 0;
}

C Program to Calculate GCD using recursion

How to write a C Program to calculate Greatest Common Divisor using recursion in C Programming Language ?

Solution For C Program :

/*C Program to calculate Greatest Common Divisor using recursion.*/

#include<stdio.h>
void main()
{
int gcd(int,int);
int a, b, result;
printf("\nEnter Any Two Values : ");
scanf("%d%d", &a, &b);
result = gcd(a, b);
printf("\nThe GCD of %d and %d is %d.", a, b, result);
}
int gcd(int x, int y)
{
int temp, n;
temp = x % y;
if(temp != 0)
{
n = gcd(y, temp);
return(n);
}
if(temp == 0)
return y;
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program To Find GCD Of Two Non-Negetive Numbers

How to write a C Program to find GCD of Two Non Negetive numbers in C Programming Language ?


Solution For C Program:

/*C  Program To Find GCD Of Two Non-Negetive Numbers*/

#include<stdio.h>
void main()
{
int x, y, temp;
printf("\nEnter any two Non Negetive Numbers : ");
scanf("%d%d", &x, &y);
while(y != 0)
{
temp = x % y;
x = y;
y = temp;
}
printf("\nThe GCD of Two Numbers is %d.", x);
}

C Program to Print the multiplication table :

#include<stdio.h>
void main()
{
int num, counter = 1;
printf("\nEnter the Table Number to Generate : ");
scanf("%d", &num);
printf("\n\n\t\t\tPrinting Table\n\n");
while(counter <= 10)
{
printf("%d\t*\t%d\t= %d\n", num, counter, num * counter);
counter++;
}
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable