Q.Write an algorithm that reads two numbers find and display
their GCD (Greatest Common Divisor).
#include<stdio.h>
#include<conio.h>
int x1,x2,GCD,rem,t1,t2,t;
void main()
{
printf("Enter 1st Number:");
scanf("%d",&x1);
printf("Enter 2nd Number:");
scanf("%d",&x2);
t1=x1;
t2=x2;
while(x2!=0)
{
rem=x1%x2;
GCD=x2;
x1=x2;
x2=rem;
}
if(GCD<0)
GCD=GCD*-1;
printf("GCD of %d and %d =%d",t1,t2,GCD);
getch();
}
No comments:
Post a Comment