Friday, June 17, 2016

C program to calculate GCD (greatest common divisors) of two numbers

Problem Statement:- GCD of two numbers is the greatest number which divides both the numbers
ex:- 20 and 10 have 10 as GCD because it divides both the numbers completely.

#include<stdio.h>
void function_gcd( int catch_firstnumber,int catch_secondnumber,int minimum_number1 )
{
  int counter_variable,gcd;
  for(counter_variable=1;counter_variable<=minimum_number1;counter_variable++)
  {
    if(catch_firstnumber%counter_variable==0 && catch_secondnumber % counter_variable == 0 )
gcd=counter_variable;
  }
 printf("the gcd of %d and %d is %d",catch_firstnumber,catch_secondnumber,gcd);
}
main()
{
float first_number,second_number;
int minimum_number,counter_variable,gcd;
int first_temp,second_temp;
clrscr();
printf("Enter the First integer number\n");
scanf("%f",&first_number);
first_temp=first_number;
        //processing for calculating GCD
while((first_number/first_temp!=1)||(first_temp<0))
{
if(first_number/first_temp!=1)
{
 printf("\nEnter a integer number only Enter first number again");
}
else
{
 printf("\nEnter a positive number only Enter first number again");
}
scanf("%f",&first_number);
first_temp=first_number;
}
printf("\nEnter the Second integer");
scanf("%f",&second_number);
second_temp=second_number;
while((second_number/second_temp!=1)||(second_temp<0))
{
if(second_number/second_temp!=1)
{
 printf("\nEnter a integer number only Enter Second number again");
}
else
{
 printf("\nEnter a positive number only Enter Second number again");
}
scanf("%f",&second_number);
second_temp=second_number;
}
    
if(first_number>second_number)
{
  minimum_number = second_number;
  function_gcd(first_number,second_number,minimum_number);
}
else
{
 minimum_number = first_number;
 function_gcd(first_number,second_number,minimum_number);
}
return 0;
}
output:
Enter the First integer number 20
Enter the Second integer number 10
the gcd of 20 and 10 is 10

No comments:

Post a Comment