Skip to main content

C++ Programs (object oriented programming)


1. WAP for digital clock.
#include<conio.h>
#include<iostream.h>
#include<dos.h>
void main()
{
    int s=0,m=0,h=0,i;
    clrscr();
    for(i=1;i<=3600;i++)
    {
        s++;
        if(s==60)
        {
            m++;
            s=0;
        }
        if(m==60)
        {
            h++;
            m=0;
        }
        textbackground(WHITE);
        textcolor(YELLOW);
        delay(5);
        clrscr();
        cprintf("%d::%d::%d",h,m,s)
        ;
    }
    getch();

}
2. WAP for matrix multiplication.
#include<conio.h>
#include<iostream.h>
void main()
{
    int a[3][3],b[3][3],c[3][3],i,j,k;
    clrscr();
    cout<<"Enter The 9 number of Matrix a:\n";
    for(i=0;i<=2;i++)
    {
        for(j=0;j<=2;j++)
        {
            cin>>a[i][j];
        }
    }
    cout<<"Enter The 9 number of Matrix b:\n";
    for(i=0;i<=2;i++)
    {
        for(j=0;j<=2;j++)
        {
            cin>>b[i][j];
        }
    }
    cout<<"Enter The 9 number of Matrix a:\n";
    for(i=0;i<=2;i++)
    {
        for(j=0;j<=2;j++)
        {
            c[i][j]=0;
            for(k=0;k<=2;k++)
            {
                c[i][j]=c[i][j]+a[i][k]*b[k][j];
            }
        }
    }
    cout<<"Matrix Multiplication is:\n";
    for(i=0;i<=2;i++)
    {
        for(j=0;j<=2;j++)
        {
            cout<<c[i][j]<<"\t";
        }
        cout<<"\n";
    }
    getch();

}
3. WAP Enter any number and check  number is prime number or not?
#include<conio.h>
#include<iostream.h>
void main()
{
    int num,temp=0,i;
    char ans='y';
    clrscr();
    while(ans=='y')
    {
        cout<<"\nEnter any number";
        cin>>num;
        i=2;
        do
        {
            if(num%i==0)
            {
                temp=1;
                break;
            }
            i++;
        }while(i<num);

        if(temp==0)
        cout<<"Number is prime";
        else
        cout<<"Number is not prime";
        temp=0;
        cout<<"\nDo u want to check another num(y/n):";
        ans=getch();
    }
    getch();

}
4. WAP enter any number and generate a prime number series.
#include<conio.h>
#include<iostream.h>
void main()
{
    int num,i,j,temp=0;
    clrscr();
    cout<<"Enter The number\n";
    cin>>num;
    for(i=1;i<=num;i++)
    {
        for(j=2;j<i;j++)
        {
            if(i%j==0)
            {
                temp=1;
                break;
            }
        }
        if(temp==0)
        cout<<i<<"\t";
        temp=0;
    }
    getch();
}

5. WAP enter any string and remove all vowels .
#include<conio.h>
#include<iostream.h>
#include<string.h>
void main()
{
    int i,j,temp=0;
    char str1[20],str2[]="aeiou";
    clrscr();
    cout<<"Enter The string\n";
    cin>>str1;
    for(i=0;str1[i]!='\0';i++)
    {
        for(j=0;j<=4;j++)
        {
            if(str1[i]==str2[j])
            {
                temp=1;
                break;
            }
        }
        if(temp==0)
        cout<<str[i];
        temp=0;
    }

    getch();
}
6. WAP enter any  integer number and calculate the sum of digit till sum become single digit.
/* num=219->sum=12->sum=2*/
#include<conio.h>
#include<iostream.h>
void main()
{
    long int num,rem,sum=0;
    clrscr();
    cout<<"Enter The number\n";
    cin>>num;
    s:
    while(num>0)
    {
        rem=num%10;
        sum=sum+rem;
        num=num/10;
    }
    if(sum>9)
    {
        cout<<"\nSum of Digit="<<sum;
        rem=0;
        num=sum;
        sum=0;
        goto s;
    }
    else
    {
        cout<<"\nSum of Digit="<<sum;
    }

getch();
}
7. WAP enter any two number and calculate the addition ,multiplication, subtraction & division using switch case.
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>

void main()
{
    int cho,a,b;
    float result;
    clrscr();
    cout<<"1.Add\n";
    cout<<"2.Mult\n";
    cout<<"3.Sub\n";
    cout<<"4.Div\n";
    cout<<"5.Exit\n";
    cout<<"Enter Ur choice:";
    cin>>cho;
    cout<<"Enter any two number\n";
    cin>>a>>b;
    switch()
    {
        case 1:
                        result=a+b;
                        break;
        case 2: result=a*b;
                       break;
        case 3: result=a-b;
                      break;
        case 4: result=a/b;
                       break;
        case 5: exit(0);
    }
    cout<<"\nResult="<<result;
    getch();
}


