0% found this document useful (0 votes)
9 views5 pages

Programming Exercises All Parts

The document contains multiple C++ exercises demonstrating various programming concepts, including drawing shapes with asterisks, performing arithmetic operations, swapping numbers, and calculating the area and perimeter of geometric figures. Each exercise includes code snippets along with their corresponding outputs. The exercises cover basic programming skills suitable for beginners.

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)
9 views5 pages

Programming Exercises All Parts

The document contains multiple C++ exercises demonstrating various programming concepts, including drawing shapes with asterisks, performing arithmetic operations, swapping numbers, and calculating the area and perimeter of geometric figures. Each exercise includes code snippets along with their corresponding outputs. The exercises cover basic programming skills suitable for beginners.

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/ 5

Exercise 1 - Part (1)

#include <iostream>
using namespace std;

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

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

Exercise 1 - Part (2)


#include <iostream>
using namespace std;

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

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

Exercise 1 - Part (3)


#include <iostream>
using namespace std;
int main() {
cout << " *" << endl;
cout << " **" << endl;
cout << " * *" << endl;
cout << "* *" << endl;
cout << "****" << endl;
return 0;
}

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

Exercise 1 - Part (4)


#include <iostream>
using namespace std;

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

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

Exercise 1 - Part (5)


#include <iostream>
using namespace std;

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

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

Exercise 1 - Part (6)


#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