January 29, 2013

Simple C++ (OOPs) Programs

/* Program to enter two values and perform addition and multiplication operation*/


#include<iostream.h>
#include<conio.h>
main()
{
      int n1,n2,sum,mul;
      cout<<"\n Enter any two numbers";
      cin>>n1>>n2;
      sum=n1+n2;
      mul=n1*n2;
      cout<<"\n Sum of two numbers is "<<sum;
      cout<<"\n Multiplication of two numbers is "<<mul;
      getch();
}
output of program to enter values and perform addition and multiplication operation in C++ (OOPs)

/* Program to show addition and average of three (3) numbers*/

#include<iostream.h>
#include<conio.h>
main()
{
      int n1,n2,n3,sum;
      float av;
      cout<<"\n Enter any three numbers";
      cin>>n1>>n2>>n3;
      sum=n1+n2+n3;
      av=sum/3.0;
      cout<<"\n Sum of three numbers is "<<sum;
      cout<<"\n Average of three numbers is "<<av;
      getch();
      }
output of program to show sum and average of three numbers in C++ (OOPs)

/* Program to enter length and breadth of rectangle and show its area*/

#include<iostream.h>
#include<conio.h>
main()
{
      int l,b;
      float ar;
      cout<<"\n Enter value of length and breadth";
      cin>>l>>b;
      ar=l*b;
      cout<<"\n Area is: "<<ar;
      getch();
      }
output of program to enter length and breadth of rectangle in C++ (OOPs)

/* Program to enter base and height of the triangle and show its area*/

#include<iostream.h>
#include<conio.h>
main()
{
      float b,h,ar;
      cout<<"\n Enter height and base of triangle";
      cin>>h>>b;
      ar=0.5*h*b;
      cout<<"\n Area of triangle is: "<<ar;
      getch();
      }
output of program to enter base and height of the triangle for its area in C++ (OOPs)

/* Program to enter Item name and its quantity and calculate the price*/

#include<iostream.h>
#include<conio.h>
main()
{
      int itcd;
      float pr,qty,tamt;
      char itnm[10];
      cout<<"\n Enter the item code and name";
      cin>>itcd>>itnm;
      cout<<"\n Enter item's quantity(in Kg) and price";
      cin>>qty>>pr;
      tamt=qty*pr;
      cout<<"\n Code\tName\tQty.\tPrice\tT.Amount";
      cout<<"\n "<<itcd<<"\t"<<itnm<<"\t"<<qty<<"\t"<<pr<<"\t"<<tamt;
      getch();
      }
output of program to enter item code, and its value to calculate the bill i.e. total amount in C++ (OOPs)


PRECAUTIONS

1. Use accurate header files as sometimes we entered getch(); but we don’t apply is header i.e.#include<conio.h>.
2. Never apply space in header files.
3. Never to apply ‘;’ after void main.
4. After declaring variables, cout and cin statements apply ‘;’ so as prevent program from syntax error.
5. Apply ‘;’ after getch().
6. Always close the brackets if you have started.

No comments:

Post a Comment