Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Simple C++ Programs to Implement Various Control Structures.

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Simple C++ Programs to Implement Various Control Structures.

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Simple C++ programs to implement various control

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

cout << ".Goodbye!" << endl;


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

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


cout << i << " ";
}
cout << "\n";

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

Write a program Illustrating class declaration, definition, and Accessing Class


Members.

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

3 . Write a C++ Program to illustrate the default constructor, parameterized


constructor, and copy constructors.
C++ Default Constructor:
#include <iostream>
using namespace std;

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

→ C++ Parameterized Constructor:

#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);

cout << "Area of Wall 1: " << wall1.calculateArea() << endl;


cout << "Area of Wall 2: " << wall2.calculateArea();

return 0;
}

→ C++ Copy Constructor:

#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);

Wall wall2 = wall1;


cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();

return 0;
}

4. WAP to find the largest of three numbers using inline function.


#include <iostream>
using namespace std;
inline int cmp(int x,int y,int z)
{
if(x>y&&x>z)
return(x);
else if(y>z)
return(y);
else
return(z);
}
int main()
{
int a,b,c;
cout<<"enter three numbers:"<<endl;
cin>>a>>b>>c;
cout<<cmp(a,b,c)<<" is larger"<<endl;
return 0;
}
5 . Given that an EMPLOYEE class contains the following members: data members: Employee
number, Employee name, Basic, DA, IT, Net Salary, and print data members. Write a C++
program to read the data of N employees and compute the Net salary of each.
#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() {
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;
}

You might also like