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

Describe problem with runtime type identification?, Describe problem with R...

Describe problem with Runtime type identification? A: The run time kind identification comes at cost of performance penalty. Compiler maintains class.

Define the recursion function in c, Define the Recursion Function in C? ...

Define the Recursion Function in C? In C, it is potential for the function to call themselves a function is describing 'recursive' if a statement within the body of a function

C program to demonstrate pointer to variable, C program to demonstrate Poin...

C program to demonstrate Pointer to variable: void p2a(int *); void main() {                 int x=10, *a,**b;                 int arr[5];                 //poin

Program with various inputs-set associative cache , 1.1 A Few Notes: 1....

1.1 A Few Notes: 1. Please test your program with various inputs prior to submission. 2. All group members must understand the entire project for interactive grading. Equal

Describe what are dynamic pointers, Question: (a) Describe what are dy...

Question: (a) Describe what are dynamic pointers. Show their memory representations diagrammatically. (b) Write short notes about pointers in arrays, paying attention on

What are the types of control structures in programming, What are the vario...

What are the various types of control structures in programming? - Primarily there are 3 types of control structures in programming: Sequence, Selection and Repetition. - Se

Described access privileges in c++? what is the default , A: In C++ the acc...

A: In C++ the access privileges are public, private and protected. The default access level assigned to members of a class is private. Private members of any class are accessible o

Program is to reverse the names stored in an array pointer, Program is to r...

Program is to reverse the names stored in an array pointer: Program is to reverse the 6 names stored in an array pointer as name[] void main()   {  clrscr();  char

C program for rotation, Normal 0 false false false EN-U...

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

Change to palindrome, A palindrome is a string that reads the same from bot...

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

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