Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

C example to implement functions for arithmetic operations

/*
 * math.h
 *
 *  Created on: Aug 22, 2017
 * 	Last Edited: Sept 5, 2017
 *      Author: Russell Trafford
 */

/* Your assignment is to take the math function and implement at least the following functions:
 * + Add (num1 + num2)
 * - Subtract (num1 - num2)
 * * Multiply (num1 * num2)
 * / Divide (num1 / num2)
 * % Modulus (num1 % num2)
*/

#include <stdio.h>
#ifndef MATH_H_
#define MATH_H_

int math(int num1, int num2, char Operator);

int main()
{

    int num1 = 4;
    int num2 = 2;
    char Operator = '%';
    int ret;

    ret = math(num1, num2, Operator);

    printf("Answer is : %d\n", ret);

    return 0;
}

//Part of your documentation should be listing the valid inputs and outputs for the functions you create.
int math(int num1, int num2, char Operator)
{
    int result;

    if (Operator == '+') {
        result = num1 + num2;
    }
    else if (Operator == '-') {
        result = num1 - num2;
    }
    else if (Operator == '*') {
        result = num1 * num2;
    }
    else if (Operator == '/') {
        result = num1 / num2;
    }
    else if (Operator == '%') {
        result = num1 % num2;
    }

    return result;
}

#endif /* MATH_H_ */

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.