Introduction to programming in c include using namespace std

Introduction to programming in c++

#include <iostream>

using namespace std;

I need help on both part a and b.

bool f(int n, int a) {

if(n == 0)

return true;

else if(n > 0 && n < a)

return false;

else

return f(n-a, a);

a) What property of n is this function testing? Give some examples of n and a where it will return true, and where it will return false.

b) Write a non-recursive version of f that does the same thing.

bool f(int n, int a) {

  

Solution

Please find the answers below:

a)

Here the function f is checking whether the number n is a multiple of a or not.

Please find below some examples where the function return true:

> f(15,5)

> f(25,5)

> f(81,9)

Please find below some examples where the function return false:

> f(81,7)

> f(11,4)

> f(13,6)

b)

bool f(int n, int a) {
  
   while(true){ //iterate till either n reaches 0 or if n is non zero integer less than a
   if(n == 0)
   return true;
   else if(n > 0 && n < a)
   return false;
   else   //if n is not 0 and if it is greater than a, then reduce n by a
   n = n-a;   
   }
}

Introduction to programming in c++ #include <iostream> using namespace std; I need help on both part a and b. bool f(int n, int a) { if(n == 0) return tru
Introduction to programming in c++ #include <iostream> using namespace std; I need help on both part a and b. bool f(int n, int a) { if(n == 0) return tru

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site