Question

LANGUAGE C++
Password Generator← 10 10.201.51. eRAD D scheduling山UitPro 6 Fizz Buzz Write a function void FizzBuzz(int n) that lists all of the numbers from 1 to n, but replace all numbers divisible by 3 but not divizible by 5 with the word Fizz, replace all numbers divisible by 5 but not divisible by 3 with the word Buzz, and replace all numbers divisible by both 3 and 5 with FizzBuzz. The output for the command FizzBuzz(20) should be as follows: Fizz Buzz Fizz Buzz Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz 7 Greatest Common Divisor Write a function int god(int a, int b) which uses the Euclidean algorithm to return the a and b. If youre not familiar with the Euclidean algorithm, google it, as you are required to use the Euclidean algorithm for this problem. You may assume that a 2 b>0 8 Prime numbers write the function tool i%Prim (int n which returns true only if it is not divisible by any numher smaller than itself and greater than I. You may safely assume that n > I if n is prime, false otherwise. A number is prime if and DELL I need help with all 3 questions. Thank you in advance

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code for 6, 7 and 8 below. Please read the comments carefully to understand the code.

6.

CODE

/////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

/*
* fizzBuzz() function takes an integer argument 'n' and performs the following operations
* 1. If a number is divisible by 15, print FizzBuzz
* 2. If a number is divisible by 3, print Fizz
* 3. If a number is divisible by 5, print Buzz
* 4. For all others, print the number
*/
void fizzBuzz(int n){
  
// Loop to traverse through all numbers between 1 and n
for(int i=1; i<=n; i++){
// if i is divisible by 15, print "FizzBuzz"
if(i % 15 == 0)
cout<<"FizzBuzz"<<endl;
// if i is divisible by 3, print "Fizz"
else if(i % 3 == 0)
cout<<"Fizz"<<endl;
// if i is divisible by 5, print "Buzz"
else if(i % 5 == 0)
cout<<"Buzz"<<endl;
// else, print the number
else
cout<<i<<endl;
}
}

int main() {
cout<<"Enter the value of n: ";
int n;
cin>>n;
//Call fizzBuzz() for n
fizzBuzz(n);
}

OUTPUT

/////////////////////////////////////////////////////////////////

nter the value of n: 20 izz Buzz Fizz izz Buzz Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz ...Program finished with exit code 0 Pr

----------------------------------------------------------------------------------------------------------------------------------------------

7.

CODE

/////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

/*
* The Euclidian GCD algorithm is given as.
* 1. If we subtract the smaller number(say b) from larger number(say a),
* GCD of a and b doesn’t change.
* 2. Hence, we keep subtracting repeatedly the smaller number from the larger number. Finally, we end up with GCD of a and b.
* 3. We can make the code easy if we use division instead of subtraction.
* 4. If we divide the larger number with the smaller number and get remainder 0, this means smaller number is the GCD.
*/

// GCD() function takes two integer arguments, a and b and fing their GCD using the above discussed logic.

int GCD (int a, int b) {
// if a is perfectly divisible by b, return b because GCD = b
if(a % b == 0)
return b;
// use the recusrsive approach
// Call GCD() recursively with b as the first argument and a % b as the second argument.
return GCD(b, a%b);
}

int main() {
cout<<"Enter the value of a and b: ";
int a, b;
cin>>a>>b;
//Call GCD() for a and b
cout<<"GCD of "<<a<<" and "<<b<<" is : "<<GCD(a, b);
}

OUTPUT

/////////////////////////////////////////////////////////////////

Enter the value of a and b: 24 36 GCD of 24 and 36 is : 12 ...Program finished with exit code 0 Press ENTER to exit console.

8.

CODE

/////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

/*
* A number is called prime if it is not divisible by any number other than 1 and itself. Some prime numbers are
* 2, 3, 5, 7, 11, 13, etc.
*/

// isPrime() function takes one integer argument n and returns true if it is prime, else false.

bool isPrime(int n) {
// Since, 2 is the only even prime number, we will first check if the number is 2.
// If n == 2, return true
if(n == 2)
return true;
// now, check if n is even. Clearly, if n is even, it is not prime as 2 is the only even prime number.
// If n is even, return false
if(n % 2 == 0)
return false;
  
// The following lines of code will be executed only when n is odd.
// Run a for loop and divide n with every odd number between 3 and n/2.
// If n is perfectly divisible by any of the odd number, then it is not prime, hence, return false.
for(int i=3; i<=n/2; i+=2) {
if(n % i == 0)
return false;
}
  
// The following line of code will be executed only when n is not 2 or even and it is not
// divisible by any odd number between 3 and n/2.
// This means that n is prime, hence, return true.
return true;
}

int main() {
cout<<"Enter the value of n: ";
int n;
cin>>n;
//Call isPrime() for n
if(isPrime(n) == true)
cout<<n<<" is a prime number.";
else
cout<<n<<" is not a prime number.";
}

OUTPUT

/////////////////////////////////////////////////////////////////

Enter the value of n: 71 71 is a prime number. ...Program finished with exit code 0 Press ENTER to exit console.

Add a comment
Know the answer?
Add Answer to:
LANGUAGE C++ I need help with all 3 questions. Thank you in advance Password Generator← 10...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • HELLO EXPERTS, I need a code for this 4 questions.... it's make me crazy now... could...

    HELLO EXPERTS, I need a code for this 4 questions.... it's make me crazy now... could you guys help me please? it's java Given two strings, word and a separator sep, return a big string made of count occurrences of the word, separated by the separator string. repeatSeparator("Word", "X", 3)- "WordXWordXWord" repeatSeparator("This", "And", 2)- "ThisAndThis" repeatSeparator("This", "And", 1)- "This"| For example: Result Test System.out.println(repeatSeparator("Pa", "Tn", 4)); PaTnPaTnPaTnPa Consider the series of numbers beginning at start and running up to but...

  • I need help with this exercise. Thank you for your help Language is C++ 6. Write...

    I need help with this exercise. Thank you for your help Language is C++ 6. Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd inte- ger is prime if it is not divisible by any odd integer less than or equal to the square root of the number.)

  • Use C programming Make sure everything works well only upload Write a program that takes an...

    Use C programming Make sure everything works well only upload Write a program that takes an integer from standard input and prints all prime numbers that are smaller than it to standard output. Recall that a prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. At the start of the program, prompt the user to input a number by printing "Input a number greater than 2:\n". If the user's input is less...

  • Prime Number Programing in C Note: The program is to be written using the bitwise operation....

    Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...

  • I need help with a project, I’ve seen many answers in C++, but i need it to be in swift, thank you!

    I need help with a project, I’ve seen many answers in C++, but i need it to be in swift, thank you! Write a program that asks the user to enter a date (e.g. July 4, 2008) and will return the day of the week that corresponds to that date. Your program should take the input in numeric form, thus the input prompt should be as follows: Please enter a date below: Enter month (1-12): Enter day (1-31) Enter year...

  • write a Matlab program ( solve the three questions). please use array and create your own...

    write a Matlab program ( solve the three questions). please use array and create your own function to check primality Question 1 : [10 points) Write a MATLAB program that will store all the first 1000 prime numbers in an array variable named arr_of_primes. Please note that a prime number is a positive integer that is bigger than or equal to 2 which is divisible only by 1 and itself. Examples of the first 8 primes are: 2, 3, 5,...

  • Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the...

    Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the console (inclusive). The identified prime numbers must be stored in an array . The inputted number should be between 0 and 1000 (inclusive). array is large enough to o Make sure your hold all the prim e numbers. Dynamic memory allocation is not required for this assignment, so you can have unused space in your array Make sure you can handle the cases of...

  • I need help making my array into a function that can called by the main function...

    I need help making my array into a function that can called by the main function using my program. This is C programming int prime (int x) { int i; i = 2; while (i < x) { if (x % i == 0) return 0; i++; } return 1; } int main (void){ int n; scanf ("%d", &n); if (n < 1) { printf ("Error: at least one data value must be provided.\n"); return 1; } int a[n]; int...

  • C programming language Purpose The purpose of this assignment is to help you to practice working...

    C programming language Purpose The purpose of this assignment is to help you to practice working with functions, arrays, and design simple algorithms Learning Outcomes ● Develop skills with multidimensional arrays ● Learn how to traverse multidimensional arrays ● Passing arrays to functions ● Develop algorithm design skills (e.g. recursion) Problem Overview Problem Overview Tic-Tac-Toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the...

  • Help. I need to write a small program that executes the following graph algorithms in any language: 1. All-Pairs Shortest Path (Floyd-Warshall). It must ask for the vertices and edges for the user to...

    Help. I need to write a small program that executes the following graph algorithms in any language: 1. All-Pairs Shortest Path (Floyd-Warshall). It must ask for the vertices and edges for the user to enter them. As an output, deploy the resulting matrix. This will be done only for directed graphs. 2. Kruskal or Prim algorithm whatever you want to do. It must ask for a graph and present it at the end. The minimum coating tree that results from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT