C++ code

Software use:- Code::Block 16.01


1. C++ program to add 1 to 10 numbers using i)while ii)do while ii)for loop

#include<iostream>
using namespace std;
int main()
{
int x,sum=0;
for(x=1;x<=10;x++)
{
sum=sum+x;
}
cout << "Addition of First 10 Numbers" <<endl;
cout << "Sum [for] = " << sum <<endl;
x=sum=0;
while(x<=10)
{
sum=sum+x;
x++;
}
cout << "Sum [while] = " <<sum <<endl;
x=sum=0;
do
{
sum = sum+x;
x++;
}
while (x<=10);
cout << "Sum [do-while] = " <<sum <<endl;
return 0;
}

Output-





2. C++ program to find whether number entered by user is prime number or not. 

#include<iostream>
using namespace std;
int main()
{ int i,num,x;
cout << "Enter the number : " << endl;
cin>>num ;
for(i=2;i<num;i++)
{ x=num%i;
if(x==0)
  • {
break;
}
}
if(x==0)
cout << "Entered Number is a not Prime Number " << endl;
else
cout << "Entered Number is Prime number" << endl;
return 0;
}

Output-


3.C++ program to calculate area of Circle,Square,Triangle.Choice and Dimensions of the shape will be entered by user. Use enum data type in a program.

An enumeration is a user-defined data type that consists of integral constants.
To define enumeration enum keyword must be use.

#include<iostream>
using namespace std;
int main()
{
enum shape{triangle,square,circle};
int h=0,b=0,s,a,r;
float area=0.0;
cout<<"Enter the shape 0 for triangle,1 for square,2 for cicle"<<endl;
cin>>a;
switch(a)
{
case triangle:
cout<<"Enter the height and base"<<endl;
cin>>h>>b;
area=0.5*b*h;
cout<<"Area of Triangle:"<<area<<endl;
break;
case square:
cout<<"Enter the side of square"<<endl;
cin>>s;
area=s*s;
cout<<"Area of Triangle:"<<area<<endl;
break;
case circle:
cout<<"Enter the radius"<<endl;
cin>>r;
area=3.14*r*r;
cout<<"Area of Triangle:"<<area<<endl;
break;
default:
cout<<"Entered wrong value";
}
return 0;
}

Output-




4. C++ program to divide two integers. Use the concept of type casting.

Type casting-It is a making a variable of one type to act like another type.

#include<iostream>
using namespace std;
int main()
{
int a=9,b=2;
float c;
c=(float) a/b;
cout << "a/b is "<< c;
}  


5. C++ program to swap values of two variables .To swap two variables write a function. Pass argument to function using 

  •  Value 
  •  Pointer 
  •  Reference variable 

#include<iostream>
void swap1(int,int);
void swap2(int *,int *);
void swap3(int &,int &);
using namespace std;
int main()
{
int a=0,b=0,c=0,d=0;
cout << "Enter Two numbers : "<< endl;
cin >>a>>b;
c=a;d=b;
cout <<endl<<" x="<<a <<" and y="<<b<<endl << endl;
cout << "By using value";
swap1(a,b);
cout << endl <<"In MAIN function ";
cout << endl << "x=" << a <<" and y=" << b << endl;
cout << "So no change in variable by passing argument by value" <<endl;
cout << endl << "By using Pointer";
swap2(&a,&b);
cout << endl << "By using Reference value";
swap3(c,d);
cout << endl << "x=" << a << " and y=" << b << endl;
return 0;}

void swap1(int x,int y)
{
int t;
t=x;
x=y;
y=t;
cout << endl << "x=" << x << " and y=" << y << endl;
}
void swap2(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
cout << endl << "x=" << *x << " and y=" << *y << endl;
}
void swap3(int &x,int &y)
{
int t;
t=x;
x=y;
y=t;
cout << endl << "x=" << x << " and y=" << y << endl;
}

Output-


6.Write C++ program to demonstrate the concept of function overloading

#include<iostream>
#include<cstring>
using namespace std;
int add (int a,int b);
float add (int a,int b,float c);
char* add (char[],char[]);
int main ()
{
int a=5,b=6;char d[]="yc", e[]="ce";
float c=2.5;
cout<< add (10,6)<<endl;
cout<< add (5,6,2.5)<<endl;
cout<< add(d,e);
return 0;
}
int add (int a,int b)
{
return(a+b);
}
float add (int a,int b,float c)
{
return (a+b+c);
}
char* add (char d[],char e[])
{
return(strcat(d,e));

}

Output-

7.Program in C++ To implement concepts of Class, Object And Constructor.

 Write a class student. Data members of class are name,rollno,and marks of Physics chemistry,maths. Methods of class are getData(), display(),Average(), Topper(). getData() reads the data of all attribute.display()displays the data of all attribute. Average() returns average of marks of all subjects.Find the topper among the students using suitable method.Write the appropriate main function.

#include<iostream>
 #include<string.h>
 using namespace std;
 class student
 {char name[20];
 int phy,che,math;
 public:
 float per;
 static int temp;
 int Roll_no;
 //Default Constructor along with the custom constructor
 student()
 {
 //cout <<"Default Constructor is called having *** student y; *** ";
 cout<<"Enter name and marks of phy,chem,maths"<<endl;
 cin>>name>>phy>>che>>math;
 }
student(char* q ,int a, int b, int c)
 {
 strcpy(name,q);
 Roll_no=temp++;
 phy=a; che=b; math=c;
 }
float percent()
 {
 per=((phy+che+math)/5.0);
 return(per);
 }
void display()
{
cout <<"Name " <<name<<endl;
cout<<endl<<"Roll No "<<Roll_no<<endl;
cout<<"Physics "<<phy<<"\tChemistry "<<che <<"\tMath "<<math<<endl;
}
student top(student m)
{ if(percent()>m.percent())
return*this;
else
return m;
}
};
int student::temp=1;
int main()
{
int r,i;
cout <<"Enter the number of students :"<<endl;
cin >> r;
student t("xyz",0,0,0);
student s[r];
for(i=0;i<r;i++)
{
 t=s[i].top(t);
}
cout <<endl<<"Topper is "<<endl;
t.display();
return 0;
}
Output-



8.Program in C++ to implement the concept of public Inheritance.

#include<iostream>
using namespace std;
class base
{
 protected:
 //private:
 int x,y;//x and y can not be inherited if it is private
 public:
 base(int a=0,int b=0)
 {
 x=a;
 y=b;
 }
 void display()
 {
 cout<<"x= "<<x<<endl<<"y= "<<y<<endl;
 }
};
class derived: public base
{
 int z;// x and y become protected data member of derived class
 // as it is protected in base class and inheritance is protected
 public:
 derived(int c=0,int a=0,int b=0):base(a,b)//constructor is inherited
 {
 z=c;
 }
 float avg()
 {
 float temp =(x+y+z)/3.0;
 return(temp);
 }
 float disply_derived()
 {
 base::display();//function is inherited from base function
 cout<<"z ="<<z<<endl;
 }
};
int main ()
{
 derived S1(10,10,10),y;
 y.disply_derived();
 cout<<"average of S1 ="<<S1.avg()<<endl;
 //display()function is not accessible in main if inheritance is private or protected
 //as it will become private function of derived class
 S1.display();
 return 0;
}

Output-


9. C++ program to implement the concept of Virtual function 


#include <iostream>
using namespace std;
class base
{
public :
 virtual
 void display (){cout<<"base"<<endl;}
};
class derivedA : public base
{
public :
 void display ()
 {
 cout<< "derivedA"<<endl;
 }
};
class derivedB : public base
{
public :
 void display ()
 {
 cout<< "derivedB"<<endl;
 }
};
int main()
{
 base a,*p;
 derivedA b,c,d;p=&b;
 derivedB e,f,g,*q;
 base * x [6]= {&b,&c,&d,&e,&f,&g};
 for (int i=0;i<6;i++)
 x[i]->display();
 p->display();
 return 0;
}


Output-




10. Program in C++ To implement the concept of operator overloading.

Write a class ͞complex͟. Data members of class are two float variables used to store real and imaginary parts of complex number. Object of the class represents complex number. Add suitable member function to the class to overload following operators.
 i)+,*(addition, multiplication)
 ii)++(pre increment) 
iii)>(greater than)

#include<iostream>
#include<cmath>
using namespace std;
class complex
{
 private:
 float x,y;
 public:
void Display ()
{
cout<<x<<"+i"<<y<<endl<<endl;
}
complex(float a=0.0,float b=0.0)
{
x=a;y=b;
}
complex operator+(complex p)
{
complex temp; temp.x=x+p.x; temp.y=y+p.y;
return temp;
}
complex operator++()
{
x++;y++; return *this;
}
bool operator>(complex p)
{float temp1=sqrt(x*x+y*y);
float temp2=sqrt((p.x)*(p.x)+(p.y)*(p.y)); if(temp1>temp2)
return 1;
else
return 0;
}
complex operator*(complex p)
{
complex temp;
temp.x=(x*(p.x))-(p.y*y);
temp.y=y*p.x+x*p.y;
return temp;
}
};
int main()
{
complex a(1,1); complex b(2,2); complex c;
cout << "a is";a.Display();
cout << "b is";b.Display();
cout << "c is";c.Display();
c=a+b;
cout<<"For c=a+b"<<endl;
 c.Display();
c=a*b;
cout <<"For c=a*b"<<endl;
 c.Display();
++a;
cout <<"For ++a"<<endl;
 a.Display();
if(a>b)
{cout <<"a is greater where "<<endl;a.Display();}
else
{cout << "b is greater where "<<endl;b.Display();}
return 0;
}

Output-




11. Program in C++ to understand the concepts of friend function.


#include<iostream>
using namespace std;
class B; // forward declaration
class A {
 private:
 int data;
 public:
 A(){data=12;}
 friend int func(A,B); //friend function Declaration
};
class B {
 private:
 int data;
 public:
 B(){ data=13; }
 friend int func(A,B); //friend function Declaration
 };
/*Function func() is the friend function of both classes A and B. So, the private data of both
class can be accessed from this function.*/
int func(A d1,B d2)
{
 return (d1.data+d2.data);
}int main()
 {
 A a;
 B b;
 cout<<" Addition is:"<<func(a,b);
 return 0;
 }

Output-
Addition is 25


12.Program in C++ to implement the concept of stack using array.

#include<iostream>
#include<string.h>
#define STACKSIZE 10
using namespace std;
class stack
{
private:
int a[STACKSIZE];
int tos;
public:
stack();
void push(int x);
int pop();
bool is_empty();
bool is_full();
int size();
void display();
};
stack ::stack()
{tos=0;}
void stack::push(int x)
{
if(!is_full())
a[tos++]=x;
else
cout <<"stack is full"<<endl;
}
int stack::pop()
{
if(!is_empty())
return(a[--tos]);
else
cout <<"Stack is empty"<<endl;
}
bool stack :: is_empty()
{
if(tos==0)
return true;
return false;
}
bool stack :: is_full()
{
if(tos==STACKSIZE)
return true;
else
return false;
}
int stack::size()
{
return tos;
}
void stack::display()
{
if(is_empty())
{
cout << "No elements to display" << endl;
}
else
{
for(int i=0;i<tos;i++)
{
cout << a[i] << " "<< endl;
}
}
}
int main()
{
stack yourstack;
yourstack.push(1);
yourstack.push(2);
yourstack.push(3);
yourstack.push(4);
yourstack.push(5);
yourstack.push(6);
cout<<"poped element in stack are"<<yourstack.pop()<<endl;
cout<<"poped element in stack are"<<yourstack.pop()<<endl;
cout<<"poped element in stack are"<<yourstack.pop()<<endl;
cout<<"stacksize is"<<yourstack.size()<<endl;
yourstack.display();
}
Output-



