Adilzhan Mustafin Lab1
Adilzhan Mustafin Lab1
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.
Output:
Grade is :A
Output:
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;
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.
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
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
count = count + 1;
cout << "Value of Count is: " << count << endl;
count += 1;
cout << "Value of Count is: " << count << endl;
carsLeft = carsLeft - 1;
cout << "Total number of cars left is: " << carsLeft << endl;
#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.
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 << "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;
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;
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>
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;
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;
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
+++++++++++++++++++++++++