Showing posts with label CPlusPlus. Show all posts
Showing posts with label CPlusPlus. Show all posts

Sunday, 25 August 2013


C++ Program to Demonstrate inline Function using Call by Reference

Code:
/*C++ Program to Demonstrate inline Function using Call by Reference*/
#include<iostream.h>
#include<conio.h>

class callbyref
{
 public:
 inline void swap(int &x, int &y)
 {
 int temp;
  temp=x;
  x=y;
  y=temp;
 }
};

void main()
{
  clrscr();
  callbyref obj;
  int a=100,b=200;
  //cout<< "\n Enter 2 Numbers\n";
  //cin>>  val1 >> val2;
  cout<< "\n Before swap value of a & b = " << a << "\t" << b <<endl;
  obj.swap(a,b);
  cout<< " After swap value of a & b = " << a << "\t" <<  b <<endl;
  getch();
}

Sample Output:



Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

C++ Program to Demonstrate inline Function

Code:

/*C++ Program to Demonstrate inline Function*/
#include<iostream.h>
#include<conio.h>

class line
{
 public:
 inline float multi(int x, int y)
 { return (x*y);
 }
 inline float cube(int z)
 { return (z*z*z);
 }
};
void main()
{
  clrscr();
  line obj;
  int val1,val2;
  cout<< "\n Enter 2 Numbers\n";
  cin>>  val1 >> val2;
  cout<< " Multiplication is " <<obj.multi(val1,val2)<<endl;
  cout<< " Cube is "<<obj.cube(val1)<<endl;
  getch();
}

Sample Output:


Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

C++ Program to Find Smallest Number using Call by Value

Code:
/*C++ Program to Find Smallest Number using Call by Value*/
#include<iostream.h>
#include<conio.h>
int compare(int a,int b)
{
return((a<b)?a:b);
}
void main()
{
clrscr();
cout<< "smallest num is:" << compare(1,10) << endl;
cout<< "smallest num is:" << compare(20,10) << endl;
cout<< "smallest num is:" << compare(5,7) << endl;
getch();
}

Sample Output:



Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

C++ Program to Demonstrate Function Overloading

Code:
/*C++ Program to Demonstrate Function Overloading*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define pi 3.14
class fn
{
 public:
  void area(int);
  void area(int,int);
  void area(float,int,int);

};
void fn::area(int a)
{
 cout<<" Area of Circle is"<<pi*a*a<<endl;
}
void fn::area(int a,int b)
{
 cout<<" Area of Rectangle is"<<a*b<<endl;
}
void fn::area(float t,int a,int b)
{
 cout<<" Area of Triangle is"<<t*a*b<<endl;
}
int main()
{
int ch;
clrscr();
fn obj;
cout<<" Function Overloading"<<endl;
cout<<"\n 1.Circle \n 2.Rectangle \n 3.Triangle \n 4.Exit :";
cout<<"\n Enter your Choice"<<endl;
cin>>ch;
switch (ch)
{
 case 1:
 {            int radius;
    cout<<"enter radius of circle:"<<endl;
    cin>> radius;
    obj.area(radius);
    break;
 }
  case 2:
 {            int len,brea;
    cout<<" Enter Length and Breath of Rectangle:"<<endl;
    cin>>len>>brea;
    obj.area(len,brea);
    break;
 }
  case 3:
 {            int hig,base;

    cout<<" Enter Height and Base of Triangle:"<<endl;
    cin>> hig>>base;
    obj.area(0.5,hig,base);
    break;
 }
  case 4:
 {
 exit(0);
 }

 default:
 {
 cout<<" Wrong Input"<<endl;
 }
}
getch();
return 0;
}

Sample Output:


Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

C++ Program to Calculate Volume of a Cube using inline Function

Code:
/*C++ Program to Calculate Volume of a Cube using inline Function*/
#include<iostream.h>
#include<conio.h>
class line
{
  public:
 inline float multi(float a,float b)
   {
    return(a*b);
   }
  inline float cube(float a)
   {
    return(a*a*a);
   }
};
void main()
{
 line obj;
 float a1,b1;
 clrscr();
 cout<<"\n Enter Two Value ";
 cin>>a1>>b1;
 cout<<" Multiplication is :"<<obj.multi(a1,b1)<<endl;
 cout<<" Cube of val1 is :"<<obj.cube(a1)<<endl;
 cout<<" Cube of val2 is :"<<obj.cube(b1)<<endl;
 getch();
 }

