Solve basic linear equation Ax+B=C in Python

The Python code in this article shows you how to solve basic linear equations in the form of Ax+B=C.

Python code to solve basic linear equation Ax+B=C

Essentially, this is a fairly small program. If you take away the comments and the introduction, then you will have about 8 lines of code.

# ------------------------------------------
# Solving equations in the form of Ax+B=C
# Designed and programmed by: Alex Shaw III
# Date created:  January 2, 2023
# Last modified: January 2, 2023
# ------------------------------------------
print('*  Solving equations in the form of Ax+B=C  *')
numA = int(input('Input a number for A: '))
numB = int(input('Input a number for B: '))
numC = int(input('Input a number for C: '))
if (numA == 0):
    print('\'A\' cannot equal zero.')
    print('\nFor the equation: {}x+{}={}'.format(numA, numB, numC))
    print('\nThe result is: x =', int(((numC - numB) / numA)))

Feel free to use this code for your own purposes.


Here is the output to this program.

Output - Techronology

Developer’s note

What about Ax-B=C?

So, to solve Ax-B=C, simply change the minus (-) sign to the plus (+) sign, on line 20 in the code listing.

As a result, the formula is (numC + numB) / numA. Remember, numA cannot equal zero.

If you have any questions or comments, then send us an email at

Python page

Click on the button below to go to our Python page.

Python page

Solve basic linear equation Ax+B=C in Python - Techronology

Python is a high-level computer programming language. If you want more information on Python, then visit Python Software Foundation.


Techronology home  Code library  Reference desk