8. WAP  enter any two number or char and swap using templates(Generic Function).
#include<conio.h>
#include<iostream.h>
template<class T>
void swap(T a, T b)
{
    T c;
    c=a;
    a=b;
    b=c;
    cout<<"\nBefore swaping\n";
    cout<<"var1="<<a<<"\tvar2="<<b;
}
void main()
{
    int a=10,b=20;
    float fa=10.1,fb=20.1;
    char ch1='a',ch2='b';
    clrscr();
   
    cout<<"\nBefore swaping Intger\n";
    cout<<"a="<<a<<"\tb="<<b;
    swap(a,b);
  
    cout<<"\nBefore swaping Float\n";
    cout<<"fa="<<fa<<"\tfb="<<fb;
    swap(fa,fb);
   
    cout<<"\nBefore swaping Float\n";
    cout<<"fa="<<fa<<"\tfb="<<fb;
    swap(ch1,ch2);
   
    getch();
}

9. WAP sorting a integer , float & char array using templates (Generic Function).
#include<conio.h>
#include<iostream.h>
template <class T>
void sort(T a,int n)
{
    int i,j;
    cout<<"\nSorted Array.....\n";
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[j]<a[i])
            swap(a[i],a[j]);
        }
        cout<<a[i]<<"\t";
    }
}
template <class T1>
T1 swap(T1 &a,T1 &b)
{
    T1 c;
    c=a;
    a=b;
    b=c;
    return a,b;
}
void main()
{
    clrscr();
    int x[]={5,4,3,2,1};
    float y[]={5.4,4.4,3.2,2.1,1.1};
    char ch[]={'e','d','c','b','a'};
    cout<<"\nInterger Array";
    sort(x,5);
    cout<<"\nFloat Array";
    sort(y,5);
    cout<<"\n Char Array";
    sort(ch,5);
    getch();

}

10. Program to Open a file, read a file and write to a file.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
#include<iomanip.h>
class Item
{
    int code;
    float price;
    char name[30];
    public:
    void getdata()
    {
        cout<<"Enter the item code:";
        cin>>code;
        cout<<"Enter the item price:";
        cin>>price;
        cout<<"Enter the item name:";
        cin>>name;
    }
    void display()
    {
        cout<<setw(5)<<code<<setw(10)<<setprecision(2)<<price<<setw(15)<<name<<"\n";
    }
};
void main()
{
    clrscr();
    Item obj[2];
    int i;
    fstream out("c:\\item.txt",ios::in|ios::out);


    for(i=0;i<=1;i++)
    {
        obj[i].getdata();
    }
    out.write((char *)&obj,sizeof(obj));
    //out.seekg(0,ios::beg);
    out.read((char *)&obj,sizeof(obj));
    cout<<"\n\n";
    cout<<setw(6)<<"Code"<<setw(10)<<"Price"<<setw(14)<<"Name\n";
    for(i=0;i<=1;i++)
    {
        obj[i].display();
    }
    out.close();
    getch();
}

Comments

  1. great tutorial................very helpful and conceptual.

    ReplyDelete

Post a Comment

Popular posts from this blog

Write a program to calculate the total expenses. Quantity and price per item are input by the user and discount of 10% is offered if the expense is more than 7000.

  #include<iostream.h> #include<conio.h> void main() { int totalexp, qty, price, discount; cout<<" Enter quantity: "; cin>>qty; cout<<" Enter price: "; cin>>price; totalexp=qty*price; if (totalexp>7000) { discount=(totalexp*0.1); totalexp=totalexp-discount; } cout<<" Total Expense is Rs. "<<totalexp; getch(); }

C++ Casino Game Project

Description: This C++ program on CASINO GAME is a simple text base game.We have used procedure oriented method to design this game. This program is without grahics to keep program easy for beginners . Player can deposit his money to play. From this amount he can bet on number between 1 to 12. If he win he gets 10 times of money otherwise lost his money. In this project, We have used programming concept like do..while loop, user defined function, library function like randomize(), random() etc. #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<stdio.h> #include<time.h> void draw_line( int n, char ch); void rules(); void main() { int balanceamt,amt,no,dice; char playername[80],ch; clrscr(); draw_line(60,'='); cout<<" \n\n\n\n\t\tCASINO GAME\n\n\n\n "; draw_line(60,'='); cout<<" \n\n\nEnter Your Name : "; gets(playername); cout<<" \n\n\Enter Deposit amount to play game : ...

How to Create Project report(Vehicle Management System)

INTRODUCTION Vehicle Management System is software which is helpful for bus operators, who wants to operate many bus trips in a day. Vehicle Management System is a windows application written for 32-bit Windows operating systems which focused in the area of adding, editing and deleting the passengers, staff and the bus routes. In this software a person can be register as a user and he can manage the bus routes and the staff, passengers’ details.  He can add a bus and its details including bus route details. User can also add the details of the staff and their duty time in the system. There are mainly 5 modules in this software Bus Management Route Management Employee Management Passenger Management             In Bus module a user can add a new bus details to the database. He can set the details of the route and the timing also. Bus details like the type, engine number, seating capacity and ...