Write a program to prompt the user for two positive integers

Write a program to prompt the user for two positive integers x, y and output the greatest common divisor (GCD) and the least common multiple (LCM). The greatest common divisor is the largest positive integer n that divides both numbers x and y without a remainder. The least common multiple is smallest positive integer n that is 1 divisible by x and y. If the user enters 0 or a negative number report an error message. The following function is required (Note: all arguments are pass-by-reference). void gcd lcm(int *x, int *y, int *gcd, int *lcm);

I have this programe that runs perfuctlly but I need to use

void gcd_lcm(int *x, int *y, int *gcd, int *lcm); as a function

there is my code just modify it use   gcd_lcm(int *x, int *y, int *gcd, int *lcm); as afuction


//computing GCD and LCM

// header
#include<stdio.h>
#include<math.h>

// main function
int main()
{
// declare varibles
int x;
int y;
int GCD;
int LCM;
int remainder;
int xx;
int yy;
// user input
printf(\"Enter two positive integers: \ \");
scanf(\"%d%d\", &x, &y);
xx=x;
yy=y;
if(x<=0||y<=0)
printf(\"error \ \");
else{

while(remainder!=0){
// computing the GCD
remainder=xx%yy; xx=yy; yy=remainder;
}
GCD=xx;
LCM=(x*y)/GCD;
printf(\"GCD: %d\ \", GCD);
printf(\"LCM: %d \ \", LCM);
}
return 0;
}

Solution

GcdLcm.c

#include <stdio.h>
void gcd_lcm(int *x, int *y, int *gcd, int *lcm);
int main()
{
int x, y, gcd, lcm;

printf(\"Enter two positive integers: \");
scanf(\"%d %d\", &x, &y);
if(x<=0||y<=0)
printf(\"error \ \");
else{
gcd_lcm(&x, &y, &gcd, &lcm);
printf(\"GCD: %d LCM: %d\ \",gcd, lcm);
}
  
}

void gcd_lcm(int *x, int *y, int *gcd, int *lcm){
int r, num, d;
if (*x > *y)
{
num = *x;
d = *y;
}
else
{
num = *y;
d = *x;
}
r = num % d;
while (r != 0)
{
num = d;
d = r;
r = num % d;
}
*gcd = d;
*lcm = *x * *y / *gcd;
  
}

Output:

sh-4.3$ gcc -o main *.c                                                                                                                                                                                                     

sh-4.3$ main                                                                                                                                                                                                                

Enter two positive integers: 15 28                                                                                                                                                                                          

GCD: 1 LCM: 420

Write a program to prompt the user for two positive integers x, y and output the greatest common divisor (GCD) and the least common multiple (LCM). The greatest
Write a program to prompt the user for two positive integers x, y and output the greatest common divisor (GCD) and the least common multiple (LCM). The greatest

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site