13.C+ Program to implement the concept of queue using array.

#include<iostream>
#include<string.h>
#define QSIZE 10
using namespace std;
class queue
{
private:
int a[QSIZE];
int tos,bos;
public:
queue();
void Enqueue(int x);
int Dequeue();
bool is_empty();
bool is_full();
int size();
void display();
};
queue ::queue()
{
tos=0;bos=0;
}
void queue::Enqueue(int x)
{
if(!is_full())
{
a[tos++]=x;
}
else
cout <<"queue is full"<<endl;
}
int queue::Dequeue()
{
if(!is_empty())
return(a[bos++]);
else
cout <<"Queue is empty"<<endl;
return -1;
}
bool queue :: is_empty()
{
if(tos==0) return true;
else
return false;
}
bool queue :: is_full()
{
if(tos==QSIZE) return true;
else
return false;
}
int queue::size()
{
return (tos-bos);
}
void queue::display()
{
if(is_empty())
{
cout << "No elements to display" << endl;
}
else
{
cout<<"Queue elements : \n";
for(int i=bos;i<tos;i++)
{
cout << a[i] << " "<< endl;
}
}
}


 int main()
{
queue yourqueue;
yourqueue.Enqueue(1);
yourqueue.Enqueue(2);
yourqueue.Enqueue(3);
yourqueue.display();
cout<<"Dequeue element is"<<yourqueue.Dequeue()<<endl;
yourqueue.display();
 return 0;
 }


Output-





13. C++ Program to implement the concept of stack using link list. 

     Implement recursive member function display () which displays all the data members of link list .


#include<iostream>
using namespace std;
struct node
{
int data; node*previous;
};
node *head;
class stackLL
{
public:
stackLL()
{ head=NULL; }
void push(int x);
int pop();
bool is_empty();
void display(node*);
};
void stackLL::push(int x)
{
node *temp=new node; temp->data=x;
temp->previous=head; head=temp;
}
int stackLL::pop()
{
if(is_empty())
{
cout<<"Empty";
}
else
{
int z=head->data;
node *temp=head; head=head->previous; delete temp; return z;
}
}
bool stackLL :: is_empty()
{
if(head==NULL)
return true;
else
return false;
}
void stackLL::display(node* temp =head)
{
if(temp==NULL);
else
{
cout<<temp->data<<endl;
display( temp->previous);
}
}
int main()
{
stackLL yourstack;
yourstack.push(1);
yourstack.push(2);
yourstack.push(3);
yourstack.push(4);
yourstack.push(5);
cout<<"stack elements are"<<endl;
yourstack.display();
cout<<"poped element in stack are "<<yourstack.pop()<<endl;
cout<<"poped element in stack are "<<yourstack.pop()<<endl;
cout<<"poped element in stack are "<<yourstack.pop()<<endl;
cout<<"stack elements are"<<endl;
yourstack.display();
}

Output-



14.C++ program to implement the concept of exception handling .

Function to get density of a material if mass and volume is provided by user. Throw an exception if user enters value of mass or volume equal to zero.  


#include <iostream>
using namespace std;
float density (int m,int v);
int main()
{
cout<<"enter value of mass and volume"<<endl;
int x,y;
cin>> x>> y;
d:
try
{
if(x==0||y==0)
    throw x;
else
density (x,y);
}
catch(int)
{
cout<<"exception caught"<<endl;
cout<<"enter correct value of mass and voume"<<endl;
cin>>x>>y;
goto d;
density(x,y);
}
return 0;
}
float density (int m,int v)
{
float d;
d=float(m)/v;
cout<< "density="<<d;
return(d);
}


Output-





Comments

Popular posts from this blog

Python Code

Perl