Sample Output:


Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

C++ Program with Enumeration - enum

Code:
/*C++ Program with Enumeration - enum*/
#include<iostream.h>
#include<conio.h>
enum shape
{
circle,
rectangle,
triangle,
};
int main()
{
clrscr();
cout<<"\n Enter Shape Code:\n ";
int code;
cin>>code;
while(code>=circle && code<=triangle)
{
switch (code)
{
case circle:
cout<<" Its a circle";
break;
case rectangle:
cout<<" Its a rectangle";
break;
case triangle:
cout<<" its a triangle";
break;
}
cout<<"\n Enter Shape Code:\n ";
cin>>code;
}
return 0;
}

Sample Output:


Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

C++ Program to Demonstrate setprecision Manipulator

Code:
/*C++ Program to Demonstrate setprecision Manipulator*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main(void)
{
float a=5,b=3;
clrscr();
float c=a/b;
cout << setprecision(1) <<" c = " << c << endl;
cout << setprecision(2) <<" c = " << c << endl;
cout << setprecision(3) <<" c = " << c <<endl;
cout << setprecision(4) <<" c = " << c <<endl;
cout << setprecision(5) <<" c = " << c <<endl;
getch();
}

Sample Output:

Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

C++ Program to Demonstrate setfill Manipulator

Code:
/*C++ Program to Demonstrate setfill Manipulator*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main(void)
{
 int a=100,b=200;
 clrscr();
 cout<<setfill('*');
 cout<<setw(5) << a <<setw(5) << b <<endl;
 cout<<setw(6) << a <<setw(6) << b <<endl;
 cout<<setw(7) << a <<setw(7) << b <<endl;
 getch();
}

Sample Output:


Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

Saturday, 24 August 2013


C++ Program to Demonstrate ends Manipulator

Code:
/*C++ Program to Demonstrate ends Manipulator*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
int number=123;
clrscr();
cout<< '\"' << "number = " <<number<<ends;
cout<< '\"' << endl;
getch();
}

Sample Output:


Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

C++ Program to Display Data Variable using setw Manipulator Function

Code:
/*C++ Program to Display Data Variable using setw Manipulator Function*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main(void)
{
 int a=100,b=200;
 clrscr();
 cout<< setw 5) << a << setw(5) << b << endl;
 cout<< setw(6) << a << setw(6) << b << endl;
 cout<< setw(7) << a << setw(7) << b << endl;
 getch();
}

Sample Output:


Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

C++ Program to show Base of Number using setbase Manipulator Function

Code:
/*C++ Program to show Base of Number using setbase Manipulator Function*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main(void)
{
int value;
clrscr();
cout<<"\nEnter the Value \n";
cin>>value;
cout<<"Decimal base " << setbase(10) << value << endl;
cout<<"Hexadecimaal base " << setbase(16) << value << endl;
cout<<"Octal base " << setbase(8) << value << endl;
getch();
}

Sample Output:



Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

Program to show Base of Number using dec, oct and hexadecimal Manipulator Function

Code:
/*Program to show Base of Number using dec, oct and hexadecimal Manipulator Function*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main(void)
{
int value;
clrscr();
cout<<"\nEnter the Value \n";
cin>>value;
cout<<"Decimal value is "<<dec<<value<<endl;
cout<<"Hexadecimal value is "<<hex<<value<<endl;
cout<<"Octal value is "<<oct<<value<<endl;
getch();
}

Sample Output:


Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

C++ Program to Demonstrate Scope Regulator Operator

Code:
/*C++ Program to Demonstrate Scope Regulator Operator*/
#include<iostream.h>
#include<conio.h>
int m=10; //global m
int main()
{
 int m=20;  //m redeclared. local to main
 clrscr();
 {
 int k=m;
 int m=30; //m declared agan local to inner block
 cout<<"\n we are in inner block \n";
 cout<<" value of k = "<<k<<endl;
 cout<<" value of m = "<<m<<endl;
 cout<<" value of ::m= "<<::m<<endl;
 }
 cout<<"\n we are in outer block \n";
 cout<<" value  of m="<<m<<"\n";
 cout<<" value of ::m= "<<::m <<"\n";
 getch();
 return 0;
 }

