0% found this document useful (0 votes)
36 views4 pages

Lab 2

The document contains several C++ programming experiments demonstrating basic concepts like variables, input/output, arithmetic operators, constants, conditional statements, and more.

Uploaded by

bilealabebe5
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)
36 views4 pages

Lab 2

The document contains several C++ programming experiments demonstrating basic concepts like variables, input/output, arithmetic operators, constants, conditional statements, and more.

Uploaded by

bilealabebe5
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/ 4

Experiment 1

int main() {

int intValue = 10;

float floatValue = 3.14;

char charValue = 'A';

std::string stringValue = "Hello, World!";

std::cout << "Integer value: " << intValue << std::endl;

std::cout << "Floating-point value: " << floatValue << std::endl;

std::cout << "Character value: " << charValue << std::endl;

std::cout << "String value: " << stringValue << std::endl;

return 0;

Experiment 2:

#include <iostream>

const double PI = 3.14159;

int main() {
double radius;
double area;

std::cout << "Enter the radius of the circle: ";


std::cin >> radius;

area = PI * radius * radius; // Calculation of circle area

std::cout << "The area of the circle is: " << area << std::endl;

return 0;
}

Self test practice

1.
#include <iostream>

const double CELSIUS_TO_FAHRENHEIT = 9.0 / 5.0; // Declaration of constant conversion factor

int main() {
double celsiusTemperature;
double fahrenheitTemperature;

std::cout << "Enter the temperature in Celsius: ";


std::cin >> celsiusTemperature;

fahrenheitTemperature = (celsiusTemperature * CELSIUS_TO_FAHRENHEIT) + 32.0; // Conversion from


Celsius to Fahrenheit

std::cout << "The temperature in Fahrenheit is: " << fahrenheitTemperature << std::endl;

return 0;
}

2.

#include <iostream>

int main() {
int diceRoll = 1 + rand() % 6; // Simulate a dice roll between 1 and 6

std::cout << "The dice rolled: " << diceRoll << std::endl;

return 0;
}

Experiment 3

#include <iostream>

int main() {
int num1 = 10;
int num2 = 5;
int addition = num1 + num2;
int subtraction = num1 - num2;
int multiplication = num1 * num2;
int division = num1 / num2;
int modulus = num1 % num2;

std::cout << "Addition: " << addition << std::endl;


std::cout << "Subtraction: " << subtraction << std::endl;
std::cout << "Multiplication: " << multiplication << std::endl;
std::cout << "Division: " << division << std::endl;
std::cout << "Modulus: " << modulus << std::endl;

return 0;
}

Experiment 4

#include <iostream>

int main() {
int result1 = 5 + 10 * 2;
int result2 = (5 + 10) * 2;

std::cout << "Result 1: " << result1 << std::endl;


std::cout << "Result 2: " << result2 << std::endl;

return 0;
}

Self test exercise

#include <iostream>

int main() {
int n = 1, p = 2, q = 3;

bool result1 = n <= p + q && n >= p - q || n == 0;


int result2 = ++n * q-- / ++p - q;
int result3 = n | p & q ^ p << 2 + q;
int result4 = p < q ? (n < p ? q * n - 2 : q / n + 1) : q - n;

std::cout << "Result 1: " << result1 << std::endl;


std::cout << "Result 2: " << result2 << std::endl;
std::cout << "Result 3: " << result3 << std::endl;
std::cout << "Result 4: " << result4 << std::endl;

return 0;
}

You might also like