Question

In: Computer Science

Write a C++ program for Euclids Algorithm that keeps track of the number of iterations (%...

Write a C++ program for Euclids Algorithm that keeps track of the number of iterations (% & loop)

1. Euclid’s Algorithm An alternative of the Euclidean algorithm for finding greatest common divisors (GCD) is repeatedly performing the modulo operation on two numbers until the remainder is 0. Here is the pseudocode for find the GCD of two positive numbers a and b using the Euclidean algorithm :while b ≠ 0 temp = b b = a mod t a = t Create a program that asks the user for two positive integers. The program should validate that the input numbers are both positive and asks the user to reenter if needed. It then calculates the GCD using the Euclidean algorithm as described above, while keeping track of how many times the modulo operation is performed. The program outputs should include the GCD and the number of times for which the modulo operation is performed.

Solutions

Expert Solution

I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).

Images of the Code:
Note: If the below code is missing indentation please refer code Images

Typed Code:

//including required headers
#include <iostream>
using namespace std;

int main()
{
// variable to store user input
int a,b;
// prompt for a Positive Number
cout << "Enter a Positive Number :";
// reading value from user
cin >> a;
// if input value is negative
while(a<0)
{
// prompt error message
cout << "Invalid input!\n";
// prompt for a Positive Number
cout << "Enter a Positive Number :";
// reading value from user
cin >> a;
}
  
// prompt for a Positive Number
cout << "Enter a Positive Number :";
// reading value from user
cin >> b;
// if input value is negative
while(b<0)
{
// prompt error message
cout << "Invalid input!\n";
// prompt for a Positive Number
cout << "Enter a Positive Number :";
// reading value from user
cin >> b;
}
  
// A variable to store modulo operation count
int Modulo_count=0;
// A variable to store loop running count
int loop_count=0;
// calculting GCD
// if b is non zero value
while(b!=0)
{
//storing b value in temp
int temp =b;
//performing Modulo operation
b= a%temp;
// changing a value to temp
a = temp;
  
// incrementing Modulo_count
Modulo_count++;
  
// incrementing loop_count
loop_count++;
}
  
//Printing GCD
cout << "GCD : "<<a << "\n";
//Printing Modulo count
cout << "Modulo operation is performed " << Modulo_count << " times\n";
//Printing Loop Count
cout << "Loop Count is "<< loop_count << "\n";


return 0;
}
//code ended here

Output:


If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!


Related Solutions

Write C++ a program that shows a class called gamma that keeps track of how many...
Write C++ a program that shows a class called gamma that keeps track of how many objects of itself there are. Each gamma object has its own identification called ID. The ID number is set equal to total of current gamma objects when an object is created. Test you class by using the main function below. int main()    {    gamma g1;    gamma::showtotal();    gamma g2, g3;    gamma::showtotal();    g1.showid();    g2.showid();    g3.showid();    cout <<...
Write a C++ program that keeps asking user to enter a positive integer number until a...
Write a C++ program that keeps asking user to enter a positive integer number until a sentinel value (999) is entered. Then for each positive number entered by the user, find out how many digits it consists. (Hint: divide a number by 10 will remove one digit from the number. You can count how many divisions it needs to bring the number to 0.) An example of executing such a program is shown below. Note that the user input is...
Grocery List Program Write a program that keeps track of the user's grocery list items. Prompt...
Grocery List Program Write a program that keeps track of the user's grocery list items. Prompt the user if they'd like to (each action is a function): See the list Display all items (if any) in the list Add item to their list Confirm with the user before adding item (y/n or yes/no) If they enter a duplicate item, notify them the item already exists Remove items from their list Confirm with the user before removing item (y/n or yes/no)...
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
Write a program in c++ that maintains a telephone directory. The Telephone directory keeps records of...
Write a program in c++ that maintains a telephone directory. The Telephone directory keeps records of people’s names and the corresponding phone numbers. The program should read a transaction file and report the result into an output file. The transaction file can include commands such as “Add”, “Delete”, “Display”, and “Update”, and “Search”. Each command is written in one line with a few other information. The “Display” Command should simply display everyone in the directory on the screen The “Add”...
ASSIGNMENT: Write a program to keep track of the total number of bugs collected in a...
ASSIGNMENT: Write a program to keep track of the total number of bugs collected in a 7 day period. Ask the user for the number of bugs collected on each day, and using an accumulator, keep a running total of the number of bugs collected. Display the total number of bugs collected, the count of the number of days, and the average number of bugs collected every day. Create a constant for the number of days the bugs are being...
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
develop an algorithm and then a C program that accomplishes the following. determines the minimum number...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive; produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input; terminate if 0 or a negative number is entered. Here is possible example of the program running (remember...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive; produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input; terminate if 0 or a negative number is entered. Here is possible example of the program running (remember...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT