Lab 2 Computer Programming Lab Journal, Exercises of Computer Programming

A lab journal for computer programming. It covers topics such as input output statements, data types, arithmetic operators, and the const keyword. code examples and exercises for the reader to practice. The lab journal also includes a bonus problem for the reader to attempt. suitable for students studying computer programming.

Typology: Exercises

2022/2023

Available from 05/01/2023

wajahat-ali-tariq
wajahat-ali-tariq 🇵🇰

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab 2
Computer Programming
Lab Journal - Lab 2
Objective:
1) Getting Familiar with input output statements.
2) Understanding different data types.
3) Understanding the use of arithmetic operators and performing calculations
4) Getting familiar with the const keyword
1. Write the following codes in your compiler, execute them and Write the output.
char grade = ‘A’;
cout << "Grade is :" << grade << endl;
Output:
int marks = 80;
cout << "Marks are :" << marks << endl;
Output:
int marks;
cout << "Enter marks: ";
cin >> marks;
cout << "Marks are:" << marks << endl;
Output:
float radius;
float pi = 3.14;
float circum;
cout << "Enter radius: ";
cin >> radius;
circum = 2 * pi * radius;
cout << "Circumference of circle with radius " << radius << " is "<< circum
<< endl;
Fundamentals of Programming Page 1
pf3
pf4
pf5

Partial preview of the text

Download Lab 2 Computer Programming Lab Journal and more Exercises Computer Programming in PDF only on Docsity!

Computer Programming

Lab Journal - Lab 2

Objective:

1) Getting Familiar with input output statements.

2) Understanding different data types.

3) Understanding the use of arithmetic operators and performing calculations

4) Getting familiar with the const keyword

1. Write the following codes in your compiler, execute them and Write the output.

char grade = ‘A’; cout << "Grade is :" << grade << endl; Output: int marks = 80; cout << "Marks are :" << marks << endl; Output: int marks; cout << "Enter marks: "; cin >> marks; cout << "Marks are:" << marks << endl; Output: float radius; float pi = 3.14; float circum; cout << "Enter radius: "; cin >> radius; circum = 2 * pi * radius; cout << "Circumference of circle with radius " << radius << " is "<< circum << endl;

Output: This code has two errors. Solve them First before writing the output. Hint: You can build the program and see the ErrorList Window to see the errors. Or you can also see the red squigly line in your compiler underlining the point of error. double radius = 250800000; //in inches const float pi = 3.14; double circumOfEarth; circum = 2 * pi * radius; cout << "Circumference of Earth in inches with radius " << radius << " is "<< circum << endl; Output: const float pi = 3.14; cout << "Enter value of pi: "; cin >> pi; cout << "Value of pi is "<< pi << endl; Build this program. You will see that it has an error on the input line of the program. Now remove the keyword const before the variable pi and build now. Does it still has an error? NO So you see the values of const variables (who has the word const before them) can not be changed/modified in the program. They are constant! What did you learn about const keyword? The word constant only declares const not variables //This is a program that increase the value of count variable by 1. int count = 10; count = count + 1;

i- Add them and store the result in a third variable sum.

ii- Subtract them and store the result in a fourth variable difference.

iii- Multiply them and store the result in a fifth variable product.

Display the output of this program in the following format:

Sum of ____________ and ____________ is _______________.

Difference of ____________ and ____________ is ____________.

Product of ____________ and ____________ is ______________.

These dashes will have the values of num1, num2, sum, difference and product respectively.

Code: Output:

Problem 2:

Write a program in C++ that accepts the base and height of a right-angle triangle from the user and

displays the area of the triangle.

(Hint: Formula for area of right angle triangle = (base*height)/2)

Code: Output:

Problem 3: A person is running in a circular ground. Write a program in C++ that asks the user to input

the radius of the ground in meters and the number of rounds the person completes. The program

should display the total distance travelled by the person in meters.

(Hint: Formula for distance = circumference*rounds)

Note: You can see the formula for circumference in the codes provided to you in Task 1.

Code: