Question

C++ PROGRAM ONLY!

For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (usi

From the calling procedure: 1) Initialize: Make the initial call Ex: gcd(30, 12); Once inside the function: 2) Check the Base

OUTPUT File format - The GCD for 72 & 32 = 8 The GCD for 99 & 30 = 3 1. 3. Screen 1/0-cut and pasted to a txt file within ecl

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The C++ code implementing gcd is as follows:-

The output is stored in a file with name "results.txt"

 #include<iostream> #include<algorithm> #include<iomanip> #include<fstream> #include<vector> #include<utility> #include<conio.h> using namespace std; int gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b); } void readValues(int &a,int &b){ cout<<"\nEnter first integer: "; cin>>a; cout<<"Enter the second integer:"; cin>>b; } void outputToFile(ofstream &File,int gcdValue){ File<<gcdValue<<endl; } int main(){ ofstream File; File.open("results.txt",ios::out|ios::trunc); for(int i=1;i<=4;i++){ int a,b; readValues(a,b);// Function to read inputs int gcdValue=gcd(a,b);// Function to implement gcd outputToFile(File,gcdValue);// Function to output value } File.close(); }

The screen input/output-

Enter first integer: 72 Enter the second integer:32 Enter first integer: 99 Enter the second integer:30 Enter first integer:

The output file:-

results.txt como

Feel free to comment for any issues, do rate the answer positively

Add a comment
Know the answer?
Add Answer to:
C++ PROGRAM ONLY! For this lab you need to write a program that will read in...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g...

    Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g edi ) and gcdr , which both take two integers a, b and calculates their greatest common divisor (GCD) using the Euclidean algorithm gcdi () should do so using iteration while gcdr () should use recursion. Then write a third function, gcd(), which takes two integers a, band an optional third argument nethod which takes a charater string containing either "iterative" or "recursive", with...

  • Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers...

    Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers using a recursive function that implements Euclid's GCD algorithm as described below. Your program should greet the user "Euclid's GCD algorithm", prompt the user to input two integers, and then output the result "Euclid's Greatest Common Divisor Algorithm" GCD(M,N) = M                      (if N is 0) GCD(M,N) = GCD(N, M % N)   (if N > 0) you may assume that inputs are non-negative name your assembly...

  • In Assembly Language Please display results and write assembler code in (gcd.s) The Euclidean algorithm is...

    In Assembly Language Please display results and write assembler code in (gcd.s) 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...

  • C++ Part1: Palindrome detector Write a program that will test if some string is a palindrome...

    C++ Part1: Palindrome detector Write a program that will test if some string is a palindrome Ask the user to enter a string Pass the string to a Boolean function that uses recursion to determine if something is a palindrome or not. The function should return true if the string is a palindrome and false if the string is not a palindrome. Main displays the result ( if the string is a palindrome or not Part 2: Compute the Greatest...

  • Write a complete C++ program that at least consists of the main() function and a recursive...

    Write a complete C++ program that at least consists of the main() function and a recursive function gcd() with return value. Define function gcd() with two and only two parameters that calculates the greatest common divider of two given parameters. Hint: use the difference between two parameters or the remainder obtained using one parameter divide the other. In main() Read 2 positive integers with proper prompt. Call gcd() with proper syntax. Display the result, i.e. the greatest common divider of...

  • Spring 2020 CSCI 2450: HW5 (Programming Assignment (10 points) Answer any ONE of the following problems....

    Spring 2020 CSCI 2450: HW5 (Programming Assignment (10 points) Answer any ONE of the following problems. If you answer both of them, you will get bonus points. Use any instructions and Irvine procedures we covered till now. These problems might require instructions from chapter 07. Problem 1: Greatest Common Divisor ( GCD) Greatest Common Divisor (GCD): The greatest common divisor (GCD) of two integers is the largest integer that will evenly divide both integers. The GCD algorithm involves integer division...

  • IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd)...

    IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd) of two positive integers. gcd is the largest integer that divides evenly into both of them. For example, the gcd(102, 68) = 34. You may recall learning about the greatest common divisor when you learned to reduce fractions. For example, we can simplify 68/102 to 2/3 by dividing both numerator and denominator by 34, their gcd. Finding the gcd of huge numbers is an...

  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

  • C++ I do not understand this problem. 14THE GREATEST COMMON DENOMINATOR PROBLEM Write a function named...

    C++ I do not understand this problem. 14THE GREATEST COMMON DENOMINATOR PROBLEM Write a function named god() that accepts two integers as input parameters and returns the greatest common divisor of the two numbers. The greatest common divisor (GCD) of two integers, a and b, is the largest integer that is a factor of both a and b. The GCD of any number and 1 is 1, and the GCD of any number and o is that number. One efficient...

  • C++ Using Recursion We are going to create a (looping) menu that accesses functions that use...

    C++ Using Recursion We are going to create a (looping) menu that accesses functions that use recursion. The function headers and a description will be provided. You are responsible for defining the functions. Ensure that they each contain a base case they reach that doesn’t have a recursive function call, otherwise it’s an infinite loop! Functions must be implemented with recursion. int Factorial(int arg) Returns arg! (4! Is 4 * 3 * 2 = 24) Base case is Factorial(1) returning...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT