Pascal Program: Math & Geometry Calculations Menu
Posted on Mar 31, 2025 in Computing and Communications
PROGRAM MenuFunctionsOrProcedures;
USES crt;
VAR
B, X, R, Y: INTEGER;
Functions
FUNCTION Sum (N: INTEGER): INTEGER;
VAR
I, S: INTEGER;
BEGIN
S := 0;
FOR I := 1 TO N DO
BEGIN
S := S + I;
END;
Sum := S;
END;FUNCTION Factorial (N: INTEGER): INTEGER;
VAR
I, F: INTEGER;
BEGIN
F := 1;
IF N >= 0 THEN
BEGIN
FOR I := 1 TO N DO
BEGIN
F := F * I;
END;
END
ELSE
BEGIN
F := 0; // Factorial is not defined for negative numbers
END;
Factorial := F;
END;FUNCTION Power (Base, Exponent: INTEGER): INTEGER;
VAR
I, P: INTEGER;
BEGIN
P := 1;
IF Exponent >= 0 THEN
BEGIN
FOR I := 1 TO Exponent DO
BEGIN
P := P * Base;
END;
END
ELSE
BEGIN
// Handling negative exponent for integer power is tricky,
// usually results in real numbers. Returning 0 for simplicity.
P := 0;
END;
Power := P;
END;
Procedures
PROCEDURE Exchange (VAR Num1, Num2: INTEGER);
VAR
Temp: INTEGER;
BEGIN
Temp := Num1;
Num1 := Num2;
Num2 := Temp;
END;PROCEDURE Sequence (StartNum, EndNum: INTEGER);
VAR
I: INTEGER;
BEGIN
IF StartNum <= EndNum THEN
BEGIN
FOR I := StartNum TO EndNum DO
BEGIN
WRITE(I, ' ');
END;
END
ELSE
BEGIN
FOR I := StartNum DOWNTO EndNum DO
BEGIN
WRITE(I, ' ');
END;
END;
WriteLn;
END;PROCEDURE OddNumbers (StartNum, EndNum: INTEGER);
VAR
CurrentNum: INTEGER;
BEGIN
CurrentNum := StartNum;
REPEAT
IF NOT (CurrentNum MOD 2 = 0) THEN
BEGIN
WRITE(CurrentNum, ' ');
END;
CurrentNum := CurrentNum + 1;
UNTIL CurrentNum > EndNum;
WriteLn;
END;PROCEDURE EvenNumbers (StartNum, EndNum: INTEGER);
VAR
CurrentNum: INTEGER;
BEGIN
CurrentNum := StartNum;
REPEAT
IF (CurrentNum MOD 2 = 0) THEN
BEGIN
WRITE(CurrentNum, ' ');
END;
CurrentNum := CurrentNum + 1;
UNTIL CurrentNum > EndNum;
WriteLn;
END;PROCEDURE MultiplicationTable (Num: INTEGER);
VAR
I, Result: INTEGER;
BEGIN
WriteLn('Multiplication Table for ', Num, ':');
FOR I := 1 TO 10 DO
BEGIN
Result := Num * I;
WriteLn(Num, ' * ', I, ' = ', Result);
END;
END;PROCEDURE SquareCalculations (Side: INTEGER);
VAR
Area, Perimeter: INTEGER;
BEGIN
Area := Side * Side;
Perimeter := 4 * Side;
WriteLn('Square with side ', Side);
WriteLn('The perimeter is: ', Perimeter);
WriteLn('The area is: ', Area);
END;PROCEDURE RectangleCalculations (Base, Height: INTEGER);
VAR
Area, Perimeter: INTEGER;
BEGIN
Area := Base * Height;
Perimeter := 2 * Base + 2 * Height;
WriteLn('Rectangle with base ', Base, ' and height ', Height);
WriteLn('The perimeter is: ', Perimeter);
WriteLn('The area is: ', Area);
END;PROCEDURE TriangleArea (Base, Height: INTEGER);
VAR
Area: REAL; // Area can be non-integer
BEGIN
Area := (Base * Height) / 2.0;
WriteLn('Triangle with base ', Base, ' and height ', Height);
WriteLn('The area is: ', Area:0:2);
END;PROCEDURE CircleCalculations (Radius: INTEGER);
VAR
Area, Circumference: REAL;
PI: REAL;
BEGIN
PI := 3.14159;
Area := PI * Radius * Radius;
Circumference := 2 * PI * Radius;
WriteLn('Circle with radius ', Radius);
WriteLn('The circumference (perimeter) is: ', Circumference:0:3);
WriteLn('The area is: ', Area:0:3);
END;
Main Program
BEGIN
clrscr;
REPEAT
// Main Menu Display
WriteLn('--- MAIN MENU ---');
WriteLn('1. Sum (1 to N)');
WriteLn('2. Factorial');
WriteLn('3. Power (Base^Exponent)');
WriteLn('4. Exchange Two Numbers');
WriteLn('5. Display Number Sequence');
WriteLn('6. Display Odd Numbers in Range');
WriteLn('7. Display Even Numbers in Range');
WriteLn('8. Multiplication Table');
WriteLn('9. Calculate Areas and Perimeters');
WriteLn('0. EXIT');
WRITE('Select a menu option: ');
readln(B);
Clrscr;CASE B OF
0: BEGIN
WriteLn('Exiting program...');
END;1: BEGIN // Sum
WriteLn('--- Sum Calculation ---');
WRITE('Enter a positive integer N: ');
READLN(X);
IF X >= 1 THEN
BEGIN
R := Sum(X);
WriteLn('The sum of numbers from 1 to ', X, ' is: ', R);
END
ELSE
BEGIN
WriteLn('Please enter a positive integer.');
END;
END;2: BEGIN // Factorial
WriteLn('--- Factorial Calculation ---');
WRITE('Enter a non-negative integer N: ');
READLN(X);
R := Factorial(X);
IF R = 0 AND X < 0 THEN
WriteLn('Factorial is not defined for negative numbers.')
ELSE
WriteLn('The factorial of ', X, ' is: ', R);
END;3: BEGIN // Power
WriteLn('--- Power Calculation ---');
WRITE('Enter the base: ');
Readln(X);
WRITE('Enter the non-negative exponent: ');
Readln(Y);
IF Y >= 0 THEN
BEGIN
R := Power(X, Y);
WriteLn(X, ' raised to the power of ', Y, ' is: ', R);
END
ELSE
BEGIN
WriteLn('Exponent must be non-negative for this function.');
END;
END;4: BEGIN // Exchange
WriteLn('--- Exchange Numbers ---');
WRITE('Enter the first number: ');
Readln(X);
WRITE('Enter the second number: ');
Readln(Y);
WriteLn('Before exchange: First = ', X, ', Second = ', Y);
Exchange(X, Y);
WriteLn('After exchange: First = ', X, ', Second = ', Y);
END;5: BEGIN // Sequence
WriteLn('--- Display Number Sequence ---');
WRITE('Enter the starting number: ');
Readln(X);
WRITE('Enter the ending number: ');
Readln(Y);
Sequence(X, Y);
END;6: BEGIN // Odd Numbers
WriteLn('--- Display Odd Numbers ---');
WRITE('Enter the starting number of the range: ');
Readln(X);
WRITE('Enter the ending number of the range: ');
Readln(Y);
OddNumbers(X, Y);
END;7: BEGIN // Even Numbers
WriteLn('--- Display Even Numbers ---');
WRITE('Enter the starting number of the range: ');
Readln(X);
WRITE('Enter the ending number of the range: ');
Readln(Y);
EvenNumbers(X, Y);
END;8: BEGIN // Multiplication Table
WriteLn('--- Multiplication Table ---');
WRITE('Enter a number: ');
Readln(X);
MultiplicationTable(X);
END;9: BEGIN // Areas and Perimeters Sub-Menu
REPEAT
WriteLn('--- Areas and Perimeters ---');
WriteLn('1. Square');
WriteLn('2. Rectangle');
WriteLn('3. Triangle (Area only)');
WriteLn('4. Circle');
WriteLn('0. Back to Main Menu');
WRITE('Select a shape option: ');
Readln(B); // Reusing B for sub-menu choice
Clrscr;CASE B OF
0: BEGIN
// Exits sub-menu, loop condition will handle going back
END;1: BEGIN // Square
WriteLn('--- Square Calculations ---');
WRITE('Enter the side length: ');
READLN(X);
IF X > 0 THEN SquareCalculations(X)
ELSE WriteLn('Side must be positive.');
END;2: BEGIN // Rectangle
WriteLn('--- Rectangle Calculations ---');
WRITE('Enter the base length: ');
READLN(X);
WRITE('Enter the height: ');
READLN(Y);
IF (X > 0) AND (Y > 0) THEN RectangleCalculations(X, Y)
ELSE WriteLn('Base and height must be positive.');
END;3: BEGIN // Triangle
WriteLn('--- Triangle Area Calculation ---');
WRITE('Enter the base length: ');
READLN(X);
WRITE('Enter the height: ');
READLN(Y);
IF (X > 0) AND (Y > 0) THEN TriangleArea(X, Y)
ELSE WriteLn('Base and height must be positive.');
END;4: BEGIN // Circle
WriteLn('--- Circle Calculations ---');
WRITE('Enter the radius: ');
READLN(X);
IF X > 0 THEN CircleCalculations(X)
ELSE WriteLn('Radius must be positive.');
END;ELSE
WriteLn('Invalid shape option entered!');
WriteLn('Please enter the number of the option you want.');
END; // End Case Sub-MenuIF B <> 0 THEN
BEGIN
WriteLn;
WriteLn('Press Enter to continue...');
readln;
Clrscr;
END;UNTIL B = 0;
B := 99; // Reset B to prevent main loop from exiting if user chose 0 in sub-menu
Clrscr;
END; // End Case 9 (Sub-Menu)ELSE
WriteLn('Invalid option entered!');
WriteLn('Please enter the number of the option you want (0-9).');
END; // End Case Main MenuIF B <> 0 THEN
BEGIN
WriteLn;
WriteLn('Press Enter to return to the main menu...');
readln;
Clrscr;
END;UNTIL B = 0;readln; // Final pause before program window closes
END.