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

Write a c program to input a floating point number, Step 1 Define the start...

Step 1 Define the start of the program    It should be noted that within C all commands should end in a semi-colon. For most of your programs the definition of a program header as

How many levels deep can include files be nested, How many levels deep can ...

How many levels deep can include files be nested? - As such, there is no limit to number of levels of nested include files you can have however your compiler might run out of s

C++ Data Structure program, Description A long time ago in a galaxy far, f...

Description A long time ago in a galaxy far, far away, the country Mafghanistan had n cities and m old roads, where each road connected a pair of cities. Due to the treacherous mo

Lexicographic permutation, Given an integer n and a permutation of numbers ...

Given an integer n and a permutation of numbers 1, 2 ... , n-1, n write a program to print the permutation that lexicographically precedes the given input permutation. If the given

Data structure, how to convert a general tree into binary tree with example...

how to convert a general tree into binary tree with example

Program to access file system, The program must use the file named on the c...

The program must use the file named on the command line as its input file. Suppose the program is named pdbtool. Then if the user types pdbtool 1A36.pdb the program must read the d

Find out the largest torque, Find out the largest torque: A flat belt...

Find out the largest torque: A flat belt is taken in use to transmit torque from pulley A to pulley B as shown in figure given below. The radius of each pulley is 50mm and co

Result system, write a promgram using object oriented peramid

write a promgram using object oriented peramid

Explain the scope resolution operator, The Scope Resolution Operator( :: ) ...

The Scope Resolution Operator( :: ) Global variables are explained outside any functions and thus can be used by all the functions defined thereafter. However, if a global vari

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