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

Program to calculate the average sales for each employee , There are 2...

There are 25 employees in a business. Each employee has 5 sales each day of the month (Assume there are 30 days in each month of the year). Draw a flowchart for a pr

Determine the total resistance of the circuit, For the circuit shown in Fig...

For the circuit shown in Figure, verify (a) the battery voltage V , (b) the total resistance of the circuit, and (c) the values of resistance of resistors R 1 , R 2 and

Pointers to members, P o i n t er s to Members: In C a pointer is...

P o i n t er s to Members: In C a pointer is created  to locate  the address of another variable  for faster access. Consider the following example. int x, *p; p =

C program to display the contents of file on a screen, Program is to displa...

Program is to display the contents of file on a screen: Write a program to display the contents of file on a screen void main()   {  clrscr();  ifstream fin("ascii

#psuedocode, Create a pseudocode in getting Calendar Quarter. The program s...

Create a pseudocode in getting Calendar Quarter. The program should identify which quarter falls the given date. Note: Consider the date format DDMMYYYY.

Search property from catalogue , Implement the search property from catalo...

Implement the search property from catalogue menu option.  After selecting this option the user should be asked to specify the property using the following sub-menu: 1. Specif

Setup and source code with nice user interface, Virtual Webcam effects - Se...

Virtual Webcam effects - Setup and Source code with Nice User Interface Project Description: I want a similar webcam effects application which can apply effects to webcam fee

Is there present a way to force new to allocate memory, Yes. "Memory pools"...

Yes. "Memory pools" are useful in many situations. The bad news is that I'll need to drag you through the mire of how it acts before we talk about all the uses. Firstly, recall

Program, Define a class polynomial with three private data members a, b and...

Define a class polynomial with three private data members a, b and c of type double to represent the coefficient of two degree polynomial(ax^2+bx+c). Include a constructor in a pol

C++, #questAt a shop of marbles, packs of marbles are prepared. Packets are...

#questAt a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets wit

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