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

C++ Lab Manual

The document contains several C++ programming exercises focused on control flow statements, including if-else statements, nested if-else, switch statements, loops (for, while, do-while), and the use of break and continue. Each program demonstrates a specific control flow concept with example code and prompts for user input. These exercises are designed to help learners understand and practice fundamental programming constructs in C++.

Uploaded by

Shivali Gahlot
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)
2 views4 pages

C++ Lab Manual

The document contains several C++ programming exercises focused on control flow statements, including if-else statements, nested if-else, switch statements, loops (for, while, do-while), and the use of break and continue. Each program demonstrates a specific control flow concept with example code and prompts for user input. These exercises are designed to help learners understand and practice fundamental programming constructs in C++.

Uploaded by

Shivali Gahlot
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

Programming exercises on control flow statements in C++

Program 1: Basic If-Else Statement

#include <iostream>
using namespace std;

int main() {
int number;
cout << "Enter an integer: ";
cin >> number;

if (number > 0)
cout << "The number is positive." << endl;
else if (number < 0)
cout << "The number is negative." << endl;
else
cout << "The number is zero." << endl;

return 0;
}

Program 2: Nested If-Else

#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three integers: ";
cin >> a >> b >> c;

if (a >= b && a >= c)


cout << "The largest number is: " << a << endl;
else if (b >= a && b >= c)
cout << "The largest number is: " << b << endl;
else
cout << "The largest number is: " << c << endl;

return 0;
}

Program 3: Switch Statement

#include <iostream>
using namespace std;

int main() {
char op;
float num1, num2;
cout << "Enter operator (+, -, *, /): ";
cin >> op;
cout << "Enter two operands: ";
cin >> num1 >> num2;

switch(op) {
case '+':
cout << num1 + num2 << endl;
break;
case '-':
cout << num1 - num2 << endl;
break;
case '*':
cout << num1 * num2 << endl;
break;
case '/':
if (num2 != 0)
cout << num1 / num2 << endl;
else
cout << "Division by zero error!" << endl;
break;
default:
cout << "Invalid operator!" << endl;
}

return 0;
}

Program 4: For Loop

#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter a number: ";
cin >> num;

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


cout << num << " * " << i << " = " << num * i << endl;
}

return 0;
}

Program 5: While Loop

#include <iostream>
using namespace std;

int main() {
int num, sum = 0;

cout << "Enter numbers (enter a negative number to stop): " << endl;
while (true) {
cin >> num;
if (num < 0)
break;
sum += num;
}

cout << "Sum of entered numbers is: " << sum << endl;
return 0;
}

Program 6: Do-While Loop


#include <iostream>
using namespace std;

int main() {
int choice, number, factorial;

do {
cout << "Menu:" << endl;
cout << "1. Factorial of a number" << endl;
cout << "2. Sum of first N natural numbers" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter a number: ";
cin >> number;
factorial = 1;
for (int i = 1; i <= number; ++i) {
factorial *= i;
}
cout << "Factorial of " << number << " is " << factorial << endl;
break;
case 2:
cout << "Enter a number: ";
cin >> number;
cout << "Sum of first " << number << " natural numbers is " << (number * (number + 1)) / 2
<< endl;
break;
case 3:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
} while (choice != 3);

return 0;
}

Program 7: Break and Continue

#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 50; ++i) {
if (i % 3 == 0)
continue;
if (i == 47)
break;
cout << i << " ";
}

return 0;
}

You might also like