Activity - Control Flows in C Using Dev C
Activity - Control Flows in C Using Dev C
This activity will guide you through creating a program in Dev-C++ that utilizes control flow statements
such as if, else if, else, switch, while, for, and do-while loops. You will write a simple program that
simulates a basic calculator and an interactive number guess game.
1. Open Dev-C++.
Here, you'll write a C++ program that includes the following control flows:
Sample Code
#include <iostream>
using namespace std;
void calculator() {
char operation;
float num1, num2;
if (operation == '+') {
cout << "Result: " << num1 + num2 << endl;
} else if (operation == '-') {
cout << "Result: " << num1 - num2 << endl;
} else if (operation == '*') {
cout << "Result: " << num1 * num2 << endl;
} else if (operation == '/') {
if (num2 != 0)
cout << "Result: " << num1 / num2 << endl;
else
cout << "Error: Division by zero is not allowed." << endl;
} else {
cout << "Invalid operation!" << endl;
}
}
void guessGame() {
int number = 7; // Number to guess
int guess, attempts = 0;
do {
cout << "Guess the number (1-10): ";
cin >> guess;
attempts++;
if (guess < number)
cout << "Too low! Try again." << endl;
else if (guess > number)
cout << "Too high! Try again." << endl;
cout << "Congratulations! You've guessed the number in " << attempts << " attempts." << endl;
}
int main() {
int choice;
while (true) {
cout << "1. Calculator" << endl;
cout << "2. Guessing Game" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
calculator();
break;
case 2:
guessGame();
break;
case 3:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice! Please try again." << endl;
}
}
}
2. Test the calculator by choosing 1 and performing operations like addition, subtraction,
multiplication, and division.
3. Test the guessing game by choosing 2 and trying to guess the number.