Friday, March 31, 2023

BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 11-Write a C++ Program to create a File and to display the contents of that file with line numbers.

 

Bharathiar University

Programming in C++ Lab
C++ Program 11
Write a C++ Program to create a File and to display the contents of that file with line numbers.

In this page

  • C++ Source Code for MS Dos Turbo C++
  • C++ Source Code for Visual studio code
Source code for Turbo C++
#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
    char fname[30], str[100];
    fstream fp;
    int i=0;
    clrscr();
    cout<<"Enter the Name of File: ";
    gets(fname);
    fp.open(fname, fstream::out);
    if(!fp)
    {
cout<<"\nError Occurred!";
    return 0;
    }
    cout<<"Enter the Data: ";
    gets(str);
    while(strlen(str)>0)
    {
fp<<str;
fp<<'\n';
gets(str);
    }
    fp.close();
    fp.open(fname, fstream::in);
    if(!fp)
    {
cout<<"\nError Occurred!";
return 0;
    }
    cout<<"\nContent of "<<fname<<":-\n";
    fp.getline(str, 1000);
    while(strlen(str)>0)
   {
      
     cout<<++i<<".";
     cout << str << endl;
     fp.getline(str, 1000);
   
   }
    fp.close();
    getch();
    return 0;
}

Output:


Output.txt file





Source code for Visual studio code

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    char fname[30], str[50],ch;
    fstream fp;
    int i=0;
    cout<<"Enter the Name of File: ";
    gets(fname);
    fp.open(fname, fstream::out);
    if(!fp)
    {
        cout<<"\nError Occurred!";
        return 0;
    }
    cout<<"Enter the Data: ";
    gets(str);
    while(strlen(str)>0)
    {
        fp<<str;
        fp<<"\n";
        gets(str);
    }
    fp.close();
    fp.open(fname, fstream::in);
    if(!fp)
    {
        cout<<"\nError Occurred!";
        return 0;
    }
     cout<<"\nContent of "<<fname<<":-\n";
    fp.getline(str, 1000);
    while(strlen(str)>0)
   {
     
     cout<<++i<<".";
     cout << str << endl;
     fp.getline(str, 1000);
    
   }
   
    fp.close();
    cout<<endl;
    return 0;
}

Output:

test.txt file

Monday, March 27, 2023

BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 10- Write a C++ Program to check whether the given string is a palindrome or not using pointers

 

Bharathiar University

Programming in C++ Lab
C++ Program 10
Write a C++ Program to check whether the given string is a palindrome or not using pointers

In this page

  • C++ Source Code for MS Dos Turbo C++
Source code for Turbo C++

#include <iostream.h>
#include<string.h>
#include<conio.h>

int main() {
    char str[20], rev[20];
    int *n;
    clrscr();
    cout<<"Palindrome String Using Pointers"<<endl;
    cout << "Enter a string: ";
    cin>>str;
    strcpy(rev, str);
    strrev(rev);
    *n=strcmp(str, rev);
    if (*n==0)
      cout<<"String is Palindrome";
else
cout<<"String is not Palindrome";
getch();
 return 0;
}

OUTPUT





Wednesday, March 22, 2023

BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 6- Write a C++ Program to create a class, which consists of EMPLOYEE Detail like E_Number, E_Name, Department, Basic_Salary and Grade. Write a member function to get and display them. Derive a class PAY from the above class and write a member function to calculate DA, HRA and PF depending on the grade.

Bharathiar University

Programming in C++ Lab
C++ Program 6
Write a C++ Program to create a class, which consists of EMPLOYEE Detail like E_Number, E_Name, Department, Basic_Salary and Grade. Write a member function to get and display them. Derive a class PAY from the above class and write a member function to calculate DA, HRA and PF depending on the grade.

In this page

  • C++ Source Code for MS Dos Turbo C++
  • C++ Source Code for Visual Studio Code
Source code for Turbo C++

 #include<iostream.h>

#include<conio.h>


class EMPLOYEE {

    public:

    int E_Number;

    char E_Name[50],dept[50];

    char grade;

    float basic_sal;

    void get()

    {

cout<<"Enter Employee details::"<<endl;

cout<<"Enter Employee Number: ";

cin>>E_Number;

cout<<"Enter Employee Name: ";

cin>>E_Name;

cout<<"Enter Employee department: ";

cin>>dept;

cout<<"Enter Employee Basic Salary: ";

cin>>basic_sal;

cout<<"Enter Employee Grade(A/B/C): ";

cin>>grade;

    }

    void display()

    {

cout<<"Employee Details"<<endl;

cout<<"****************"<<endl;

cout<<"Employee Number: "<<E_Number<<endl;

cout<<"Employee Name: "<<E_Name<<endl;

cout<<"Employee Department: "<<dept<<endl;

cout<<"Employee Basic salary: "<<basic_sal<<endl;

cout<<"Employee Grade: "<<grade<<endl;

    }

};


class PAY:public EMPLOYEE

{

float hra,da,pf,netsal;

    public:

    void calculate()

    {

if(grade=='A')

{

    da=0.5*basic_sal;

    hra=0.4*basic_sal;

    pf=0.1*(basic_sal+da);

    netsal=basic_sal+da+hra-pf;

}

else if(grade=='B')

{

     da=0.4*basic_sal;

    hra=0.3*basic_sal;

    pf=0.08*(basic_sal+da);

    netsal=basic_sal+da+hra-pf;

}

else

{

     da=0.3*basic_sal;

    hra=0.2*basic_sal;

    pf=0.05*(basic_sal+da);

    netsal=basic_sal+da+hra-pf;

}

    }

void disp_sal()

{

    cout<<"Dearness Allowance: "<<da<<endl;

    cout<<"House Rent Allowance: "<<hra<<endl;

    cout<<"Provident Fund: "<<pf<<endl;

    cout<<"Employee Net Salary: "<<netsal<<endl;

}

};


int main()

{

    PAY p;

    clrscr();

    p.get();

    p.calculate();

    p.display();

    p.disp_sal();

    getch();

return 0;

}


OUTPUT




Source code for Visual studio Code

#include<iostream>
#include<conio.h>
using namespace std;
class EMPLOYEE {
    public:
    int E_Number;
    char E_Name[50],dept[50];
    char grade;
    float basic_sal;
    void get()
    {
cout<<"Enter Employee details::"<<endl;
cout<<"Enter Employee Number: ";
cin>>E_Number;
cout<<"Enter Employee Name: ";
cin>>E_Name;
cout<<"Enter Employee department: ";
cin>>dept;
cout<<"Enter Employee Basic Salary: ";
cin>>basic_sal;
cout<<"Enter Employee Grade(A/B/C): ";
cin>>grade;
    }
    void display()
    {
cout<<"Employee Details"<<endl;
cout<<"****************"<<endl;
cout<<"Employee Number: "<<E_Number<<endl;
cout<<"Employee Name: "<<E_Name<<endl;
cout<<"Employee Department: "<<dept<<endl;
cout<<"Employee Basic salary: "<<basic_sal<<endl;
cout<<"Employee Grade: "<<grade<<endl;
    }
};

class PAY:public EMPLOYEE
{
float hra,da,pf,netsal;
    public:
    void calculate()
    {
if(grade=='A')
{
    da=0.5*basic_sal;
    hra=0.4*basic_sal;
    pf=0.1*(basic_sal+da);
    netsal=basic_sal+da+hra-pf;
}
else if(grade=='B')
{
     da=0.4*basic_sal;
    hra=0.3*basic_sal;
    pf=0.08*(basic_sal+da);
    netsal=basic_sal+da+hra-pf;
}
else
{
     da=0.3*basic_sal;
    hra=0.2*basic_sal;
    pf=0.05*(basic_sal+da);
    netsal=basic_sal+da+hra-pf;
}
    }
void disp_sal()
{
    cout<<"Dearness Allowance: "<<da<<endl;
    cout<<"House Rent Allowance: "<<hra<<endl;
    cout<<"Provident Fund: "<<pf<<endl;
    cout<<"Employee Net Salary: "<<netsal<<endl;
}
};

int main()
{
    PAY p;
    
    p.get();
    p.calculate();
    p.display();
    p.disp_sal();
    
return 0;
}

OUTPUT 

Enter Employee details::
Enter Employee Number: 101
Enter Employee Name: Raja
Enter Employee department: HR
Enter Employee Basic Salary: 15000
Enter Employee Grade(A/B/C): A
Employee Details
****************
Employee Number: 101
Employee Name: Raja
Employee Department: HR
Employee Basic salary: 15000
Employee Grade: A
Dearness Allowance: 7500
House Rent Allowance: 6000
Provident Fund: 2250
Employee Net Salary: 26250


Sunday, March 19, 2023

BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 4- Write a C++ Program to create a class FLOAT that contains one float data member. Overload all the four arithmetic operators so that they operate on the object FLOAT

  

Bharathiar University

