Question

C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3...

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;
}

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char **argv) { const int BUF_LEN = 128; char str[BUF_LEN]; int i; char c; int is_binary; int d, n; /* Get the user to enter a string */ printf("Please enter a string made of 0s and 1s, finishing the entry by pressing Enter.\n"); for (i=0; i<BUF_LEN-1; i++) { scanf("%c", &c); if (c == '\n') { break; } str[i] = c; } str[i] = '\0'; /*...
ARM assembly Code The Euclidean algorithm is a way to find the greatest common divisor of...
ARM assembly Code The Euclidean algorithm is a way to find the greatest common divisor of two positive integers, a and b. First let me show the computations for a=210 and b=45. Divide 210 by 45, and get the result 4 with remainder 30, so 210=4·45+30. Divide 45 by 30, and get the result 1 with remainder 15, so 45=1·30+15. Divide 30 by 15, and get the result 2 with remainder 0, so 30=2·15+0. The greatest common divisor of 210...
Write Java program Lab42.java which takes as input two positive integers m and n and computes...
Write Java program Lab42.java which takes as input two positive integers m and n and computes their least common multiple by calling method lcm(m,n), which in turn calls recursive method gcd(m,n) computing the greatest common divisor of m and n. Recall, that lcm(m,n) can be defined as quotient of m * n and gcd(m,n).
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input....
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input. The first integer indicates the number of values that will follow. Read that many values, and return their sum, ignoring any additional values that may follow. However, if there are fewer integers than specified in the input, print "Error" and terminate. Hint: int n, success; ... success = scanf("%d", &n); // FIRST INTEGER INPUT reads an integer from stdin, returning 1 if the integer...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h>...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h> #define SIZE 5 int main(int argc, char *argv[]) {    int numerator = 25;    int denominator = 10;    int i = 0;    /*    You can assume the files opened correctly and the    correct number of of command-line arguments were    entered.    */    FILE * inPut = fopen(argv[1], "r");    FILE * outPut = fopen(argv[2], "w");    float result =...
Using Windows 32 framework , write an assembly language program; Write a recursive procedure to find...
Using Windows 32 framework , write an assembly language program; Write a recursive procedure to find the greatest common divisor of two non negative numbers. You should use Euclidean algorithm and this is typically discussed in CSC 230. Your procedure: ❼ Needs to follow cdecl protocol. ❼ Needs to take two parameters. ❼ Should return -1, if the parameters are negative. ❼ Should return gcd, if the parameters are non negative. Your main procedure should do the followings. ❼ Read...
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens...
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens the file for reading Reads an integer from the file and returns it. Create a main function that Asks the user to input a file Passes that file to the “ReadFile” function Prints the returned integer to the screen Extra Credit: Set a default argument of “intfile.txt” for the “ReadFile” function Write an overloaded function of “ReadFile” that accepts two filenames and reads an...
would someone kindly convert the following C code to java? thanks so much # include <stdio.h>...
would someone kindly convert the following C code to java? thanks so much # include <stdio.h> int main(void) { int changeOwed; int check; char invisibleChar; int count = 0; int numQ=0, numD=0, numN=0, numP=0; do{ printf("How much change is owed (in cents)?\n"); check = scanf("%d", &changeOwed); // returns 1 if the input is a number and returns 0 otherwise //Get's rid of any extra invisible characters if the input is a char/String do{ scanf("%c",&invisibleChar); }while(invisibleChar != '\n'); }while(check == 0...
Java Create a new Drive class. * Create a Menu class. * The menu class will...
Java Create a new Drive class. * Create a Menu class. * The menu class will use the previous 4 classes you created in GCD, LCM, FACTORIAL, DIGITS The Menu will display a menu that give you the option of selecting: 1) Greatest Common Denominator 2) Lowest Common Multiple 3) Factorial 4) Number of Digits in an Integer Enter 1,2,3 or 4: When the User enter the choice, then the correct function/method is called for the class and asks the...
C program fractions.c that does the following: 1. The program starts by making the user enter...
C program fractions.c that does the following: 1. The program starts by making the user enter a non-negative integer number a, called the first numerator. If the user enters a negative number, the program repeats the entry. 2. The program then makes the user enter a positive integer number b, called the first denominator. If the user enters a non-positive number, the program repeats the entry. 3. The program then makes the user enter a non-negative integer number c, called...