Interesting C++ Program With Explanation
Program No.1
Raising a number n to a power p is the same as multiplying n by itself p times.Write a function called power ( ) that takes a double value for n and an int value for p, and returns the result as double value. Use a default argument of 2 for p, so that if thisargument is omitted, the number will be squared. Write a main( ) function that gets values from the user to test this function.
//Raising a number n to a power p is the same as multiplying n by itself p times.
#include//including file iostream
double power(double,int=2); //prototype of power function
int main(){
double n;
int p ;
std::cout<<"enter the number and its power to be calculated"<<"\n";
std::cin>>n>>p;
std::cout<<"\n"<<"it's square is :"<<"\n";
std::cout<
std::cout<<"\n"<<"power as you required is :"<<"\n";
std::cout<<"\n"<<<'\n';
return 0;
}
//power function calculates power of number n
//and returns ,default power is 2.
double power(double n,int p){
double q=1;
for(int i=0;i
q=q*n;
return q;
}
Here Is the Output:
Program No. 2
A point on the two dimensional plane can be represented by two numbers: an X coordinate and a Y coordinate. The sum of two points can be defined as a new point whose X coordinate is the sum of the X coordinates of the points and whose coordinate is the sum of their Y coordinates. Write a program that uses a structure called point to model a point. Define three points, and have the user input values to two of them. Than set the third point equal to the sum of the other two, and display the value of the new point.
// adding two points using structure and display new point.
#includestruct point
//structure defination
{ int x,y;
};
//Add function adds two point and return new point
point add(point p1,point p2)
{
point p3;
p3.x=p1.x+p2.x;
p3.y=p1.y+p2.y;
return p3;
}
main()
{
int x,y;
point p1,p2,p3;
std::cout<<"enter the x-coordinate :";
std::cin>>p1.x;
std::cout<<"enter the y-coordinate :";
std::cin>>p1.y;
std::cout<<"enter the x-coordinate for second point :";
std::cin>>p2.x;
std::cout<<"enter the y-coordinate for second point :";
std::cin>>p2.y;
p3=add(p1,p2); //calling add functions
std::cout<<"co-ordinates of third point is:"<<"\t"<<<"\t"<
return 0;
}
Here Is Output:
Program No. 3
Create the equivalent of a four function calculator. The program should request the user to enter a number, an operator, and another number. It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. (It should use a switch statement to select the operation). Finally it should display the result. When it finishes the calculation, the program should ask if the user wants to do another calculation.
// Creating the equivalent of a four function calculator //using switch statement.
# includeint add(int a,int b) //adds two numbers and return new number
{
int c=a+b;
return c;
}
int sub(int a,int b) //subtracts two numbers and return new number
{
int c=a-b;
return c;
}
int multi(int a,int b) // multiplies two numbers and return new number
{
int c=a*b;
return c;
}
int divide(int a,int b) //it divides two numbers and return new number
{
int c=a/b;
return c;
}
int main()
{
int a,i,j;
char o;
char t='y’;
do {
std::cout<<"enter the first number ,operator & second number"<<"\n";
std::cin>>i>>o>>j;
switch (o)
{
case '+':
{
a=add(i,j);
std::cout<<"addition of two numbers is:\t"<
break; }
case '-':
{
a=sub(i,j);
std::cout<<"subtraction of two numbers is:\t"<
break; }
case '*':
{
a=multi(i,j);
std::cout<<"multiply of two numbers is:\t"<
break;
}
case '/':
{
a=divide(i,j);
std::cout<<"division result is:\t"<
break; }
defualt:{
std::cout<<"use (+,-,*,/) operators only";
break; }
}
std::cout<<"\n"<<"wanna try again press y/n"<<"\n";
std::cin>>t;
}while(t=='y');
}
Here Is Output:
Program No. 4
A phone number, such as (212) 767-8900, can be thought of as having three parts: the area code (212), the exchange (767) and the number (8900). Write a program that uses a structure to store these three parts of a phone number separately. Call the structure phone. Create two structure variables of type phone. Initialize one, and have the use number for the other one. Then display both numbers.
// create a phone number containing three parts area code, //exchange and number
//using structure variables.
#includeusing std::cout;
using std::cin;
using std::endl;
//creating structure phone having area code , exchange and //number
struct phone
{
int areacode;
int exchange;
int number;
};
//creating setnumber function having return type phone
//ignore is a member function of istream.
phone setnumber(phone &p3)
{
cin.ignore(); //ignore function ignores first brace ’(’
cin>>p3.areacode; //input area code
cin.ignore(1); //ignores ‘)’
cin>>p3.exchange; //input exchange part of phone number
cin.ignore(); //ignores ‘-’
cin>>p3.number; //input number
}
//creating a display function which display phone number
//in given pattern.
void display(phone &p3)
{
cout<<"("<<<")"<<<"-"<<
}
int main()
{
phone p2,p1;
cout<<"enter phone number in the form (212)767-8900:"<<"\t";
setnumber(p2); //sets phone number from user to p2
//initializing phone number to p1
p1.areacode=212;
p1.exchange=767;
p1.number=8900;
cout<<"my number is:";
display(p1); //display p1
cout<<"your number is:";
display(p2); //display p2
return 0;
}
Here Is Output:
Program No. 5
Create two classes DM and DB which store the value of distances. DM stores distances in meters and centimeters and DB in feet and inches. Write a program that can read values for the class objects and add one object of DM with another object of DB. Use a friend function to carry out the addition operation. The object that stores the results maybe a DM or DB objects, depending on the units in which the results are required. The display should be in the format feet in inches meters and centimeters depending on the object on display.
// create two classes dm & db stores distance in different scales carry out addition
//operation with objects of two classes using friend function
#includeusing std::cout;
using std::cin;
using std::endl;
class db; //forward declaration of class db
class dm
{
int meter,centi;
friend dm operator +(dm &,db &);
public:
dm();
dm(int m);
void insert();
void display();
};
dm::dm() //constructor
{
meter=0;
centi=0;
}
dm::dm(int m) //parameterised constructor
{
meter=m;
centi=100*m;
}
void dm::insert()
{
int m;
cout<<"enter value in meter:";
cin>>m;
meter=m;
centi=100*m;
}
void dm::display()
{
cout<<"result in meter is:";
cout<<<"m"<
cout<<"result in centimeter is:";
cout<<<"cm"<
}
class db
{
int feet,inch;
friend dm operator +(dm &,db &);
public:
db();
db(int f);
void insert();
};
db::db()
{
feet=0;
inch=0;
}
db::db(int f)
{
feet=f;
inch=12*f;
}
void db::insert()
{
int f;
cout<<"enter value in feet:";
cin>>f;
feet=f;
inch=12*f;
}
dm operator +(dm &d1,db &d2)
{
dm d3;
d3.meter=d1.meter+(d2.feet/3.28);
d3.centi=100*(d3.meter);
return d3;
}
int main()
{
dm d5,d6;
db d4;
d5.insert();
d4.insert();
d6=d5+d4;
d6.display();
return 0;
}
Here Is Output: