In: Computer Science
** Pascal ** Write a simple program in Pascal with these specifications:
1. Calculates real root of number entered by user (if root happens to be a complex number, print message "The root is complex").
2. The program keeps asking for input, user can input a key to terminate program. (e.g: Press 1 to enter calculator, Press any other key to exit calculator).
Program with comments to explain each step:
Program RealRootCalculator; {Name of
Program}
var choice: integer; {declaring an integer type variable to
store choice to enter and exit the program}
var number: real; {root of this variable will be
calculated}
begin
writeln('Press 1 to enter into the real root calculator');
read(choice);
writeln('Choice = ', choice);
if choice =1 then {if user enters 1, program will enable
user to calculate real roots else exit}
begin
writeln('Welcome to real root calculator');
repeat {program keep on calculating roots untill choice is 1}
writeln('Enter the number : ');
read(number);
if number < 1 then writeln('Imaginary roots') {when -ve
number is entered, program will print imaginary
roots}
else writeln('Root of', number:0:2,' :', sqrt(number):0:2);
writeln('Enter 1 to continue or any other key to exit');
read(choice);
until choice <> 1 {loop will be terminated when this
condition, choice not equal to 1 becomes true, if user keep
entering 1 then program keeps running and will not
exit}
end;
writeln('exited from calculator');
end.
=========================
Output
==========================
Press 1 to enter into the real root calculator : 1 Welcome to real root calculator Enter the number : 3 Root of 3.00 :1.73 Enter 1 to continue or any other key to exit : 1 Enter the number : Root of 5.00 :2.24 Enter 1 to continue or any other key to exit : 1 Enter the number : -5 Imaginary roots Enter 1 to continue or any other key to exit : 5 exited from calculator