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
In assembler code must be a gcd.s file So I here is the C code I...
In assembler code must be a gcd.s file So I here is the C code I have written originally for the program. My question is how do I convert it to assembly code? // C code below #include //this function swaps given integers void swap(int*a,int*b) {     int temp=*a;     *a=*b;     *b=temp; } //this function returns gcd of a and b int gcd(int a,int b) {     if(a     swap(&a,&b);     int r=a%b;     if(r==0)     return b;     return gcd(b,r); } int main() {     int first,second;     printf("Enter...
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).
q7.3 Fix the errors in the code (in C) //This program uses a function called CharacterScan...
q7.3 Fix the errors in the code (in C) //This program uses a function called CharacterScan to read a char from the user //The function must take an int pointer as a parameter //The program should print the char and ascii code for each character the user enters //The program should only exit whe nthe user enters escape #include <stdio.h> char CharacterScan(int*); int main(void){ while(1){ int aCode; int* iPtr; char* c = CharacterScan(iPtr); if(aCode) break; else printf("%c is ASCII code...
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...
Description: The purpose of this assignment is to practice writing code that calls functions, and contains...
Description: The purpose of this assignment is to practice writing code that calls functions, and contains loops and branches. You will create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. General Comments: Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement an...
Write some C++ program segments that solves the indicated tasks (you do not have to write...
Write some C++ program segments that solves the indicated tasks (you do not have to write a complete program, nor be concerned about "good" output; a small code segment will be enough). a) A program that gets a double number from the user, decides whether that number is positive, negative, or zero and display its decision on the screen. a) A function isPositive that takes as input a double number and returns the integer 1 if the number is positive,...
I am to create three different versions of the following C program code below that implements...
I am to create three different versions of the following C program code below that implements the conversion of unsigned binary numbers into decimal (Base 2 to Base 10 conversion). Version 1: Complete the C program ”bin2dec ver1.c” that implements binary to decimal conversion. The maximum number of binary bits is 32. The program is made of the functions ”unsigned binary to decimal(const char *str)”and ”main”. The parameter ”str” passed to this function points to a C string comprising only...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT