Branching Problems

Reading Time: 8 minutes


Number Problems

Review programs in BASIC to solve simple numerical problems.

Finding Sign of Number

Write a program in BASIC 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.
INPUT "Enter a number: ", N

IF N < 0 THEN
    PRINT "Number is negative."
END IF

IF N > 0 THEN
    PRINT "Number is positive."
END IF

IF N = 0 THEN
    PRINT "Number is zero."
END IF

Determining Even or Odd

Write a program in BASIC 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.
INPUT "Enter a number: ", N

REMAINDER = N MOD 2

IF REMAINDER = 0 THEN
    PRINT "It is an even number."
ELSE
    PRINT "It is an odd number."
END IF

Determining Divisibility

Write a program in BASIC 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.
INPUT "Enter a number: ", N
INPUT "Enter a factor: ", F

REMAINDER = N MOD F

IF REMAINDER = 0 THEN
    PRINT "The number is divisible by ", F
ELSE
    PRINT "The number is not divisible by ", F
END IF

Finding Minimum

Write a program in BASIC 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
INPUT "Enter A: ", A
INPUT "Enter B: ", B

IF A < B THEN
    PRINT "Smaller number is: ", A
ELSE IF A > B THEN
    PRINT "Smaller number is: ", B
ELSE
    PRINT "The numbers are equal."
END IF

Simple Daily Problems

Solve simple real-life problems in BASIC 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 BASIC 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!
INPUT "Enter an age: ", A

IF A < 18 THEN
    PRINT "The person is minor."
    PRINT "He/She can watch cartoons!"
ELSE IF A < 60 THEN
    PRINT "The person is adult."
    PRINT "He/She can watch TV series!"
ELSE IF A >= 60 THEN
    PRINT "The person is senior."
    PRINT "He/She can watch devotionals!"
ELSE
    PRINT "Error: Age cannot be negative."
END IF

PRINT "Happy Watching!"

Determine Weekday Name

Write a program in BASIC 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.
INPUT "Enter a day number: ", D

IF D = 1 THEN
    PRINT "Sunday."
ELSE IF D = 2 THEN
    PRINT "Monday."
ELSE IF D = 3 THEN
    PRINT "Tuesday."
ELSE IF D = 4 THEN
    PRINT "Wednesday."
ELSE IF D = 5 THEN
    PRINT "Thursday."
ELSE IF D = 6 THEN
    PRINT "Friday."
ELSE IF D = 7 THEN
    PRINT "Saturday."
ELSE
    PRINT "Error: A week has only seven days."
END IF

Geometrical Problems

Solve simple geometric problems in BASIC that involves branching.

Determineing Angle Type

Write a program in BASIC 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.
INPUT "Enter an angle: ", A

IF A = 90
    PRINT "It is an right angle."
ELSE IF A < 90 THEN
    PRINT "It is an acute angle."
ELSE IF A > 90 THEN
    PRINT "It is an obtuse angle."
ELSE
    PRINT "Error: Angle cannot be negative."
END IF
Every time a user enters an angle, BASIC 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 BASIC 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.
INPUT "Enter angle A: ", A
INPUT "Enter angle B: ", B
INPUT "Enter angle C: ", C

SUM = A + B + C

IF SUM = 180 THEN
    PRINT "The angles can form a triangle."
ELSE
    PRINT "The angles cannot form a triangle."
END IF

Determining Special Angle Pairs

Write a program in BASIC 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.
INPUT "Enter angle 1: ", A
INPUT "Enter angle 2: ", B

SUM = A + B

IF (SUM = 90) or (SUM = 180) or (SUM = 360) THEN
    PRINT "The angles form special angle pairs."
ELSE
    PRINT "The angles do not form special angle pairs."
END IF

Determining Triangle Inequality

Write a program in BASIC 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.
INPUT "Enter side A: ", A
INPUT "Enter side B: ", B
INPUT "Enter side C: ", C

AB = A + B
BC = B + C
CA = C + A

IF (AB > C) AND (BC > A) AND (CA > B) THEN
    PRINT "The sides can form a triangle."
ELSE
    PRINT "The sides cannot form a triangle."
END IF

Determining Point Location

Write a program in BASIC 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.
INPUT "Enter x coordinate: ", X
INPUT "Enter y coordinate: ", Y

IF X = 0 AND Y = 0 THEN
    PRINT "Point is at origin."
ELSE IF X = 0 THEN
    PRINT "Point is in x-axis."
ELSE IF Y = 0 THEN
    PRINT "Point is in y-axis."
ELSE
    PRINT "Point is in one of the quadrant."
END IF