Simple C++ Programs to Implement Various Control Structures.
Simple C++ Programs to Implement Various Control Structures.
structures.
A. if 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;
}
B. do while loop
#include <iostream>
using namespace std;
int main() {
int number;
do {
cout << "Enter a positive number (or 0 to exit): ";
cin >> number;
if (number > 0) {
cout << "You entered: " << number << endl;
} else if (number < 0) {
cout << "Please enter a positive number." << endl;
}
} while (number != 0);
C . Switch case
#include <iostream>
using namespace std;
int main() {
int choice;
cout << "Menu:" << endl;
cout << "1. Option 1" << endl;
cout << "2. Option 2" << endl;
cout << "3. Option 3" << endl;
cout << "Enter your choice (1-3): ";
cin >> choice;
switch (choice) {
case 1:
cout << "You selected Option 1." << endl;
break;
case 2:
cout << "You selected Option 2." << endl;
break;
case 3:
cout << "You selected Option 3." << endl;
break;
default:
cout << "you are entering an invalid choice. better luck next chance!"
<< endl;
}
return 0;
}
D. for loop
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number you want to print the numbers up to: ";
cin >> num;
return 0;
}
E . While loop
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number you want to print the numbers up to: ";
cin >> num;
int i = 1;
while(i<=num){
cout << i << " ";
i++;
}
cout << "\n";
return 0;
}
F. Array
#include <iostream>
using namespace std;
int main() {
int numbers[10];
for (int i = 0; i < 10; i++) {
numbers[i] = i + 1;
}
cout << "Numbers are: ";
for (int i = 0; i < 10; i++) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
#include <iostream>
using namespace std;
// Class Declaration
class Rectangle {
private:
double length, width;
public:
void setDimensions(double l, double w) {
length = l;
width = w;
}
double calculateArea() {
return length * width;
}
void displayArea() {
cout << "Area of the rectangle: " << calculateArea() << endl;
}
};
int main() {
Rectangle rect;
rect.setDimensions(10.5, 5.5);
rect.displayArea();
return 0;
}
// declare a class
class Wall {
private:
double length;
public:
// default constructor to initialize variable
Wall()
: length{5.5} {
cout << "Creating a wall." << endl;
cout << "Length = " << length << endl;
}
};
int main() {
Wall wall1;
return 0;
}
#include <iostream>
using namespace std;
class Wall {
private:
double length;
double height;
public:
Wall(double len, double hgt)
: length{len}
, height{hgt} {
}
double calculateArea() {
return length * height;
}
};
int main() {
Wall wall1(10.5, 8.6);
Wall wall2(8.5, 6.3);
return 0;
}
#include <iostream>
using namespace std;
class Wall {
private:
double length;
double height;
public:
Wall(double len, double hgt)
: length{len}
, height{hgt} {
}
Wall(const Wall& obj)
: length{obj.length}
, height{obj.height} {
}
double calculateArea() {
return length * height;
}
};
int main() {
Wall wall1(10.5, 8.6);
return 0;
}
class Employee {
public:
int empNumber;
string empName;
double basicSalary, DA, IT, netSalary;
void inputData() {
cout << "Enter Employee Number: ";
cin >> empNumber;
cin.ignore();
cout << "Enter Employee Name: ";
getline(cin, empName);
cout << "Enter Basic Salary: ";
cin >> basicSalary;
cout << "Enter Dearness Allowance (DA): ";
cin >> DA;
cout << "Enter Income Tax (IT): ";
cin >> IT;
}
void calculateNetSalary() {
netSalary = basicSalary + DA - IT;
}
void printData() {
cout << "\nEmployee Number: " << empNumber << endl;
cout << "Employee Name: " << empName << endl;
cout << "Basic Salary: " << basicSalary << endl;
cout << "Dearness Allowance (DA): " << DA << endl;
cout << "Income Tax (IT): " << IT << endl;
cout << "Net Salary: " << netSalary << endl;
}
};
int main() {
int N;
cout << "Enter the number of employees: ";
cin >> N;
Employee employees[N];
for (int i = 0; i < N; i++) {
cout << "\nEnter details for Employee " << i + 1 << ":\n";
employees[i].inputData();
employees[i].calculateNetSalary();
employees[i].printData();
}
return 0;
}
6 . Write a C++ program to read the N employee data and compute each employee's
Net salary (DA=52% of Basic and Income Tax (IT) =30% of the gross pay).
#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
int empNumber;
string empName;
double basicSalary, DA, IT, netSalary;
void inputData() {
cout << "Enter Employee Number: ";
cin >> empNumber;
cin.ignore();
cout << "Enter Employee Name: ";
getline(cin, empName);
cout << "Enter Basic Salary: ";
cin >> basicSalary;
cout << "Enter Dearness Allowance (DA): ";
cin >> DA;
cout << "Enter Income Tax (IT): ";
cin >> IT;
}
void calculateNetSalary() {
DA = 0.52 * basicSalary;
IT = 0.30 * basicSalary;
netSalary = basicSalary + DA - IT;
}
void printData() {
cout << "\nEmployee Number: " << empNumber << endl;
cout << "Employee Name: " << empName << endl;
cout << "Basic Salary: " << basicSalary << endl;
cout << "Dearness Allowance (DA): " << DA << endl;
cout << "Income Tax (IT): " << IT << endl;
cout << "Net Salary: " << netSalary << endl;
}
};
int main() {
int N;
cout << "Enter the number of employees: ";
cin >> N;
Employee employees[N];
for (int i = 0; i < N; i++) {
cout << "\nEnter details for Employee " << i + 1 << ":\n";
employees[i].inputData();
employees[i].calculateNetSalary();
employees[i].printData();
}
return 0;
}