Thursday, March 16, 2023
BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 3 - Write a C++ Program to read an integer number and find the sum of all digits until it reduces to a single digit using constructor, destructor and inline member functions
Saturday, March 11, 2023
BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 5 - Write a C++ Program to create a class STRING. Write a member function to initialize, get and display strings. Overload the operators ++ and == to concatenate two Strings and to compare two strings respectively.
Bharathiar University
Programming in C++ LabC++ Program 5Write a C++ Program to create a class STRING. Write a member function to initialize, get and display strings. Overload the operators ++ and == to concatenate two Strings and to compare two strings respectively.
In this page
- C++ Source Code for Visual Studio Code
- C++ Source Code for MS Dos Turbo C++
FOR VISUAL STUDIO CODE
Source code#include <iostream> #include<string.h>#include<stdio.h>using namespace std; class STRING { public: string s; void initialize() { s=""; } void get() { cin >> s; } void operator+(STRING ob) { s.append(" "+ob.s); display(); } void operator==(STRING ob) { if (s.compare(ob.s)==0) cout<<"Given Strings are equal"<<endl; else cout<<"Strings are not equal"<<endl; } void display() { cout<<"Concatenated String: "<<s<<endl; } }; int main() { STRING ob1,ob2; ob1.initialize(); ob2.initialize(); cout << " Enter the first string: "; ob1.get(); cout << "\n Enter the second string: "; ob2.get(); ob1==ob2; ob1+ob2; return 0; }
OUTPUT 1Enter the first string: hello
Enter the second string: worldStrings are not equalConcatenated String: hello world
OUTPUT 2 Enter the first string: hello
Enter the second string: helloGiven Strings are equalConcatenated String: hello hello
SOURCE CODE FOR TUBO C++(MS DOS)
#include <iostream.h>
#include <string.h>
#include<stdlib.h>
#include<conio.h>
class STRING
{
public:
char s[100];
void
initialize()
{
}
void get()
{
cin >> s;
}
void
operator+(STRING ob)
{
strcat(s,ob.s);
display();
}
void
operator==(STRING ob)
{
if (strcmp(s,ob.s)==0)
cout<<"Given Strings are
equal"<<endl;
else
cout<<"Strings are not
equal"<<endl;
}
void display() {
cout<<"Concatenated String:
"<<s<<endl;
}
};
int main()
{
clrscr();
STRING ob1,ob2;
ob1.initialize();
ob2.initialize();
cout <<
" Enter the first string: ";
ob1.get();
cout <<
"\n Enter the second string: ";
ob2.get();
ob1==ob2;
ob1+ob2;
getch();
return 0;
}
Sample output
#include <string.h>
#include<stdlib.h>
#include<conio.h>
class STRING
{
public:
char s[100];
void
initialize()
{
void get()
{
cin >> s;
void
operator+(STRING ob)
{
strcat(s,ob.s);
display();
void
operator==(STRING ob)
{
if (strcmp(s,ob.s)==0)
cout<<"Given Strings are
equal"<<endl;
else
void display() {
cout<<"Concatenated String:
"<<s<<endl;
}
};
int main()
{
clrscr();
STRING ob1,ob2;
ob2.initialize();
cout <<
" Enter the first string: ";
ob1.get();
cout <<
"\n Enter the second string: ";
ob2.get();
ob1==ob2;
ob1+ob2;
getch();
return 0;
}
Sample output
Tuesday, March 7, 2023
BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 2 - C++ Program to Write a C++ Program to create a class ARITHMETIC which consists of a FLOAT and an INTEGER variable. Write member functions ADD(), SUB(), MUL(), DIV() to perform addition, subtraction, multiplication and division respectively. Write a member function to get and display values.
Bharathiar University
Programming in C++ LabC++ Program 2Write a C++ Program to create a class ARITHMETIC which consists of a FLOAT and an INTEGER variable. Write member functions ADD(), SUB(), MUL(), DIV() to perform addition, subtraction, multiplication and division respectively. Write a member function to get and display values.
In this page
- C++ Source Code for Visual Studio Code
- C++ Source Code for MS Dos Turbo C++
FOR VISUAL STUDIO CODE#include<iostream>#include<iomanip>using namespace std;class Arithmetic{ public: int a,b,ans; float q;
void getvalues() { cout<<"Enter two numbers:"; cin>>a>>b; } void display(int result) { cout<<result<<endl; } void displayFloat(float result) { cout<<setprecision(3)<<result<<endl; } int add() { ans=a+b; return ans;
} int sub() { ans=a-b; return ans;
} int mul() { ans=a*b; return ans;
} float div() { q=a/b; return q; }
};
int main(){ class Arithmetic arith; int r; float f; arith.getvalues(); r=arith.add(); cout<<"Addition:"; arith.display(r); r=arith.sub(); cout<<"Subtraction:"; arith.display(r); r=arith.mul(); cout<<"Multiplication:"; arith.display(r); f=arith.div(); cout<<"Division:"; arith.displayFloat(f); return 0;}
OUTPUT
Enter two numbers:209Addition:29Subtraction:11Multiplication:180Division:2.22
SOURCE CODE FOR TUBO C++(MS DOS)#include<iostream.h>
#include <iomanip.h>
#include<conio.h>
class Arithmetic
{
public:
int a,b,ans;
float q;
void getvalues()
{
cout<<"Enter
two numbers:";
cin>>a>>b;
}
void display(int
result)
{
cout<<result<<endl;
}
void
displayFloat(float result)
{
cout<<setprecision(3)<<result<<endl;
}
int add()
{
ans=a+b;
return
ans;
}
int sub()
{
ans=a-b;
return
ans;
}
int mul()
{
ans=a*b;
return
ans;
}
float div()
{
q=(float)a/b;
return
q;
}
};
int main()
{
class Arithmetic
arith;
int r;
float f;
clrscr();
arith.getvalues();
r=arith.add();
cout<<"Addition:";
arith.display(r);
r=arith.sub();
cout<<"Subtraction:";
arith.display(r);
r=arith.mul();
cout<<"Multiplication:";
arith.display(r);
f=arith.div();
cout<<"Division:";
arith.displayFloat(f);
getch();
return 0;
}
OUTPUT
#include<iostream.h>
#include <iomanip.h>
#include<conio.h>
class Arithmetic
{
public:
int a,b,ans;
float q;
void getvalues()
{
cout<<"Enter
two numbers:";
cin>>a>>b;
}
void display(int
result)
{
cout<<result<<endl;
}
void
displayFloat(float result)
{
cout<<setprecision(3)<<result<<endl;
}
int add()
{
ans=a+b;
return
ans;
}
int sub()
{
ans=a-b;
return
ans;
}
int mul()
{
ans=a*b;
return
ans;
}
float div()
{
q=(float)a/b;
return
q;
}
};
int main()
{
class Arithmetic
arith;
int r;
float f;
clrscr();
arith.getvalues();
r=arith.add();
cout<<"Addition:";
arith.display(r);
r=arith.sub();
cout<<"Subtraction:";
arith.display(r);
r=arith.mul();
cout<<"Multiplication:";
arith.display(r);
f=arith.div();
cout<<"Division:";
arith.displayFloat(f);
getch();
return 0;
}
Thursday, March 2, 2023
BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 12 - C++ Program to merge two files into single file
Bharathiar University
Programming in C++ LabC++ Program 12Write a C++ Program to Merge Two Files into Single File
In this Page:
- C++ Source Code for Visual Studio Code
- C++ Source Code for MS Dos Turbo C++
FOR VISUAL STUDIO CODE
/* C++ Program to Merge Two Files into a Single file */
#include<iostream>#include<fstream>#include<stdio.h>#include<stdlib.h>using namespace std;
int main(){ ifstream fp1, fp2; ofstream fp3;
char ch, fname1[100], fname2[100], fname3[100];
cout<<"Enter first file name with extension: "; cin>>fname1; cout<<"\nEnter second file name with extension: "; cin>>fname2; cout<<"\nEnter target file with extension to copy: "; cin>>fname3;
fp1.open(fname1); fp2.open(fname2); fp3.open(fname3);
if(!fp1 || !fp2 || !fp3) { perror("\nError Message in file opening "); exit(EXIT_FAILURE); }
while(!fp1.eof()) { fp1>>noskipws>>ch; fp3<<ch; }
while(!fp2.eof()) { fp2>>noskipws>>ch; fp3<<ch; }
cout<<"\nThe two files were merged into "<<fname3<<" file successfully....!!\n";
fp1.close(); fp2.close(); fp3.close();
return 0;}
OUTPUT:
Enter first file name with extension: sample1.txt
Enter second file name with extension: sample2.txt
Enter target file with extension to copy: output.txt
The two files were merged into output.txt file successfully....!!
FOR TURBO C++
/* C++ Program to
Merge Two Files into a Single file */
#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
ifstream
fp1, fp2;
ofstream
fp3;
clrscr();
char
ch, fname1[100], fname2[100], fname3[100];
cout<<"Enter
first file name with extension :: ";
cin>>fname1;
cout<<"\nEnter
second file name with extension :: ";
cin>>fname2;
cout<<"\nEnter
third file with extension to copy :: ";
cin>>fname3;
fp1.open(fname1);
fp2.open(fname2);
fp3.open(fname3);
if(!fp1
|| !fp2 || !fp3)
{
perror("\nError
Message in file opening ");
exit(EXIT_FAILURE);
}
while(fp1.eof()==0)
{
fp1>>ch;
fp3<<ch;
}
while(fp2.eof()==0)
{
fp2>>ch;
fp3<<ch;
}
cout<<"\nThe
two files were merged into "<<fname3<<" file
successfully....!!\n";
fp1.close();
fp2.close();
fp3.close();
getch();
return
0;
}
OUTPUT:
/* C++ Program to
Merge Two Files into a Single file */
#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
ifstream
fp1, fp2;
ofstream
fp3;
clrscr();
char
ch, fname1[100], fname2[100], fname3[100];
cout<<"Enter
first file name with extension :: ";
cin>>fname1;
cout<<"\nEnter
second file name with extension :: ";
cin>>fname2;
cout<<"\nEnter
third file with extension to copy :: ";
cin>>fname3;
fp1.open(fname1);
fp2.open(fname2);
fp3.open(fname3);
if(!fp1
|| !fp2 || !fp3)
{
perror("\nError
Message in file opening ");
exit(EXIT_FAILURE);
}
while(fp1.eof()==0)
{
fp1>>ch;
fp3<<ch;
}
while(fp2.eof()==0)
{
fp2>>ch;
fp3<<ch;
}
cout<<"\nThe
two files were merged into "<<fname3<<" file
successfully....!!\n";
fp1.close();
fp2.close();
fp3.close();
getch();
return
0;
}
Thursday, February 16, 2023
BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 1 - C++ Program to implement stack data structure
Bharathiar University
Programming in C++ LabC++ Program 1Write a C++ Program to create a class to implement the data structure STACK. Write a constructor to initialize the TOP of the STACK. Write a member function PUSH() to insert an element and member function POP() to delete an element check for overflow and underflow.
In this page
- C++ Source Code for Visual Studio Code
- C++ Source Code for MS Dos Turbo C++
FOR VISUAL STUDIO CODE
#include<iostream>using namespace std; #define MAX 1000 //max size for stackclass Stack{ public: int top; int myStack[MAX]; //stack array Stack() { top = -1; } bool push(int x); int pop(); void viewStack();}; //pushes element on to the stack bool Stack::push(int item) { if (top >= (MAX-1)) { cout << "Stack Overflow!!!"; return false; }else { myStack[++top] = item; cout<<item<<endl; return true; }} //removes or pops elements out of the stackint Stack::pop(){ if (top < 0) { cout << "Stack Underflow!!"; return 0; }else { int item = myStack[top--]; return item; }}
void Stack::viewStack(){ if(top<0) { cout<<"Stack has no elements\n"; } else { for(int i=top;i>=0;i--) cout<<"| "<<myStack[i]<<" |\n------\n"; }}
// main program to demonstrate stack functionsint main(){ class Stack st; int item,ch=0; do { cout<<"1.PUSH\n"; cout<<"2.POP\n"; cout<<"3.VIEW STACK\n"; cout<<"4.Quit\n"; cout<<"Enter your choice:"; cin>>ch; switch(ch) { case 1: cout<<"PUSH the Item into Stack\n"; cout<<"Enter the item:"; cin>>item; st.push(item); break; case 2: cout<<"POP the Item from Stack\n"; item=st.pop(); cout<<"Item "<<item<<" is deleted\n"; break; case 3: cout<<"STACK\n"; st.viewStack(); break; case 4: cout<<"Thank You"; break; default: cout<<"Please enter Correct choice\n"; break; } }while(ch!=4); return 0;}
OUTPUT
1.PUSH2.POP3.VIEW STACK4.QuitEnter your choice:1PUSH the Item into StackEnter the item:30301.PUSH2.POP3.VIEW STACK4.QuitEnter your choice:3STACK
| 30 |------| 20 |------| 10 |------1.PUSH2.POP3.VIEW STACK4.QuitEnter your choice:2POP the Item from StackItem 30 is deleted1.PUSH2.POP3.VIEW STACK4.QuitEnter your choice:3STACK| 20 |------| 10 |------1.PUSH2.POP3.VIEW STACK4.QuitEnter your choice:4Thank You
SOURCE CODE FOR TUBO C++(MS DOS)
#include<iostream.h>#include<stdio.h>#include<conio.h>
#define MAX 1000class Stack{ public: int top; int myStack[MAX];
Stack() { top = -1; } void push(int x); int pop(); void viewStack();}; //pushes element on to the stack void Stack::push(int x) { if (top >= (MAX-1)) { cout << "Stack Overflow!!!";
}else { myStack[++top] = x; cout<<"Item "<<x<<"is pushed into stack"<<endl; }}
//removes or pops elements out of the stackint Stack::pop(){ if (top < 0) { cout << "Stack Underflow!!"; return 0; }else { int x = myStack[top--]; return x; }}
void Stack::viewStack(){ if(top<0) { cout<<"Stack has no elements\n"; } else { for(int i=top;i>=0;i--) cout<<"| "<<myStack[i]<<" |\n------\n"; }}
// main program to demonstrate stack functionsint main(){ class Stack st; int x,ch=0; clrscr(); do { cout<<"1.PUSH\n"; cout<<"2.POP\n"; cout<<"3.VIEW STACK\n"; cout<<"4.Quit\n"; cout<<"Enter your choice:"; cin>>ch;
switch(ch) { case 1: cout<<"PUSH the Item into Stack\n"; cout<<"Enter the item:"; cin>>x; st.push(x); break; case 2: cout<<"POP the Item from Stack\n"; x=st.pop(); cout<<"Item "<<x<<" is deleted\n"; break; case 3: cout<<"STACK\n"; st.viewStack(); break; case 4: cout<<"Thank You"; break; default: cout<<"Please enter Correct choice\n"; break; } }while(ch!=4); getch(); return 0;}
SAMPLE OUTPUT
Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files
BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files SOURCE CODE...
-
Write a C Program to Find Sum, Average and Standard Deviation for a Given Set of Values Source Code: #include<stdio.h> #include<co...
-
Bharathiar University Programming Java Lab Java Program 1 Write a Java Applications to extract a portion of a character string and print t...
-
Bharathiar University Programming Java Lab Java Program 2 Write a Java Program to implement the concept of multiple inheritance using Inte...




