0% found this document useful (0 votes)
25 views7 pages

Adilzhan Mustafin Lab1

The document is a lab journal for a System Level Programming course, detailing exercises and code examples focused on input/output statements, data types, arithmetic operations, and the const keyword in C++. It includes specific coding tasks for students to implement, such as calculating the circumference of a circle, performing basic arithmetic operations, and calculating the area of a triangle. Additionally, it emphasizes the importance of understanding variable mutability with the const keyword and provides examples of code outputs.

Uploaded by

adik804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

Adilzhan Mustafin Lab1

The document is a lab journal for a System Level Programming course, detailing exercises and code examples focused on input/output statements, data types, arithmetic operations, and the const keyword in C++. It includes specific coding tasks for students to implement, such as calculating the circumference of a circle, performing basic arithmetic operations, and calculating the area of a triangle. Additionally, it emphasizes the importance of understanding variable mutability with the const keyword and provides examples of code outputs.

Uploaded by

adik804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

System Level Programming

Lab Journal - Lab 1


Name: Adilzhan Mustafin

Group: IT2-2102

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:

Grade is :A

int marks = 80;


cout << "Marks are :" << marks << endl;

Output:

Marks are :80

int marks;
cout << "Enter marks: ";
cin >> marks;
cout << "Marks are:" << marks << endl;

Output:
Enter marks: 12
Marks are:12

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 "<<

System Level Programming Page 1


circum << endl;
Output:
Enter radius: 15
Circumference of circle with radius 15 is 94.2

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:
Circumference of Earth in inches with radius 2.508e+08 is 1.57502e+09

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?
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?
If we put const keyword before variable it bocome immutbale variable

//This is a program that increase the value of count variable by 1.


int count = 10;

count = count + 1;

cout << "Value of Count is: " << count << endl;

count += 1;

cout << "Value of Count is: " << count << endl;

System Level Programming Page 2


Compile this program and see the output.
You will notice that count += 1; is the same as count = count + 1;
So you have learned a shorter way of writing count = count + 1;
Output:-
Value of Count is: 11
Value of Count is: 12
int carsLeft = 20;

carsLeft = carsLeft - 1;

cout << "Total number of cars left is: " << carsLeft << endl;

Rewrite the above code by replacing the line number 2 by a shorter


version
Code:-

#include <iostream>
using namespace std;

int main() {
int carsLeft = 20;

--carsLeft;

cout << "Total number of cars left is: " << carsLeft << endl;

return 0;
}

2. Now Write Code for the following problems yourselves. Write code as well as the snapshot of
their output in the journal below

Problem 1:
Write a program in C++ that accepts the values of two variables num1 and num2 from the user
and
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 ______________.

System Level Programming Page 3


These dashes will have the values of num1, num2, sum, difference and product respectively.
Code:
#include <iostream>
using namespace std;
int main()
{
double num1, num2, sum, difference, product;

cout << "Enter the first number (num1): ";


cin >> num1;
cout << "Enter the second number (num2): ";
cin >> num2;

sum = num1 + num2;


difference = num1 - num2;
product = num1 * num2;

cout << "Sum of " << num1 << " and " << num2 << " is " << sum <<
"." << endl;
cout << "Difference of " << num1 << " and " << num2 << " is " <<
difference << "." << endl;
cout << "Product of " << num1 << " and " << num2 << " is " <<
product << "." << endl;

return 0;
}

Output:
#include <iostream>
using namespace std;
int main()
{
double num1, num2, sum, difference, product;

cout << "Enter the first number (num1): ";


cin >> num1;
cout << "Enter the second number (num2): ";
cin >> num2;

sum = num1 + num2;


difference = num1 - num2;
product = num1 * num2;

cout << "Sum of " << num1 << " and " << num2 << " is " << sum <<
"." << endl;
cout << "Difference of " << num1 << " and " << num2 << " is " <<
difference << "." << endl;
cout << "Product of " << num1 << " and " << num2 << " is " <<
product << "." << endl;

System Level Programming Page 4


return 0;
}

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:
#include <iostream>
using namespace std;

int main() {
// Declare variables for base and height
double base, height, area;

// Accept user input for base and height


cout << "Enter the base of the triangle: ";
cin >> base;
cout << "Enter the height of the triangle: ";
cin >> height;

// Calculate the area of the triangle


area = (base * height) / 2;

// Display the result


cout << "The area of the right-angle triangle is: " << area <<
endl;

return 0;
}

Output:
Enter the base of the triangle: 3
Enter the height of the triangle: 4
The area of the right-angle triangle is: 6

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:
#include <iostream>

System Level Programming Page 5


using namespace std;

int main() {
double radius, rounds, circumference, totalDistance;
const double pi = 3.14;
cout << "Enter the radius of the circular ground (in meters): ";
cin >> radius;
cout << "Enter the number of rounds completed: ";
cin >> rounds;

circumference = 2 * pi * radius;

totalDistance = circumference * rounds;

cout << "Total distance traveled by the person is: " <<
totalDistance << " meters." << endl;

return 0;
}

Output:
Enter the radius of the circular ground (in meters): 5
Enter the number of rounds completed: 3
Total distance traveled by the person is: 94.2 meters.

Bonus Problem: -
This task will not be marked. Just give it a try. It’s a bonus problem.

Write a program in C++ that asks the user to enter two integer numbers, stores them in
variable 'num1' and 'num2' respectively. The program swaps the values of two variables with
each other using a third variable 'temp' and displays the values of both variables after
swapping.
Code:
#include <iostream>
using namespace std;

int main() {
int num1, num2, temp;
cout << "Enter the first integer (num1): ";
cin >> num1;
cout << "Enter the second integer (num2): ";
cin >> num2;

cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2
<< endl;

temp = num1;
num1 = num2;

System Level Programming Page 6


num2 = temp;

cout << "After swapping: num1 = " << num1 << ", num2 = " << num2
<< endl;

return 0;
}

Output:
Enter the first integer (num1): 5
Enter the second integer (num2): 10
Before swapping: num1 = 5, num2 = 10
After swapping: num1 = 10, num2 = 5
+++++++++++++++++++++++++

System Level Programming Page 7

You might also like