Branching Problems

Reading Time: 8 minutes


Number Problems

Review programs in Python to solve simple numerical problems.

Finding Sign of Number

Write a program in Python that takes input from user a number and then prints its sign or zero.

Example 1
----------
Enter a number: 1430
Number is positive.

Example 2
----------
Enter a number: -4862
Number is negative.

Example 3
----------
Enter a number: 0
Number is zero.
n = int(input('Enter a number: '))

if n < 0:
    print('Number is negative.')

if n > 0:
    print('Number is positive.')

if n == 0:
    print('Number is zero.')

Determining Even or Odd

Write a program in Python that takes input from user a number and then prints if the number is even or odd.

Enter a number: 16796
It is an even number.
n = int(input('Enter a number: '))

remainder = n % 2

if remainder == 0:
    print('It is an even number.')
else:
    print('It is an odd number.')

Determining Divisibility

Write a program in Python that takes a number N and a factor F from user and determines if N is divisible by F.

Example 1
----------
Enter a number: 42
Enter a factor: 7
The number is divisible by 7.

Example 2
----------
Enter a number: 54
Enter a factor: 8
The number is divisible not by 8.
n = int(input('Enter a number: '))
f = int(input('Enter a factor: '))

remainder = n % f

if remainder == 0:
    print('The number is divisible by ', f)
else:
    print('The number is not divisible by ', f)

Finding Minimum

Write a program in Python that takes input from user two numbers in variables A, B and then prints the variable name that contains the smaller value.

Enter A: 4862
Enter B: 1430

Smaller number is: 1430
a = int(input('Enter A: '))
b = int(input('Enter B: '))

if a < b:
    print('Smaller number is: ', a)
elif a > b:
    print('Smaller number is: ', b)
else:
    print('The numbers are equal.')

Simple Daily Problems

Solve simple real-life problems in Python that involves branching.

Determining Citizen Class

You are asked to develop a program in your complex so that people residing there can only watch the common public TV serials only based on his/her age.

Write a program in Python that takes an age A from user and decides if the person is a minor, adult or senior citizen and then prints the programs they can watch.

AgeClassCan Watch
$00-17$MinorCartoons
$18-59$AdultTV Series
$60$ and aboveSeniorDevotionals
Example 1
----------
Enter an age: 85
The person is senior.
He/She can watch devotionals!
Happy Watching!

Example 2
----------
Enter an age: 27
The person is adult.
He/She can watch TV series!
Happy Watching!

Example 3
----------
Enter an age: 12
The person is minor.
He/She can watch cartoons!
Happy Watching!

Example 4
----------
Enter an age: -30
Error: Age cannot be negative.
Happy Watching!
a = int(input('Enter an age: '))

if a < 18:
    print('The person is minor.')
    print('He/She can watch cartoons!.')
elif a < 60:
    print('The person is adult.')
    print('He/She can watch TV series.')
elif a >= 60:
    print('The person is senior.')
    print('He/She can watch devotionals.')
else:
    print('Error: Age cannot be negative.')

print('Happy Watching!')

Determine Weekday Name

Write a program in Python that takes a weekday number D from user and prints the weekday name.

\[ \begin{aligned} 1 &= \text{Sunday} \\ 2 &= \text{Monday} \\ &\cdots \\ 7 &= \text{Saturday} \\ \end{aligned} \]

Example 1
----------
Enter day number: 5
Thursday.

Example 2
----------
Enter day number: 2
Monday.

Example 3
----------
Enter day number: 8
Error: A week has only seven days.
d = int(input("Enter a day number: "))

if d == 1:
    print("Sunday.")
elif d == 2:
    print('Monday.')
elif d == 3:
    print('Tuesday.')
elif d == 4:
    print('Wednesday.')
elif d == 5:
    print('Thursday.')
elif d == 6:
    print('Friday.')
elif d == 7:
    print('Saturday.')
else:
    print('Error: A week has only seven days.')

Geometrical Problems

Solve simple geometric problems in Python that involves branching.

Determineing Angle Type

Write a program in Python that takes an angle A from user and decides if the angle is acute, obtuse or right angle.

Example 1
----------
Enter an angle: 45
It is an acute angle.

