0% found this document useful (0 votes)
4 views

computer assignment 2

The document contains three C++ programs: one for calculating the volumes of a cube, cylinder, or sphere based on user input, another for printing all prime numbers between 1 and 300, and a demonstration of the difference between 'break' and 'continue' statements in loops. Each program includes relevant code snippets and explanations of their functionality. The programs utilize control structures such as switch statements, for loops, and nested loops.

Uploaded by

na8787321
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)
4 views

computer assignment 2

The document contains three C++ programs: one for calculating the volumes of a cube, cylinder, or sphere based on user input, another for printing all prime numbers between 1 and 300, and a demonstration of the difference between 'break' and 'continue' statements in loops. Each program includes relevant code snippets and explanations of their functionality. The programs utilize control structures such as switch statements, for loops, and nested loops.

Uploaded by

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

C++ Program for Volume Menu

This program uses a switch statement and appropriate formulas to compute volumes of a cube,
cylinder, or sphere based on user choice.

#include <iostream>

#include <cmath> // For M_PI and pow()

using namespace std;

int main() {

int choice;

double side, radius, height, volume;

do {

cout << "Volume\n";

cout << "1. Cube\n";

cout << "2. Cylinder\n";

cout << "3. Sphere\n";

cout << "4. Exit\n";

cout << "Enter Your Choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter side of the cube: ";

cin >> side;

volume = pow(side, 3);

cout << "Volume of Cube = " << volume << endl;

break;
case 2:

cout << "Enter radius and height of the cylinder: ";

cin >> radius >> height;

volume = M_PI * pow(radius, 2) * height;

cout << "Volume of Cylinder = " << volume << endl;

break;

case 3:

cout << "Enter radius of the sphere: ";

cin >> radius;

volume = (4.0 / 3.0) * M_PI * pow(radius, 3);

cout << "Volume of Sphere = " << volume << endl;

break;

case 4:

cout << "Exiting the program.\n";

break;

default:

cout << "Invalid Choice! Try again.\n";

} while (choice != 4);

return 0;

}
Print All Prime Numbers Between 1 and 300

This uses a for loop and nested loops to check for prime numbers:

#include <iostream>

using namespace std;

int main() {

bool isPrime;

cout << "Prime numbers between 1 and 300 are:\n";

for (int num = 2; num <= 300; num++) {

isPrime = true;

for (int i = 2; i <= num / 2; ++i) {

if (num % i == 0) {

isPrime = false;

break;

if (isPrime)

cout << num << " ";

return 0;

}
Difference Between continue and break Statements

Statement Description Example


break
Exits loop when a condition is
Terminates the loop immediately.
met.
continue
Skips the current iteration and continues with the
Skips a number or condition.
next one.

Demonstration:

#include <iostream>

using namespace std;

int main() {

cout << "Using break:\n";

for (int i = 1; i <= 10; i++) {

if (i == 5)

break; // Loop will stop when i == 5

cout << i << " ";

cout << "\n\nUsing continue:\n";

for (int i = 1; i <= 10; i++) {

if (i == 5)

continue; // Skip the iteration when i == 5

cout << i << " ";

return 0;

Output:

Using break: Using continue:

1 2 3 4 1 2 3 4 5 6 7 8 9 10

You might also like