0% found this document useful (0 votes)
27 views2 pages

Activity - Control Flows in C Using Dev C

Uploaded by

Ma RA
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)
27 views2 pages

Activity - Control Flows in C Using Dev C

Uploaded by

Ma RA
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/ 2

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.

Step 1: Setup Dev-C++

1. Open Dev-C++.

2. Create a new project:

o File > New > Project > Console Application.

o Choose C++ as the language.

3. Save the project.

Step 2: Write the Program

Here, you'll write a C++ program that includes the following control flows:

1. If-else statements for calculator operations.

2. Switch-case for choosing between different functionalities.

3. While and for loops for repeated tasks.

4. Do-while loop for a guessing game

Sample Code

#include <iostream>
using namespace std;

void calculator() {
char operation;
float num1, num2;

cout << "Enter an operation (+, -, *, /): ";


cin >> operation;
cout << "Enter two numbers: ";
cin >> 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;

} while (guess != number);

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;
}
}
}

Step 3: Compile and Run the Program

1. Press F9 to compile and run the program.

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.

4. You can exit the program by choosing option 3.

You might also like