Programming in C++ Lab
C++ Program 4
Write a C++ Program to create a class FLOAT that contains one float data member. Overload all the four arithmetic operators so that they operate on the object FLOAT





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<stdio.h>
#include<iomanip>
using namespace std;    
class FLOAT    
{    
        public: 
      float f;    
           void operator+(FLOAT ob)
       {     
              float result=f+ob.f;
              cout<<"Addition: "<<result;
              cout<<endl;
             
       } 
        void operator-(FLOAT ob)
       {     
              float result=f-ob.f;
              cout<<"Subtraction: "<<result;
             cout<<endl;
       }
        void operator*(FLOAT ob)
       {     
              float result=f*ob.f;
              cout<<"Multiplication: "<<result;
             cout<<endl;
       }
        void operator/(FLOAT ob)
       {     
              float result=f/ob.f;
              cout<<"Division: "<<setprecision(3)<<result;
             cout<<endl;
       } 
          
};    
int main()    
{    
    FLOAT ob1,ob2;
    cout << " Enter 1st float value: ";
    cin>>ob1.f;
    cout << " Enter 2nd float value: ";
    cin>>ob2.f;
    

    ob1+ob2;
    ob1-ob2;
    ob1*ob2;
    ob1/ob2;           
    return 0;    

Output
Enter 1st float value: 1.5
 Enter 2nd float value: 1.3
Addition: 2.8
Subtraction: 0.2
Multiplication: 1.95
Division: 1.15


Turbo c++


#include <iostream.h>
#include<stdio.h>
#include<iomanip.h>
#include<conio.h>

class FLOAT
{
public:
      float f;
   void operator+(FLOAT ob)
       {
      float result=f+ob.f;
      cout<<"Addition: "<<result;
      cout<<endl;

       }
void operator-(FLOAT ob)
       {
      float result=f-ob.f;
      cout<<"Subtraction: "<<result;
     cout<<endl;
       }
void operator*(FLOAT ob)
       {
      float result=f*ob.f;
      cout<<"Multiplication: "<<result;
     cout<<endl;
       }
void operator/(FLOAT ob)
       {
      float result=f/ob.f;
      cout<<"Division: "<<setprecision(2)<<result;
     cout<<endl;
       }

};
int main()
{
    FLOAT ob1,ob2;
    clrscr();
    cout << " Enter 1st float value: ";
    cin>>ob1.f;
    cout << " Enter 2nd float value: ";
    cin>>ob2.f;
    ob1+ob2;
    ob1-ob2;
    ob1*ob2;
    ob1/ob2;
    getch();
    return 0;
}

OUTPUT




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

 

Bharathiar University

Programming in C++ Lab
C++ 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




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;

class SumofDigits {
      
  public:
    long int n;
    SumofDigits()
    {
      n=0;
     
    }
    inline void getInteger() {
      cout << "Enter an Integer Number:";
      cin >> n;
    }

    int sum() 
    {
      int s = 0, r;

      while (n>0||s>9) {
if(n == 0)
        {
            n = s;
            s = 0;
        }
      r = n % 10;
          s = s + r;
          n = n / 10;
}
      return s;
  }
  ~SumofDigits()
  {
    cout<<"End of Program. Object Deleted";
  }
};

int main() {

  SumofDigits SD;
  int result;
   cout<<"Sum Of Digits"<<endl;
      cout<<"*************"<<endl;
  SD.getInteger();
  result = SD.sum();
  cout << "Sum of all digits of a number is " << result<<endl;
  return 0;
}

Output
Sum Of Digits
*************
Enter an Integer Number:12343
Sum of all digits of a number is 4
End of Program. Object Deleted


Turbo c++


#include <iostream.h>
#include<conio.h>

class SumofDigits {


   public:
    long int n;
    SumofDigits()
    {
n=0;

    }
    int sum();
    inline void getInteger() {
      cout << "Enter an Integer Number:";
      cin >> n;
    }
    ~SumofDigits()
  {
    cout<<"End of Program. Object Deleted";
  }
};
 int SumofDigits::sum()
    {
      int s = 0, r;

      while (n>0||s>9) {
if(n == 0)
        {
            n = s;
            s = 0;
        }
  r = n % 10;
  s = s + r;
  n = n / 10;
}
      return s;
  }

int main() {

  SumofDigits SD;
  int result;
  clrscr();
  cout<<"Sum Of Digits"<<endl;
cout<<"*************"<<endl;
  SD.getInteger();
  result = SD.sum();
  cout << "Sum of all digits of a number is " << result<<endl;
  getch();
  return 0;
}

OUTPUT


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++ Lab
C++ 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.

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 1
Enter the first string: hello

 Enter the second string: world
Strings are not equal
Concatenated String: hello world

OUTPUT 2
 Enter the first string: hello

 Enter the second string: hello
Given Strings are equal
Concatenated 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

 


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++ Lab
C++ Program 2
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.


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:20
9
Addition:29
Subtraction:11
Multiplication:180
Division: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




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++ Lab
C++ Program 12
Write 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:




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...