Example 2
----------
Enter an angle: 90
It is an right angle.

Example 3
----------
Enter an angle: 135
It is an obtuse angle.

Example 3
----------
Enter an angle: -270
Error: Angle cannot be negative.
a = int(input('Enter an angle: '))

if a == 90:
    print('It is an right angle.')
elif a < 90:
    print('It is an acute angle.')
elif a > 90:
    print('It is an obtuse angle.')
else:
    print('Error: Angle cannot be negative.')
Every time a user enters an angle, Python decides which PRINT should it execute based on IF instruction.

  • If a user enters 90, then the first PRINT instruction is executed.
  • If a user enters a value less than 90, then the second PRINT instruction is executed.
  • If a user enters a value greater than 90, then the third PRINT instruction is executed.
  • If neither value matches, then user entered a negative number, so fourth PRINT instruction is executed.
[Got it. Thanks!]

Determining Triangle Validity

Write a program in Python that takes three angles A, B and C from user and determines if they can form a triangle.

\[ \text{Three angles can form a triangle if and only if they sum up to exactly 180}\degree \]

Example 1
----------
Enter angle A: 45
Enter angle B: 45
Enter angle C: 90
The angles can form a triangle.

Example 2
----------
Enter angle A: 45
Enter angle B: 60
Enter angle C: 30
The angles cannot form a triangle.
a = int(input('Enter angle A: '))
b = int(input('Enter angle B: '))
c = int(input('Enter angle C: '))

sum = a + b + c

if sum == 180:
    print('The angles can form a triangle.')
else:
    print('The angles cannot form a triangle.')

Determining Special Angle Pairs

Write a program in Python that takes two angles A and B from user and determines if they are special or not.

\[ \text{Two angles are complementary if they sum up to exactly 90}\degree \\ \text{Two angles are supplementary if they sum up to exactly 180}\degree \]

Example 1
----------
Enter angle 1: 135
Enter angle 2: 45
The angles form special angle pairs.

Example 2
----------
Enter angle 1: 60
Enter angle 2: 30
The angles form special angle pairs.

Example 3
----------
Enter angle 1: 50
Enter angle 2: 85
The angles do not form special angle pairs.
a = int(input('Enter angle 1: '))
b = int(input('Enter angle 2: '))

sum = a + b

if (sum == 90) or (sum == 180) or (sum == 360):
    print('The angles form special angle pairs.')
else:
    print('The angles do not form special angle pairs.')

Determining Triangle Inequality

Write a program in Python that takes three sides A, B and C from user and determines if they can form a triangle. The sum of the lengths of any two sides of a triangle is greater than the length of the third side

\[ \text{Three sides can form a triangle if and only if the sum of two sides is greater than the third side} \]

Example 1
----------
Enter side A: 3
Enter side B: 4
Enter side C: 5
The sides can form a triangle.

Example 2
----------
Enter angle A: 4
Enter angle B: 3
Enter angle C: 10
The sides cannot form a triangle.
a = int(input('Enter side A: '))
b = int(input('Enter side B: '))
c = int(input('Enter side C: '))

ab = a + b
bc = b + c
ca = c + a

if (ab > c) and (bc > a) and (ca > b):
    print('The sides can form a triangle.')
else:
    print('The sides cannot form a triangle.')

Determining Point Location

Write a program in Python that takes coordinates X and Y from user and determines the location of point in origin, x-axis, y-axis, or any of the quadrant.

Example 1
----------
Enter x coordinate: 0
Enter y coordinate: 0
Point is at origin.

Example 2
----------
Enter x coordinate: 5
Enter y coordinate: 0
Point is in x-axis.

Example 3
----------
Enter x coordinate: 0
Enter y coordinate: 5
Point is in y-axis.

Example 4
----------
Enter x coordinate: 5
Enter y coordinate: 5
Point is in one of the quadrant.
x = int(input('Enter x coordinate: '))
y = int(input('Enter y coordinate: '))

if x == 0 and y == 0:
    print('Point is at origin.')
elif x == 0:
    print('Point is in x-axis')
elif y == 0:
    print('Point is in y-axis')
else:
    print("Point is in one of the quadrant.")