Program to Addition, subtraction and multiplication of two numbers using function

Posted in /   /   /  

Program to Addition, subtraction and multiplication of two numbers using function
vinaykhatri

Vinay Khatri
Last updated on October 31, 2024

    In this programming tutorial, you will learn how to write a program in three different programming languages to do Addition, subtraction, and multiplication of two numbers using function. Functions are supported in every programming language, and they make it to write reusable code and make the complete program more modular.

    Addition, subtraction, and multiplication of two numbers using function

    1. C Program to do Addition, subtraction, and multiplication of two numbers using function

    #include <stdio.h>
    
    //function to add two numbers
    float add(float num1, float num2)
    {
    	return num1+num2;
    }
    
    // function to substract two numbers
    float sub(float num1, float num2)
    {
    	return num1-num2;
    }
    
    //function to multiple two numbers
    float mul(float num1, float num2)
    {
    	return num1*num2;
    }
    
    //function to divide two numbers
    float div(float num1, float num2)
    {
    	return num1/num2;
    }
    
    int main() 
    {
        char operator;
        float num1, num2;
    	//ask user to enter the operator
        printf("Select the operator (+, -, *, /): ");
        scanf("%c", &operator);
        
        //ask user to enter two numbers
        printf("Enter two numbers (eg. 12 3): ");
        scanf("%f %f",&num1, &num2);
    	
    	//start switch case
        switch(operator)
        {	//if operator is addition
            case '+':
                printf("%.1lf + %.1lf = %.1lf",num1, num2, add(num1, num2));
                break;
    
    		// if operator is substraction
            case '-':
                printf("%.1f - %.1f = %.1f",num1, num2, sub(num1,num2));
                break;
                
    		//if operator is multiplication
            case '*':
                printf("%.1f * %.1f = %.1f",num1, num2, mul(num1,num2));
                break;
    	
    		//if operator is division
            case '/':
                printf("%.1f / %.1f = %.1f",num1, num2, div(num1,num2));
                break;
    
            // if user enter a wrong operaotr
            default:
                printf("Please select the correct operator");
        }
    
    	return 0;
    }

    Output

    Select the operator (+, -, *, /): /
    Enter two numbers (eg. 12 3): 12 4
    12.0 / 4.0 = 3.0

    2. C++ Program to do Addition, subtraction, and multiplication of two numbers using function

    #include <iostream>
    using namespace std;
    
    //function to add two numbers
    float add(float num1, float num2)
    {
    	return num1+num2;
    }
    
    // function to substract two numbers
    float sub(float num1, float num2)
    {
    	return num1-num2;
    }
    
    //function to multiple two numbers
    float mul(float num1, float num2)
    {
    	return num1*num2;
    }
    
    //function to divide two numbers
    float div(float num1, float num2)
    {
    	return num1/num2;
    }
    
    int main() 
    {
        char op;
        float num1, num2;
    	//ask user to enter the operator
        cout<<"Select the operator (+, -, *, /): "; cin>>op;
        
        //ask user to enter two numbers
        cout<<"Enter two numbers (eg. 12 3): "; cin>>num1>>num2;
    	
    	//start switch case
        switch(op)
        {	//if operator is addition
            case '+':
                cout<<num1<<" + "<< num2<<" = "<<add(num1, num2);
                break;
    
    		// if operator is substraction
            case '-':
                cout<<num1<<" - "<< num2<<" = "<< sub(num1, num2);
                break;
                
    		//if operator is multiplication
            case '*':
               cout<<num1<<" * "<< num2<<" = "<< mul(num1, num2);
                break;
    	
    		//if operator is division
            case '/':
                cout<<num1<<" / "<< num2<<" = "<< div(num1, num2);
                break;
    
            // if the user enters a wrong operator
            default:
                cout<<"Please select the correct operator;
        }
    
    	return 0;
    }

    Output

    Select the operator (+, -, *, /): /
    Enter two numbers (eg. 12 3): 12 10
    12 / 10 = 1.2

    3. Python Program to do Addition, subtraction, and multiplication of two numbers using function

    # function to add two numbers
    def add(num1, num2):
        return num1+num2;
    
    # function to substract two numbers
    def sub(num1, num2):
        return num1-num2;
    
    # function to multiple two numbers
    def mul(num1, num2):
        return num1*num2;
    
    # function to divide two numbers
    def div(num1, num2):
        return num1/num2;
    
    if __name__ =="__main__":
        operator = input("Select the operator (+, -, *, /):" )
    
        print("Enter two numbers")
        num1 = float(input("Number 1: "))
        num2 = float(input("Number 2: "))
    
        if operator=="+":
            print(f"{num1} + {num2} =",round(add(num1, num2),2) )
        elif operator=="-":
            print(f"{num1} - {num2} =",round(sub(num1, num2),2) )
        elif operator=="*":
            print(f"{num1} + {num2} =",round(mul(num1, num2),2) )
        elif operator=="/":
            print(f"{num1} / {num2} =",round(div(num1, num2),2) )
        else:
             print("Please select the correct operator")

    Output

    Select the operator (+, -, *, /):/
    Enter two numbers
    Number 1: 122
    Number 2: 3
    122.0 / 3.0 = 40.67

    Conclusion

    In this tutorial, we learned how to make a simple calculator using functions or how to do Addition, Subtraction, multiplication of two numbers using function. In this article, we implement the same program in 3 different popular programming languages C, C++, and Python. If you like this article or have any suggestions please let us know by commenting down below.

    People are also reading:

    Leave a Comment on this Post

    0 Comments