Sample Output:


Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

Wednesday, 21 August 2013


C++ Program to Demonstrate math.h Library Functions

1.Trigonometric Functions

Code:
/*Trigonometric functions*/
#include<iostream>
#include<math.h>
#define PI 3.14159265
using namespace std;
class Trigo{
double val;
public:
void getData(){
cout <<"\n Enter the Value\n" << endl;
cin >>val;
}
void TrigoFun(){
cout<<endl<<"Sine                       of " << val << " is :" <<(val*PI/180);;
cout<<endl<<"cosine                    of " << val << " is :" <<cos(val*PI/180.0);
cout<<endl<<"arc tangent(atan)    of " << val << " is :" <<atan(val)*180/PI;
cout<<endl<<"arc tangent(aTan2) of " << val << " is :" <<atan2(val,-30.0)*180/PI;
cout<<endl<<"====================================================";
}
};

int main(){
Trigo obj;
obj.getData();
obj.TrigoFun();
return 0;
}

Sample Output:



2. Exponential and logarithmic functions

Code:
/*Exponential and logarithmic functions*/
#include<iostream>
#include<math.h>
using namespace std;
class Expo{
float val;
public:
void getData(){
cout<<"\n Enter the value\n";
cin>>val;
}
void expoResult(){
cout<<"\n The exponential value of = " << val <<exp(val);
cout<<"\n Log  Value                 of = " << val <<log(val);
cout<<"\n Log2 Value                of = " << val <<log2(val);
cout<<endl<<"====================================================";
}
};

int main(){
Expo e;
e.getData();
e.expoResult();
return 0;
}

Sample Output:



3. Power functions

Code:
/*Power functions*/
#include<iostream>
#include<math.h>
using namespace std;
class Power{
float val1,val2;
public:
void getData(){
cout<<"\n Enter the value\n";
cin>>val1>>val2;
}
void powFun(){
cout<<endl<<"\n "<<val1<<"^" << val2 <<"= " << pow(val1,val2);
cout<<endl<<"\n Square Root of "<< val1 << " is " << sqrt(val1) << " and "<< val2 <<" is " <<sqrt(val2);
cout<<endl<<"\n Cubic Root of Value1 = " << cbrt(val1);
cout<<endl<<"\n The Hypotenuse is      = " << hypot(val1,val2);
cout<<endl<<"====================================================";
}
};

int main(){
Power p;
p.getData();
p.powFun();
return 0;
}

Sample Output:



4. Rounding and Remainder Functions

Code:
/*Rounding and Remainder Functions*/
#include<iostream>
#include<math.h>
#include<fenv.h>
using namespace std;
class Round{
 double value;
 public:
 void getData(){
 cout<<"\n Enter the value\n";
 cin>>value;
 }
void roundResult(){
 cout<<"\n Ceil Value          = "<<ceil(value);
 cout<<"\n floor  Value        = "<<floor(value);
 cout<<"\n trunc  Value        = "<<trunc(value);
 cout<<"\n round  Value       = "<<round(value);
 cout<<"\n rint  Value           = "<<rint(value);
 cout<<"\n remainder  Value = "<<remainder(value,4.2);
 cout<<"\n nearbyint  Value  = "<<nearbyint(value);
 cout<<endl<<"====================================================";
}
};

int main(){
Round obj;
obj.getData();
obj.roundResult();
return 0;
}

Sample Output:



5. Minimum, Maximum, Difference Functions

