Blogger templates

Pages

Thursday, 17 May 2012

Write a C Program to Compute the LCM and HCF of Two Numbers by using While Statement

LCM and HCF

HCF:
  • HCF Stands For Higest Common Factor.
  • HCF is also Known as GCD (Greatest Common Divisor).
  • HCF of Two Number is defined as the Largest Number that Divides Both Numbers Completely with Remainder Zero.
LCM:
  • LCM Stands For Lowest Common Multiple.
  • LCM of two Numbers Defined as the Smallest Number that is Multiple of both Numbers. 
Statement of C Program: Find The LCM and HCF of Two Numbers :

#include<stdio.h>
#include<conio.h>

void main()
{
int num1 , num2 , lcm , gcd , remainder , numerator , denominator ;
clrscr();

printf( "Enter two numbers\n");
scanf(" %d%d " , &num1 , &num2 );
if (num1 > num2)
{
numerator=num1;
denominator=num2;
}
else
{
numerator = num2 ;
denominator = num1 ;
}

remainder = num1 % num2;
while ( remainder != 0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = (num1 * num2 ) / gcd;

printf("GCD of %d and %d =%d\n" , num1, num2, gcd);
printf(" LCM of %d and %d= %d\n" , num1, num2, lcm);
getch();
}

Output:
 Enter two numbers
 5
 15
 GCD of 5 and 15 = 5
 LCM of 5 and 15 = 15


0 comments:

Post a Comment