0% found this document useful (0 votes)
15 views3 pages

All Exercises Programs

The document contains four exercises demonstrating basic programming concepts in C++. Exercise 1 prints a shape using asterisks, Exercise 2 performs arithmetic operations, Exercise 3 shows how to swap two numbers with and without a third variable, and Exercise 4 calculates the area and perimeter of a triangle and a rectangle. Each exercise includes code snippets and their corresponding outputs.

Uploaded by

armaashar7
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)
15 views3 pages

All Exercises Programs

The document contains four exercises demonstrating basic programming concepts in C++. Exercise 1 prints a shape using asterisks, Exercise 2 performs arithmetic operations, Exercise 3 shows how to swap two numbers with and without a third variable, and Exercise 4 calculates the area and perimeter of a triangle and a rectangle. Each exercise includes code snippets and their corresponding outputs.

Uploaded by

armaashar7
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/ 3

Exercise 1: Print Shape

#include <iostream>
using namespace std;

int main() {
cout << " * ******** * ****** ******** "
<< endl;
cout << " * * * * * * * * * "
<< endl;
cout << " * * * * * * ****** ***** "
<< endl;
cout << " * *** * * * * *** * * * * "
<< endl;
cout << "* * ******** * * ****** ******** "
<< endl;
return 0;
}

Output:
* ******** * ****** ********
* * * * * * * * *
* * * * * * ****** *****
* *** * * * * *** * * * *
* * ******** * * ****** ********

Exercise 2: Arithmetic Operations


#include <iostream>
using namespace std;

int main() {
int a = 20, b = 6;

cout << "Addition: " << a + b << endl;


cout << "Subtraction: " << a - b << endl;
cout << "Multiplication: " << a * b << endl;
cout << "Division: " << a / b << endl;
cout << "Remainder: " << a % b << endl;

return 0;
}

Output:
Addition: 26
Subtraction: 14
Multiplication: 120
Division: 3
Remainder: 2

Exercise 3: Swap Numbers


#include <iostream>
using namespace std;

int main() {
int x = 10, y = 20, temp;

// Using third variable


temp = x;
x = y;
y = temp;
cout << "After swapping using third variable: x = " << x << ", y = " << y <<
endl;

// Without third variable


x = x + y;
y = x - y;
x = x - y;
cout << "After swapping without third variable: x = " << x << ", y = " << y
<< endl;

return 0;
}

Output:
After swapping using third variable: x = 20, y = 10
After swapping without third variable: x = 10, y = 20

Exercise 4: Area & Perimeter


#include <iostream>
using namespace std;

int main() {
float base = 5, height = 10;
float a = 5, b = 6, c = 7;
float width = 8;

// Triangle
float areaTriangle = 0.5 * base * height;
float perimeterTriangle = a + b + c;
// Rectangle
float areaRectangle = width * height;
float perimeterRectangle = 2 * (width + height);

cout << "Triangle Area: " << areaTriangle << endl;


cout << "Triangle Perimeter: " << perimeterTriangle << endl;

cout << "Rectangle Area: " << areaRectangle << endl;


cout << "Rectangle Perimeter: " << perimeterRectangle << endl;

return 0;
}

Output:
Triangle Area: 25
Triangle Perimeter: 18
Rectangle Area: 80
Rectangle Perimeter: 36

You might also like