Write a program to calculate the Greatest Common Divisor(GCD) of a number
#include<stdio.h>
#include<conio.h>
#include<process.h>
int gcd(int m,int n)
{
int rem;
while(n!=0)
{
rem=m%n;
m=n;
n=rem;
}
return(m);
}
main()
{
int num1,num2,num3,gcd1,gcd2;
clrscr();
printf("Enter three positive integers");
scanf("%d%d%d",&num1,&num2,&num3);
if(num1==0 && num2==0 && num3==0)
{
printf("\n Invalid number");
exit(0);
}
gcd1=gcd(num1,num2);
gcd2=gcd(num3,gcd1);
printf("\n GCD of %d %d %d is : %d\n",num1,num2,num3,gcd2);
getch();
} |
#include<stdio.h>
#include<conio.h>
#include<process.h>
int gcd(int m,int n)
{
int rem;
while(n!=0)
{
rem=m%n;
m=n;
n=rem;
}
return(m);
}
main()
{
int num1,num2,num3,gcd1,gcd2;
clrscr();
printf("Enter three positive integers");
scanf("%d%d%d",&num1,&num2,&num3);
if(num1==0 && num2==0 && num3==0)
{
printf("\n Invalid number");
exit(0);
}
gcd1=gcd(num1,num2);
gcd2=gcd(num3,gcd1);
printf("\n GCD of %d %d %d is : %d\n",num1,num2,num3,gcd2);
getch();
}
Editorial Team
We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.