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

WAP to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for upto 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls.

  #include<iostream.h> #include<conio.h> void main() { int calls; float bill; cout<<" Enter number of calls : "; cin>>calls; if (calls<=100) bill=200; else if (calls>100 && calls<=150) { calls=calls-100; bill=200+(0.60*calls); } else if (calls>150 && calls<=200) { calls=calls-150; bill=200+(0.60*50)+(0.50*calls); } else { calls=calls-200; bill=200+(0.60*50)+(0.50*50)+(0.40*calls); } cout<<" Your bill is Rs. "<<bill; getch(); }   Output: Enter number of calls : 190 Your bill is Rs.250

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