C program Problem A Write a function that has one integer pa

C program

Problem A: Write a function that has one integer parameter and returns 1 if the given argument is a multiple of 3 and 0 otherwise. Write a main program that tests your function with enough test cases to give you confidence in the correctness of your function. Use assertions and, optionally, calls to printf. The function prototype should be

int multipleof3(int n);

Hint: what does the % operator do?

Solution

what does the % operator do

% operator will provide you the remainder. If remainder is 0 theen value can be divisible by 3 or not.

#include <stdio.h>
#include <assert.h>

int multipleof3(int n);
int main()
{
assert(multipleof3(6) == 1);

printf(\"Result is %d\ \", multipleof3(6));

assert(multipleof3(8) == 0);

printf(\"Result is %d\ \", multipleof3(8));

assert(multipleof3(12) == 1);

printf(\"Result is %d\ \", multipleof3(12));
  

return 0;
}
int multipleof3(int n){
if( n % 3 == 0){
return 1;
}
else{
return 0;
}
}

Output:

sh-4.2$ gcc -o main *.c                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                           

Result is 1                                                                                                                                                                                                                                            

Result is 0                                                                                                                                                                                                                                            

Result is 1

C program Problem A: Write a function that has one integer parameter and returns 1 if the given argument is a multiple of 3 and 0 otherwise. Write a main progra

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site