Program to display the greatest common divisor , C/C++ Programming

Assignment Help:

Many modern cryptography algorithms require two numbers that are coprime, or sometimes referred to as relatively prime. Two numbers are coprime if their greatest common divisor is 1. A Greek mathematician named Euclid, who lived about 300 B.C. and who is known as the father of Geometry, created a very efficient algorithm for finding the greatest common divisor (gcd) of two positive integers. The gcd of two positive integers is defined as the greatest positive integer that divides both numbers without a remainder (has a remainder of zero).

Method using subtraction: Step 1 - Subtract the lesser value from the greater value. If the difference is equal to the lesser value, then the gcd is found and it is equal to the lesser value. Otherwise, if the difference is greater than the lesser value then the difference becomes the greater value, and repeat step 1. If the difference is less than the lesser value, then the lesser value becomes the greater value and the difference becomes the lesser value. Then repeat step 1. Let's do an example with 84 and 18.

gcd(84, 18) = gcd(84 - 18, 18) = gcd(66, 18)

gcd(66, 18) = gcd(66 - 18, 18) = gcd(48, 18)

gcd(48, 18) = gcd(48 - 18, 18) = gcd(30, 18)

gcd(30, 18) = gcd(30 - 18, 18) = gcd(12, 18) switch greater and lesser.

gcd(18, 12) = gcd(18 - 12, 12) = gcd(6, 12) switch again.

gcd(12, 6 = gcd(12 - 6, 6) = gcd(6, 6)

Therefore gcd(84, 18) = 6.

Assignment:

Write a C++ program to calculate and display the greatest common divisor of two positive integers. If the greatest common divisor is 1, then announce that the two integers are coprime.

Design Considerations:

Part of this assignment is to get used to programming with functions, so you are required to use the following functions, or something quite similar:

void swapIfNecessary(long & greater, long & lesser);

// Notice this function uses reference parameters. If lesser > greater,

// then swap the values. Otherwise do not swap the values.

long gcd(long val1, long val2);

// This function returns the greatest common divisor of val1 and val2. Basically, after you ask the user for the values, you will call this function from the main function and it will do all the work.

Sample output 1:

Welcome to the gcd calculator. This program will calculate the

greatest common divisor of two positive integers.

Please enter the first positive integer : 93

Please enter the second positive integer : 51

The gcd of 93 and 51 is 3.

The numbers are not coprime.

Sample output 2:

Welcome to the gcd calculator. This program will calculate the

greatest common divisor of two positive integers.

Please enter the first positive integer : 51

Please enter the second positive integer : 62

The gcd of 51 and 62 is 1.

The numbers are coprime.

Deliverables:

Please name your file a05.cpp.  Upload your program source code (a05.cpp file) as usual.  Be sure to comment your code as required, and to acknowledge any sources of help you may have received.  Your header comments should include the identification of the assignment and your name.  It should also include a comment indicating any sources of help you may have received. If there were none, the line should read:

// Sources: None.

Points will be awarded for the following attributes of your solution:

a.   If your program does not compile, you get only 3 points irrespective of what the error is.

b.   If  it compiles, I check and deduct points for the following:

i.    Your file name must be a05.cpp

ii.   Your header section must have "Sources:" line to include any sources you may have used.

iii.  The quality of your source code (Form).  In particular, pay attention to your indentation, capitalization, and so forth.  Be sure to read the class C++ style guide before working on this problem. However, if you follow the style used in the book you will be fine!

iv.  Your program must use functions.

c.   Program works as specified, and does not have the above mentioned deficits, full points will be awarded.


Related Discussions:- Program to display the greatest common divisor

FILES, My file pointer is going to the location which i specified and writi...

My file pointer is going to the location which i specified and writing the value but it is in next line so,please help me iam developing in visual studio6

Program, Data array A has data series from 1,000,000 to 1 with step size 1,...

Data array A has data series from 1,000,000 to 1 with step size 1, which is in perfect decreasing order. Data array B has data series from 1 to 1,000,000, which is in random order.

Tool for checking memeory leak, what would be the best tool for debuggingg ...

what would be the best tool for debuggingg memory leaks in c/c++ programming

I need profile and optimize a c++ library for speed, I need Profile and opt...

I need Profile and optimize a C++ library for speed Project Description: Optimize and profile an existing C++ library for speed. This library uses OpenMP and OpenCV and C+

Write a program to find the area under the curve y =, Write a program to fi...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b.   #include float start_poin

Area under curve, Write a program to find the area under the curve y = f(x)...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b.

Arrays, #question.Write a c++ program to accept and print a 1_d array

#question.Write a c++ program to accept and print a 1_d array

A program that divides the screen into n vertical bars, Write a function th...

Write a function that takes in a number n and divides the screen into n vertical bars, alternating black and white. (What should you do if someone puts in n=0 or n=-99?)

Implement a algorithm to verify if the link list , Implement a Algorithm to...

Implement a Algorithm to verify if the link list is in Ascending order? A: template bool linklist::isAscending() const{ nodeptr ptr = head; while (ptr->_next)

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd