C++ Programs Example List 1

//WAP to input two numbers and perform arithmetic operation.

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

int main()
{
          int a,b;
          clrscr();
          cout<<endl<<"Enter the value of a:";
          cin>>a;
          cout<<endl<<"E`nter the value of b:";
          cin>>b;

          cout<<"addition      = "<<a+b<<endl;
          cout<<"subtraction   = "<<a-b<<endl;
          cout<<"multiplication= "<<a*b<<endl;
          cout<<"division      = "<<a/b<<endl;
          cout<<"reminder      = "<<a%b<<endl;
          getch();
          return 0;
}

//WAP to input 3 numbers and find total and average.

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

int main()
{
          int a,b,c,sum;
          float avg;
          clrscr();

          cout<<"enter three numbers:";
          cin>>a>>b>>c;
         
          sum=a+b+c;
          avg=sum/3.0;

          cout<<"sum of "<<a<<", "<<b<<" and "<<c " is "<<sum;
          cout<<endl<<"average is= "<<avg;

          getch();
          return 0;
}

//Write a C++ program to find minimum from two numbers.

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

int main()
{
          int a,b;
          clrscr();
          cout<<"enter the value of a:";
          cin>>a;
          cout<<"enter the value of b:";
          cin>>b;

          if(a<b)
          {
                   cout<<"a is minimun:";
          }
          else
          {
                   cout<<"b is minimun;";
          }
         
          getch();
          return 0;
}

//Write a C++ program to print day using switch case.

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

int main()
{
          int d;
          clrscr();

          cout<<"enter the day number(0-7):";
          cin>>d;

          switch(d)
          {
                   case 1:         cout<<"sunday";  break;

                   case 2:         cout<<"monday"; break;

                   case 3:         cout<<"tuesday"; break;

                   case 4:         cout<<"wednesday";      break;

                   case 5:         cout<<"thursday";         break;

                   case 6:         cout<<"friday";              break;

                   case 7:         cout<<"saturday";         break;

                   default: cout<<"invalid day/no";               
          }

          getch();
          return 0;
}

// WAP of function overloading to overload area() function.

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

float area(float r);
float area(float b, float h);

int main()
{
          float r,b,h;
          clrscr();
          cout<<"enter radius of circle:";
          cin>>r;
          cout<<endl<<"area of circle:"<<area(r);
         
          cout<<"\n\n enter base and height of triangle:";
          cin>>b>>h;
          cout<<"\n area of triangle:"<<area(b,h);
          getch();
          return 0;
}

float area(float r)
{
          return (3.14*r*r);
}

float area(float b,float h)
{
          return (0.5*b*h); 
}

//WAP to create a class Employee(id, name, salary). Get and Display Employee data using member function.

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

class employee
{
          int id;
          char name[10];
          float salary;

          public:
                   //member function declare inside class
                   void getdata()
                   {
                             cout<<endl<<"enter the id:";
                             cin>>id;
                             cout<<endl<<"enter the name:";
                             cin>>name;
                             cout<<endl<<"enter the salary:";
                             cin>>salary;
                   }

          void display();
};

//member function declare outside class
void employee::display()
{
          cout<<endl<<"employee details......:";
          cout<<endl<<"employee id="<<id;
          cout<<endl<<"employee name="<<name;
          cout<<endl<<"employee salary="<<salary;
}
         
int main()
{
          employee e1;
          clrscr();
          e1.getdata();
          e1.display();
          getch();
          return 0;
}

// WAP to create class Book(id, name, price). Get and display details of 5 books using array of objects.

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

class Book
{
          int id;
          char name[10];
          float price;

          public:
         
          void input()
          {
                   cout<<"enter book id:";
                   cin>>id;
                   cout<<"enter book name:";
                   cin>>name;
                   cout<<"enter book price:";
                   cin>>price;
          }
         
          void dispdata()
          {
                   cout<<id<<"\t";
                   cout<<name<<"\t";
                   cout<<price<<endl;
          }
};

int main()
{
          clrscr();
          Book b[5];
         
          for(int i=0;i<5;i++)
                   b[i].input();
         
          cout<<"\nBook Information:\n\n";
          cout<<endl<<" id \t title \t\t price"<<endl;
          for(i=0;i<5;i++)
                   b[i].dispdata();
         
          getch();
          return 0;
}


Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Java Event Delegation Model, Listener and Adapter Classes