83.6k views
2 votes
Write a C program that includes a function of type double called divemaster accepts two double arguments (you must write the divemaster function). When called, your function will return the first argument divided by the second argument. Make sure your function includes a decision statement so that it can't divide by 0--if the caller attempts to divide by 0 the function should return a value of -1.0.

User Flint
by
5.3k points

1 Answer

2 votes

Answer:

Following is the C program to divide two numbers:

#include<stdio.h>

double divemaster (double first, double second)

{

//Check if second argument is zero or not

if(second!=0)

return first/second;

else

return -1.0;

}

int main()

{

printf("%f\\", divemaster(100,20));

printf("%f\\", divemaster(100,0));

}

Output:

5.000000

-1.000000

Step-by-step explanation:

In the above program, a function divemaster is declared which divedes the first argument with second and return the result.

Within the body of divemaster function if else block is used to verify that the second argument is not zero, and thus there is no chance of divide by zero error.

In main two printf statements are used to call the divemaster function. One to check normal division process and second to check divide by zero condition.

User Mike Irving
by
5.5k points