## Posts

Showing posts with the label CPP

### Using the concept of Inheritance write a C++ Program to calculate the area and perimeter of rectangle

/* C++ Program to calculate the area and perimeter of rectangles using concept of inheritance. */ #include using namespace std; class Rectangle { protected: float length, breadth; public: Rectangle(): length(0.0), breadth(0.0) { cout<<"Enter length: "; cin>>length; cout<<"Enter breadth: "; cin>>breadth; } }; /* Area class is derived from base class Rectangle. */ class Area : public Rectangle { public: float calc() { return length*breadth; } }; /* Perimeter class is derived from base class Rectangle. */ class Perimeter : public Rectangle { public: float calc() { return 2*(length+breadth); } }; int main() { cout<<"Enter data for first rectangle to find area.\n"; Area a; cout<<"Area = "<

### Implement array of Objects using C++

Write a Program in C++ to implement array of Objects. Take the user input of Name of player, Age and score. Sort the data based on the highest Scores. Program: #include<iostream.h> #include<conio.h> #include<string.h> class det {  public:  int age,score;  char name;  void input();  void show();  void init(); };  void det::init()  {   age=0;   score=0;   char name1="Blank";   strcpy(name,name1);  }  void det::input()        {        cout<<"Enter the Name: ";        cin>>name;        cout<<"Enter the Age: ";        cin>>age;        cout<<"Enter the score: ";        cin>>score;        }  void det::show()        {        cout<<" Name  : " <<name;        cout<<"\t Age   : " <<age;        cout<<"\t Score : " <<score <<endl<<endl<<endl;        } int main() {  int i,n=0,j,choice,index,count=0;  char ch;  det a[

### Print Details using C++

Write a Program in C++ to print the details. #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> class Details {  public:  void person(char[],int,int,int); }; inline void Details :: person(char name[],int id,int dob,int marks) {  cout<<"Name : ";  puts(name);  cout<<"ID : "<<id;  cout<<"Date of birth : "<<dob;  cout<<"Marks : "<<marks; } int main() {  Details ob;  char name;int dob,id,marks;  cout<<"Enter your name: ";  gets(name);  cout<<"Enter your ID: ";  cin>>id;  cout<<"Enter your Date of Birth: ";  cin>>dob;  cout<<"Enter your marks: ";  cin>>marks;  ob.person(name,id,dob,marks);  getch();  return 0;  } Output: Enter your name: Sumit Enter your ID: 55 Enter your Date of Birth: 14031994 Enter your marks: 65 Name : Sumit ID: 55 Date of birth: 14031994 Marks: 65

### Find the Size of a Class and object in C++

Write a Program to find the Size of a Class and object. #include <iostream.h> #include <conio.h>  class SI   {    public:     double p,t,r;     void interest(double,double,double);   };  inline void SI :: interest(double a, double b, double c)    {     double i,sum;     p=a;     t=b;     r=c;     i=(p*t*r)/100;     sum=p+i;     cout<<"\nThe Interest is: "<<i;     cout<<"\nThe Sum is: "<<sum;    }  int main()   {   double principal,time,rate;   clrscr();   cout<<"Principal: ";   cin>>principal;   cout<<"Time: ";   cin>>time;   cout<<"Rate: ";   cin>>rate;   SI ob;   ob.interest(principal,time,rate);   cout<<"\nSize of Class: "<<sizeof(SI);   cout<<"\nSize of Object: "<<sizeof(ob);   getch();   return 0;   } Output: Principal: 5000 Time:10 Rate: 10 The Interest is: 5000 The Sum is: 10000 Size of Class: 24 Size of Object: 24

### Find Simple Interest using C++

Write a Program in C++ to find Simple Interest. #include <iostream.h> #include <conio.h>  class SI   {    public:     double p,t,r;     void interest(double,double,double);   };   void SI :: interest(double a, double b, double c)    {     double i,sum;     p=a;     t=b;     r=c;     i=(p*t*r)/100;     sum=p+i;     cout<<"\nThe Interest is: "<<i;     cout<<"\nThe Sum is: "<<sum;     getch();    }  int main()   {   double principal,time,rate;   clrscr();   cout<<"Principal: ";   cin>>principal;   cout<<"Time: ";   cin>>time;   cout<<"Rate: ";   cin>>rate;   SI ob;   ob.interest(principal,time,rate);   return 0;   } Output: Principal: 5000 Time:10 Rate: 10 The Interest is: 5000 The Sum is: 10000

### Print the digits of a number in words using C++

Write a Program in C++ to print the digits of a number in words. Program: #include <iostream.h> #include <conio.h>  class PNIW   {    public:      void ntow(int);      void display(int);   };  void PNIW :: ntow(int n)    {     int i=0,j=0,temp,sum=0,count=0,ar;     temp=n;     while(temp!=0)      {       i=temp%10;       ar[count]=i;       count++;       temp=temp/10;      }     for(i=count-1;i>=0;i--)      {       j=ar[i];       display(j);      }    }  void PNIW :: display(int i)    {     switch(i)     {     case 0:cout<<"Zero ";         break;     case 1:cout<<"One ";         break;     case 2:cout<<"Two ";         break;     case 3:cout<<"Three ";         break;     case 4:cout<<"Four ";         break;     case 5:cout<<"Five ";         break;     case 6:cout<<"Six ";         break;     case 7:cout<<"Seven ";         break;     case 8:cou

### Write a Program in C++ to find the Factorial of a Number

/*ITERATIVE*/ //Calling the Header File. #include<iostream> //Declaring the main function int main () { //Tells the compiler that we'll be using all the standard C++ library functions using namespace std ; int num , factorial = 1 ; //Ask for the number. cout << "Enter a number to calculate it's factorial" << endl ; cin >> num ; for ( int i = 1 ; i <= num ; i ++ ) { factorial = factorial * i ; } cout << "Factorial of " << num << "=" << factorial << endl ; cin . get (); return 0 ; } /*RECURSION */ # include < iostream > using namespace std ; int fact ( int ); int main (){ int num , f ; cout << " \n Enter a number: " ; cin >> num ; f = fact ( num ); cout << " \n Factorial of " << num << " is: " << f ; return 0 ; } //recursive function int