Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Keeping aside how we calculate lcm by gcd/(a*b) way, how to modify this code slightly such that it gives ad-hoc result for lcm calculation.

What I have tried:

void main()
{
	int a,b,i,v,lcm=1;
	printf("Enter two numbers");
	scanf("%d %d",&a,&b);
    
    	
	while(i<=(a*b))
	{
		if( a%i==0 && b%i==0)
		{
			v= i*lcm;
			i++;
			
		}
		else
		i++;
	
	}
	printf("The lcm is %d",v);
	getch();
}
Posted
Updated 14-Nov-17 0:11am
Comments
Richard MacCutchan 14-Nov-17 6:05am    
What formula are you trying to use? Also, you have not initialised i before using it in your while loop.

1 solution

Try
C
#include <stdio.h>

int main()
{
  int a,b,lcm;
  printf("Enter two numbers\n");
  scanf("%d %d",&a,&b);


  lcm = a > b ? a : b;

  while(lcm <= (a*b) )
  {
    if( lcm%a==0 && lcm%b==0)
    {
      break;
    }
    ++lcm;
  }
  printf("The lcm is %d\n",lcm);
  getchar();
  return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900