C CODE PLZ! All instructions are in sections of code
#include <stdio.h> /* TODO: Define 3 functions input, gcd and lcm in such a way that the main function below compiles correctly and has the correct behavior. The input function prompts the user to enter a non-negative integer. If the user enters a negative integer, the function prints a "sorry" statement and prompts the user again. It keeps on prompting until the user enters a non-negative number. The input function returns that non-negative integer. The gcd function takes two integers in parameter and does the following: * If one of the integers is zero, it sets the variable g to 1. * Otherwise, it uses Euclid's algorithm to compute the greatest common divisor of these two numbers. It puts that greatest common divisor into g. * It ends by returning g. The lcm function takes two integers in parameter, computes the least common multiple of these two integers and returns that result. The lcm function may use the gcd function. See Wikipedia at https://en.wikipedia.org/wiki/Least_common_multiple for an explanation what the least common multiple of two numbers is. CAUTION: AS-IS, THIS BOILERPLATE SOURCE CODE FILE DOES NOT COMPILE. */ int main(int argc, char **argv) { int a, b; a = input(); b = input(); printf("The greatest common divisor of %d and %d is %d.\n", a, b, gcd(a, b)); printf("The least common multiple of %d and %d is %d.\n", a, b, lcm(a, b)); return 0; }
Please look at my code and in case of indentation issues check the screenshots.
-------------main.c---------------
#include <stdio.h>
/*
The input function prompts the user to enter a non-negative
integer. If the user enters a negative integer, the function
prints
a "sorry" statement and prompts the user again. It keeps on
prompting until the user enters a non-negative number. The
input
function returns that non-negative integer.
*/
int input()
{
int n;
printf("Enter a non-negative integer: ");
scanf("%d", &n);
while(n < 0)
{
printf("Sorry! It is negative.");
printf("\nEnter a non-negative integer: ");
scanf("%d", &n);
}
return n;
}
/*
The gcd function takes two integers in parameter and does the
following:
*If one of the integers is zero, it sets the variable g to
1.
*Otherwise, it uses Euclid's algorithm to compute the
greatest
common divisor of these two numbers. It puts that greatest
common
divisor into g.
*It ends by returning g.
*/
int gcd(int a, int b)
{
int g;
if (a == 0 || b == 0) //If one of the integers is zero, it sets the
variable g to 1.
g = 1;
else{
while (a != b) //Euclid’s Algorithm
{
if (a > b)
a = a - b;
else
b = b - a;
}
g = a;
}
return g;
}
/*
The lcm function takes two integers in parameter, computes
the
least common multiple of these two integers and returns that
result. The lcm function may use the gcd function.
*/
int lcm(int a, int b)
{
int lcm = (a*b)/gcd(a,b);
return lcm;
}
int main(int argc, char **argv)
{
int a, b;
a = input();
b = input();
printf("The greatest common divisor of %d and %d is
%d.\n", a, b, gcd(a, b));
printf("The least common multiple of %d and %d is
%d.\n", a, b, lcm(a, b));
return 0;
}
--------------Screenshots--------------
--------------------Output-----------------------
---------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.
Thankyou
Get Answers For Free
Most questions answered within 1 hours.