Code:
/*Minimum, Maximum, Difference Functions*/
#include<iostream>
#include<math.h>
using namespace std;
class Diff{
double val1,val2;
public:
void getData(){
cout<<"\n Enter the Values\n";
cin>>val1>>val2;
}
void diffResult(){
cout<<"\n fdim  Value of " << val1 <<" and " <<  val2 <<  " is= " << fdim(val1,val2);
cout<<"\n fmax  Value of " << val1 <<" and " <<  val2 <<  " is= " << fmax(val1,val2);
cout<<"\n fmin  Value of " << val1 <<" and " <<  val2 <<  " is= " << fmin(val1,val2);
cout<<endl<<"====================================================";
}
};

int main(){
Diff d;
d.getData();
d.diffResult();
return 0;
}

Sample Output:



6. Comparison Macro Functions

Code:
/*Comparison Macro Functions*/
#include<iostream>
#include<math.h>
using namespace std;
class Comparison{
double val1,val2;
public:
void getData(){
cout<<"\n Enter the Values\n";
cin>>val1>>val2;
}
void comparisonResult(){
cout <<"\n 1 for true and 0 for false";
cout<<"\n isgreater  value     = "<<isgreater(val1,val2);
cout<<"\n islessequal  Value  = "<<islessequal(val1,val2);
cout<<"\n isunordered Value = "<<isunordered(val1,val2);
cout<<endl<<"====================================================";
}
};

int main(){
Comparison obj;
obj.getData();
obj.comparisonResult();
return 0;
}

Sample Output:


Comment bellow for Query and Feedback..
Prof.Mahendra Kanojia

Friday, 9 August 2013


 C++ Program to Create Class Person and Print Name and Age

Code:
/*C++ Program to Create Class Person and Print Name and Age*/
#include<iostream.h>
#include<conio.h>
class person
{
    char name[30];
    int age;
    public: void getdata(void);
        void display(void);

};
void person :: getdata(void)
    {
    cout<<"\n Enter the Name ";
    cin>>name;
    cout<<"\n Enter the Age ";
    cin>>age;
    }

    void person :: display(void)
    {
        cout<<"\n Name: "<<name;
        cout<<"\n Age: "<<age;
    }
int main()
{
clrscr();
person p;
p.getdata();
p.display();
getch();
return 0;
}

Sample Output:


Prof.Mahendra Kanojia

Wednesday, 10 July 2013


C++ Program to generate square root of 1 to 10 numbers

Code:
/*C++ Program to generate square root of 1 to 10 numbers*/
#include<iostream.h>
#include<conio.h>
int main()
{
 clrscr();
 int num;
 double sq_root;
 for (num; num<10: num++)
 {
 sq_root = sqrt((double)num);
 cout << num << " " << sq_root << '\n';
 }
 getch();
 return 0;
}

Sample Output:



Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

C++ Program to illustrate simple if-else
Finding Greatest of 2 numbers

Code:
/*C++ Program to illustrate simple if-else
Finding Greatest of 2 nos.*/
#include<iostream.h>
#include<conio.h>
int main()
{
 clrscr();
 int a,b;
 cout << " Enter number 1 = " ;
 cin >> a;
 cout << " Enter 2nd number = ";
 cin >> b;
 if (a>b)
 cout << " Number 1 is Greater than Number 2";
 else
 cout <<" Number 2 is Greater than Number 1";
 getch();
 return 0;
}

Sample Output:



Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

c++ program to convert gallons to liters

Code:
/*c++ program to convert gallons to liters*/
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float gallons,litres;
cout <<" Enter Gallons ";
cin >>gallons;
liters=gallons*3.7854;
cout <<" Litre is =" <<litres;
getch();
return 0;
}

Sample Output:


Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia

Basic c++ program to print Variables and constant

Code:
/*Basic c++ program to print Variables and constant*/
#include<iostream.h>
const int CONST_VAL =5;
int main()
{
int ivalue;
float fvalue;
char cvalue;

ivalue=1234;
fvalue=123.45;
cvalue='A';

cout <<"integer value is=" <<ivalue << endl;
cout <<"float value is =" <<fvalue <<endl;
cout <<"char value is =" <<cvalue <<endl;
cout <<"the const is" << CONST_VAL;
return 0;

}

Sample Output:

Comment bellow for your Query and Feedback
Prof.Mahendra Kanojia