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
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).
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...
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...
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...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
#include <stdio.h> #pragma warning(disable : 4996) // CSE 240 Fall 2016 Homework 2 Question 3 (25...
#include <stdio.h> #pragma warning(disable : 4996) // CSE 240 Fall 2016 Homework 2 Question 3 (25 points) // Note: You may notice some warnings for variables when you compile in GCC, that is okay. #define macro_1(x) ((x > 0) ? x : 0) #define macro_2(a, b) (3*a - 3*b*b + 4*a * b - a*b * 10) int function_1(int a, int b) { return (3*a - 3*b*b + 4*a * b - a*b * 10); } // Part 1: //...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
c++ 19.36 LAB: Output values below an amount - functions Write a program that first gets...
c++ 19.36 LAB: Output values below an amount - functions Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, and output all integers less than or equal to that value. Ex: If the input is: 5 50 60 140 200 75 100 the output is: 50 60 75 The 5 indicates that there are five integers...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels (integer values) that broadcast the show. For this problem you can ONLY use the following string library functions: strcpy, strlen, strcmp. You MAY not use memcpy, memset, memmove. You can assume memory allocations are successful (you do not need to check values returned by malloc nor calloc). typedef struct tv_show { char *name; int num_channels, *channels; } Tv_show; a. Implement the init_tv_show function that...
Please answer the following C question: There is a documented prototype for a function called get_a_line...
Please answer the following C question: There is a documented prototype for a function called get_a_line in the code below. Write a definition for get_a_line—the only function called from that definition should be fgetc. #include <stdio.h> #include <string.h> #define BUFFER_ARRAY_SIZE 10 int get_a_line(char *s, int size, FILE *stream); // Does what fgets does, using repeated calls to fgetc, but // provides a more useful return value than fgets does. // // REQUIRES // size > 1. // s points to...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT