Program 8
Program 8
Name-NIVEDITA NAYAL
Course-B.C.A
Semester-2nd
Roll no. -86
ALOGORITHM:
Input: A string st
Output: The counts of digits, whitespaces, and characters
1. Initialize variables digit, whitespace, and characters to 0.
2. Initialize an index variable i to 0.
3. While the character at st[i] is not the null character '\0':
a. If st[i] is an alphabetical character:
- Increment the characters count.
b. If st[i] is a whitespace character:
- Increment the whitespace count.
c. If st[i] is a digit:
- Increment the digit count.
d. Increment i.
4. Output the counts of digits, whitespaces, and characters.
PROGRAM:
#include<iostream>
#include<cstring>
using namespace std;
class EMPLOYEE{
private:
string name;
int Employee_number,basic;
float DA,TA,Net_salary;
public:
void write(EMPLOYEE *ep,string name,int number, int basic,int i);
void read(EMPLOYEE *ep,int size);
};
int main(){
int size;
cout<<"Enter the number of employee ";
cin>>size;
EMPLOYEE ep[size];
for(int i=0;i<size;i++){
cin.ignore();
string Name;
int Number,basic;
cout<<"Enter the name of employee "<<i+1<<" ";
getline(cin,Name);
cout<<"Enter the employee number of employee "<<i+1<<" ";
cin>>Number;
cout<<"Enter the basic pay of employee "<<i+1<<" ";
cin>>basic;
ep[i].write(ep,Name,Number,basic,i);}
ep[0].read(ep,size);
return 0;}
void EMPLOYEE:: write(EMPLOYEE *ep,string name,int number, int basic,int i){
ep[i].name=name;
ep[i].Employee_number=number;
ep[i].basic=basic;
ep[i].DA=basic*0.10;
ep[i].TA=basic*0.05;
ep[i].Net_salary=basic+ep[i].DA+ep[i].TA;
}
void EMPLOYEE :: read(EMPLOYEE *ep,int size){
for(int i=0;i<size;i++){
cout<<"The name of Employee "<<i+1<<" is "<<ep[i].name<<endl;
cout<<"The employee number of Employee "<<i+1<<" is
"<<ep[i].Employee_number<<endl;
cout<<"The basic pay of Employee "<<i+1<<" is "<<ep[i].basic<<endl;
cout<<"The net salary of Employee "<<i+1<<" is "<<ep[i].Net_salary<<endl;
}
}
OUTPUT:
PS C:\Users\amitm\OneDrive\Desktop\New folder\ c++> cd "c:\Users\amitm\OneDrive\
Desktop\New folder\ c++\" ; if ($?) { g++ practical5.cpp -o practical5 } ; if ($?) { .\practical5
}
Enter the number of employee 2
Enter the name of employee 1 bella
Enter the employee number of employee 1 1
Enter the basic pay of employee 1 10000
Enter the name of employee 2 king
Enter the employee number of employee 2 2
Enter the basic pay of employee 2 1000
The name of Employee 1 is bella
The employee number of Employee 1 is 1
The basic pay of Employee 1 is 10000
The net salary of Employee 1 is 11500
The name of Employee 2 is king
The employee number of Employee 2 is 2
The basic pay of Employee 2 is 1000
The net salary of Employee 2 is 1150
PROGRAM-9
Name-NIVEDITA NAYAL
Course-B.C.A
Semester-2nd
Roll no. -86
OBJECTIVE:
Create a class TEACHER with data members’ salary and experience. Implement the
following:
a) Initialize using parametrized constructors.
b) Illustrate the use of the default constructor.
ALOGORITHM:
Include necessary libraries.
Declare a class Teacher with private member variables salary and experience.
Declare constructors: default and parameterized.
Define a member function print() to display salary and experience.
In the main function:
Create a Teacher object t1 using the default constructor and print its details.
Create another Teacher object t2 using the parameterized constructor and print its
details.
Define the constructors:
Default constructor initializes salary to 100000 and experience to 1.
Parameterized constructor initializes salary and experience based on the arguments
passed.
PROGRAM:
#include<iostream>
using namespace std;
class Teacher{
int salary;
int experience;
public:
Teacher();
Teacher(int a,int b);
void print(){
cout<<"The salary is "<<salary<<" and experience is "<<experience<<"year"<<endl;
}
};
int main(){
Teacher t1;
t1.print();
Teacher t2(200000,2);
t2.print();
return 0;
}
Teacher :: Teacher(){
salary=100000;
experience=1;
}
Teacher :: Teacher(int a,int b){
salary=a;
experience=b;
}
OUTPUT:
PS D:\NIVEDITA NAYAL\C++\c++ practice> cd "d:\NIVEDITA NAYAL\C++\c++
practice\" ; if ($?) { g++ practical8.cpp -o practical8 } ; if ($?) { .\practical8 }
The salary is 100000 and experience is 1year
The salary is 200000 and experience is 2year
PROGRAM-10
Name-NIVEDITA NAYAL
Course-B.C.A
Semester-2nd
Roll no. -86
OBJECTIVE:
Create two objects for a class timer. Assume three data members’ hours, minutes, and
seconds. Use two constructors for initializing the objects. Add two-time objects using
operator overloading. Display appropriate values of hours, minutes, and seconds after
addition.
ALOGORITHM:
1. Define a class timer to represent time with three private member variables: hours,
minutes, and seconds.
2. Implement constructors to initialize time, including a default constructor and a
parameterized constructor.
3. Implement a copy constructor to create a copy of a timer object.
4. Declare a friend function display() to display the time.
5. Declare an overloaded friend function operator+ to add two timer objects.
6. In the display() function, output the time in hours, minutes, and seconds.
7. In the operator+ function, add the corresponding components of two timer objects and
return a new timer object with the sum of the times.
8. In the main() function:
Prompt the user to enter time in hours, minutes, and seconds.
Create a t1 object with the entered time.
Display the time of t1.
Create a t2 object by copying t1.
Display the time of t2.
Add t1 and t2 using the overloaded + operator.
Display the sum of the times.
PROGRAM:
#include<iostream>
using namespace std;
class timer{
int hours;
int minutes;
int seconds;
public:
timer():hours(0),minutes(0),seconds(0){}
timer(int h,int m,int s):hours(h),minutes(m),seconds(s){
}
timer(const timer &t){
hours=t.hours;
minutes=t.minutes;
seconds=t.seconds;
}
friend void display(timer &t);
friend timer operator+(timer &t1,timer &t2);
};
void display(timer &t){
cout<<t.hours<<"hours "<<t.minutes<<"min "<<t.seconds<<"sec"<<endl;
}
timer operator+(timer &t1,timer &t2){
int hour,min,sec;
sec=t1.seconds+t2.seconds;
min=t1.minutes+t2.minutes+sec/60;
sec=sec%60;
hour=t1.hours+t2.hours+min/60;
min=min%60;
timer t(hour,min,sec);
return t;
}
int main(){
int x,y,z;
cout<<"Enter the time in hours "<<endl;
cin>>x;
cout<<"Enter the time in minutes "<<endl;
cin>>y;
cout<<"Enter the time in second "<<endl;
cin>>z;
timer t1(x,y,z);
display(t1);
timer t2(t1);
display(t2);
timer t3=t1+t2;
cout<<"The sum of two object is "<<endl;
display(t3);
return 0;
}
OUTPUT:
PS C:\Users\amitm\OneDrive\Desktop> cd "c:\Users\amitm\OneDrive\Desktop\" ; if ($?)
{ g++ mukulc++.cpp -o mukulc++ } ; if ($?) { .\mukulc++ }
Enter the time in hours
3
Enter the time in minutes
45
Enter the time in second
45
3hours 45min 45sec
3hours 45min 45sec
The sum of two object is
7hours 31min 30sec
PROGRAM NO. 08
NAME: NIVEDITA NAYAL
COURSE: BCA
SEMESTER: II
ROLL NO.:86
ALGORITHM:
STEP1: START.
STEP2:Define the class Teacher:
Include two data members: salary and experience.
STEP3: Implement a parameterized constructor to initialize salary and
experience.
STEP4: Implement a default constructor.
Parameterized Constructor:
Implement a constructor that takes parameters for salary and experience and
initializes the respective data members.
STEP5: Default Constructor:
Implement a default constructor that initializes salary and experience to default
values.
STEP6: Illustrate the use of the default constructor:
STEP7: Create an object of the Teacher class using the default constructor.
STEP8: Display the values of salary and experience to verify they are initialized
correctly
STEP9: STOP.
PROGRAM:
#include <iostream>
using namespace std;
class TEACHER {
private:
float salary;
int experience;
public:
// Parameterized constructor
TEACHER(float s, int exp) {
salary = s;
experience = exp;
}
// Default constructor
TEACHER() {
salary = 0;
experience = 0;
}
int main() {
float s;
int exp;
return 0;
}
OUTPUT:
C:\USERS\NIVEDITA NAYAL\ONEDRIVE\DOC
Details of Teacher 1 (default constructor):
Salary: 0
Experience: 0 years
Enter salary of Teacher 2: 50000
Enter experience of Teacher 2 (in years): 2
--------------------------------
Process exited after 19.06 seconds with return value 0
Press any key to continue . . .
PROGRAM NO. 09
NAME: NIVEDITA NAYAL
COURSE: BCA
SEMESTER: II
ROLL NO.:86
OBJECTIVE: Create two objects for a class timer. Assume three data
members’ hours, minutes, and seconds. Use two constructors for initializing
the objects. Add two-time objects using operator overloading. Display
appropriate values of hours, minutes, and seconds after addition.
ALGORITHM:
STEP1: start
STEP2: Define the class Timer:
Include three data members: hours, minutes, and seconds.
STEP3: Implement two constructors to initialize objects: one with default
values and another with parameters for hours, minutes, and seconds.
STEP4: Overload the + operator to add two Timer objects together.
Define the constructor:
Implement a constructor with default values (0 hours, 0 minutes, 0 seconds).
Implement another constructor with parameters to set hours, minutes, and
seconds.
Implement operator overloading:
STEP5: Overload the + operator to add two Timer objects together.
The addition operation should handle carry-over appropriately for seconds,
minutes, and hours.
STEP6: Display the result:
After adding two Timer objects, display the appropriate values of hours,
minutes, and seconds.
STEP 7: STOP
PROGRAM:
#include <instreams>
using namespace std;
class Timer {
private:
int hours;
int minutes;
int seconds;
public:
// Parameterized constructor
Timer(int h = 0, int m = 0, int s = 0) {
hours = h;
minutes = m;
seconds = s;
}
// Overloaded + operator to add two Timer objects
Timer operator+(const Timer& t) {
Timer sum;
sum.seconds = seconds + t.seconds;
sum.minutes = minutes + t.minutes + sum.seconds / 60;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.seconds %= 60;
sum.minutes %= 60;
return sum;
}
int main() {
int h1, m1, s1, h2, m2, s2;
return 0;
}
OUTPUT:
C:\USERS\NIVEDITA NAYAL\ONEDRIVE\DOC
Enter hours, minutes, and seconds for Timer 1 (separated by spaces): 2
12
32
Enter hours, minutes, and seconds for Timer 2 (separated by spaces): 5
16
45
PROGRAM:
#include <iostream>
#include <string>
using namespace std;
public:
// Parameterized constructor
EMPLOYEE(string n, int id) {
name = n;
ID = id;
}
public:
// Parameterized constructor
MANAGER(string n, int id, float sal) : EMPLOYEE(n, id) {
salary = sal;
}
int main() {
string name;
int ID;
float salary;
return 0;
}
OUTPUT:
C:\USERS\NIVEDITA NAYAL\ONEDRIVE\DOC
Enter name of the manager: NIVEDITA NAYAL
Enter ID of the manager: 34562
Enter salary of the manager: 800000
Manager Details:
Name: NIVEDITA NAYAL
ID: 34562
Salary: $800000
--------------------------------
Process exited after 20.29 seconds with return value 0
Press any key to continue . . .
PROGRAM NO. 11
NAME: NIVEDITA NAYAL
COURSE: BCA
SEMESTER: II
ROLL NO.:86
PROGRAM:
#include <iostream>
using namespace std;
int main() {
// Declare a pointer to int
int* ptr;
// Allocate memory for an integer using the new operator
ptr = new int;
// Check if memory allocation was successful
if (ptr == nullptr) {
cout << "Memory allocation failed!" << endl;
return 1;
}
// Assign a value to the dynamically allocated memory (optional)
*ptr = 42;
// Display the value
cout << "Value stored in dynamically allocated memory: " << *ptr << endl;
// Deallocate memory using the delete operator
delete ptr;
return 0;
}
OUTPUT:
C:\USERS\NIVEDITA NAYAL\ONEDRIVE\DOC
Value stored in dynamically allocated memory: 42
--------------------------------
Process exited after 1.139 seconds with return value 0
PROGRAM NO. 12
NAME: NIVEDITA NAYAL
COURSE: BCA
SEMESTER: II
ROLL NO.:86
STEP4: Prompt the user to enter the number of integers to be stored in the list.
STEP5: Read each integer from the user and store it in a list or array.
STEP7: Check whether the current integer is odd or even using the modulo (%)
operator.
If the integer is odd, write it to the "ODD.dat" file.
If the integer is even, write it to the "EVEN.dat" file.
Close both output file streams.
STEP8: STOP.
PROGRAM:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inputFile("input.txt"); // Open input file
ofstream oddFile("ODD.dat"); // Open ODD.dat file
ofstream evenFile("EVEN.dat"); // Open EVEN.dat file
if (!inputFile) {
cerr << "Error: Unable to open input file." << endl;
return 1;
}
if (!oddFile || !evenFile) {
cerr << "Error: Unable to create output files." << endl;
return 1;
}
int number;
while (inputFile >> number) {
if (number % 2 == 0) {
evenFile << number << endl; // Write even number to EVEN.dat
} else {
oddFile << number << endl; // Write odd number to ODD.dat
}
}
cout << "Numbers categorized and stored in files successfully." << endl;
return 0;
}
OUTPUT:
C:\USERS\NIVEDITA NAYAL\ONEDRIVE\DOC
Error: Unable to open input file.
--------------------------------
Process exited after 1.287 seconds with return value 1
Press any key to continue . . .
PROGRAM NO. 14
NAME: NIVEDITA NAYAL
COURSE: BCA
SEMESTER: II
ROLL NO.:86
OBJECTIVE: Illustrate the exception for divide overflow error for any
mathematical expression.
ALGORITHM:
STEP1: START.
STEP2: Prompt the user for input:
Ask the user to enter two integer operands for the mathematical expression.
Perform the division operation:
STEP5: Catch and handle any exceptions thrown during the division operation.
STEP6: STOP.
PROGRAM:
#include <iostream>
#include <limits>
using namespace std;
int main() {
int numerator, denominator;
double result;
try {
// Check for divide overflow
if (denominator == 0) {
throw "Divide by zero error!";
}
return 0;
}
OUTPUT:
C:\USERS\NIVEDITA NAYAL\ONEDRIVE\DOC
Enter the numerator: 10
Enter the denominator: 0
Error: Divide by zero error!
PROGRAM NO. 13
NAME: NIVEDITA NAYAL
COURSE: BCA
SEMESTER: II
ROLL NO.:86
OBJECTIVE: Create a vector for storing a list of integers using the STL.
ALGORITHM:
STEP1: Include the necessary header file:
#include <vector> to use the vector container provided by the STL.
STEP4: Ask the user to enter the number of integers they want to store in the
vector.
STEP 5: Input integers from the user and store them in the vector:
Use a loop to input each integer from the user and add it to the vector using the
push_back() method.
--------------------------------
Process exited after 12.53 seconds with return value 0
Press any key to continue . .