Write a program to add, subtract, multiply and divide two numbers in python

In this tutorial, we are going to see how to write a program to add, subtract, multiply, and divide two numbers in C. To perform addition, subtraction, multiplication, and division of any two numbers in C programming, you must ask the user to enter these two numbers first, and then apply the operator to these two numbers to perform the mathematical operations.
 

#include <stdio.h> int main() { int nbr1, nbr2, addition, subtraction, multiplication; float division; printf("Enter two numbers: \n"); scanf("%d%d", &nbr1, &nbr2); addition = nbr1 + nbr2; subtraction = nbr1 - nbr2; multiplication = nbr1 * nbr2; division = nbr1 / (float)nbr2; //type casting printf("Addition = %d\n", addition); printf("Subtraction = %d\n", subtraction); printf("Multiplication = %d\n", multiplication); printf("Division = %.2f\n", division); return 0; }

Output:

Enter two numbers: 4 2 Addition = 6 Subtraction = 2 Multiplication = 8 Division = 2.00

Last update on May 28 2022 13:45:51 (UTC/GMT +8 hours)

Write a Python program to add, subtract, multiply and division of two complex numbers.

Sample Solution:-

Python Code:

print("Addition of two complex numbers : ",(4+3j)+(3-7j)) print("Subtraction of two complex numbers : ",(4+3j)-(3-7j)) print("Multiplication of two complex numbers : ",(4+3j)*(3-7j)) print("Division of two complex numbers : ",(4+3j)/(3-7j))

Sample Output:

Addition of two complex numbers : (7-4j) Subtraction of two complex numbers : (1+10j) Multiplication of two complex numbers : (33-19j) Division of two complex numbers : (-0.15517241379310348+0.6379310344827587j)

Pictorial Presentation:

Write a program to add, subtract, multiply and divide two numbers in python

Flowchart:

Write a program to add, subtract, multiply and divide two numbers in python

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to print a complex number and its real and imaginary parts.
Next: Write a Python program to get the length and the angle of a complex number.

What is the difficulty level of this exercise?

How do I merge two dictionaries in a single expression in Python?

For dictionaries x and y, z becomes a shallowly merged dictionary with values from y replacing those from x.

  • In Python 3.5 or greater:
  • z = {**x, **y}
  • In Python 2, (or 3.4 or lower) write a function:
  • def merge_two_dicts(x, y): z = x.copy() # start with x's keys and values z.update(y) # modifies z with y's keys and values & returns None return z

    and now:

    z = merge_two_dicts(x, y)
  • In Python 3.9.0a4 or greater (final release date approx October 2020): PEP-584, discussed here, was implemented to further simplify this:
  • z = x | y # NOTE: 3.9+ ONLY

Explanation

Say you have two dicts and you want to merge them into a new dict without altering the original dicts:

x = {'a': 1, 'b': 2} y = {'b': 3, 'c': 4}

The desired result is to get a new dictionary (z) with the values merged, and the second dict's values overwriting those from the first.

>>> z {'a': 1, 'b': 3, 'c': 4}

A new syntax for this, proposed in PEP 448 and available as of Python 3.5, is

z = {**x, **y}

And it is indeed a single expression.

Note that we can merge in with literal notation as well:

z = {**x, 'foo': 1, 'bar': 2, **y}

and now:

>>> z {'a': 1, 'b': 3, 'foo': 1, 'bar': 2, 'c': 4}

Ref: https://bit.ly/2Y8QU8e

#include <iostream>

using namespace std;

/* This function calculating additon of two numbers. */

int AdditionOfTwoNumbers(int num1int num2)

{

    /* Calculating the sum of two numbers

    and store into sum variables */

    int sum = num1 + num2;

    return sum;

}

/* This function calculating subtraction of two numbers. */

int SubtractionOfTwoNumbers(int num1int num2)

{

    /*Calculating the subtraction of two numbers

    and store into minus variables */

    int minus = num1-num2;

    return minus;

}

/* This function calculating multiplication of two numbers. */

int MultiplicationOfTwoNumbers(int num1int num2)

{

    /* Calculating the multiplication of two numbers

    and store into mul variables */

    int mul = num1 * num2;

    return mul;

}

/* This function calculating division of two numbers. */

double DivisionOfTwoNumbers(int num1int num2)

{

    /* Calculating the division of two numbers

    and store into div variables */

    double div = (double)num1/(double)num2;

    return div;

}

int main()

{

    int num1num2;

    /* Get the input of two variables */

    cout<<"Enter the Two Numbers:";

    cin>>num1>>num2;


    /* Call Function addition_two_numbers, subtraction_two_numbers,  

     multiplication_two_numbers, and division_two_numbers with two Parameters*/

    int add = AdditionOfTwoNumbers(num1num2);

    int sub = SubtractionOfTwoNumbers(num1num2);

    int multi = MultiplicationOfTwoNumbers(num1num2);

    double division = DivisionOfTwoNumbers(num1num2);


     /* Display sum, Subtraction, Multiplication, and Division of two numbers*/

    cout<<"Addition of two Numbers is: "<<add<<endl;

    cout<<"Subtraction of two Numbers is: "<<sub<<endl;

    cout<<"Multiplication of two Numbers is: "<<multi<<endl;

    cout<<"Division of two Numbers is: "<<division<<endl;

}