0% found this document useful (0 votes)
1 views6 pages

Sample_Program and Exercise

The document contains two Pascal programs: one that asks for a user's favorite hobby and another that multiplies two numbers. It provides explanations for each line of code, detailing variable declarations, user input, and output. Additionally, it suggests modifications to the programs to include asking for the user's name and age, as well as implementing division, percentage calculations, and temperature conversion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Sample_Program and Exercise

The document contains two Pascal programs: one that asks for a user's favorite hobby and another that multiplies two numbers. It provides explanations for each line of code, detailing variable declarations, user input, and output. Additionally, it suggests modifications to the programs to include asking for the user's name and age, as well as implementing division, percentage calculations, and temperature conversion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

program FavoriteHobby;

var

hobby: string;

begin

writeln('What is your favorite hobby?');

readln(hobby); { Read the user input and store it in the variable 'hobby' }

writeln;

writeln('Your favorite hobby is: ', hobby); { Display the entered hobby }

readln; { Wait for the user to press Enter before closing the program }

end.
Explanation of the Code

1. program FavoriteHobby;

o Declares the name of the program (optional but good practice).

2. var hobby: string;

o Declares a variable named hobby that can store a line of text.

3. begin ... end.

o The main block of the program. All code between begin and end. gets
executed.

4. writeln('What is your favorite hobby?');

o Prints a question asking for the user's favorite hobby.

5. readln(hobby);

o Reads the user's input and stores it in the variable hobby.

6. writeln('Your favorite hobby is: ', hobby);

o Displays the hobby that the user entered.

program MultiplyNumbers;

var

num1, num2, result: Integer;

begin

writeln('Enter the first number:');

readln(num1); { Read the first number }

writeln('Enter the second number:');

readln(num2); { Read the second number }


result := num1 * num2; { Multiply the numbers }

writeln;

writeln('The result of ', num1, ' * ', num2, ' is: ', result); { Display the result }

end.

Explanation of the Code

1. program MultiplyNumbers;
o Names the program (optional but recommended).

2. var num1, num2, result: Integer;

o Declares three integer variables:

▪ num1 and num2 for user input.

▪ result for the product.

3. begin ... end.

o Main program block where the code runs.

4. writeln and readln

o Prompt the user and read input into num1 and num2.

5. result := num1 * num2;

o Performs multiplication and stores it in result.

6. writeln('The result of ', num1, ' * ', num2, ' is: ', result);

o Displays the multiplication result.

Now try to change the code a bit to do the following

For the 1st example:

Try:

• Ask name, age, and hobby

• Say: "Hi Sarah! At 14, enjoying painting sounds fun!"

For the second program

Tre:

• Dividing numbers

• Calculating percentages

• Converting Celsius to Fahrenheit

You might also like