Sem II C++ Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 40

C++ PROGRAMS

/* 1) a) C++ program to Display the Sum of the digits of a given Number*/

#include<iostream.h>
int main()
{
int val, num, sum = 0;
cout << "Enter the number : ";
cin >> val;
num = val;
while (num != 0)
{
sum = sum + num % 10;
num = num / 10;
}
cout << "The sum of the digits of "<< val<< " is "<<sum;
return 0;
}

Output:
Enter the number : 12345
The sum of the digits of 12345 is 15

/*2) b) Write a program to check whether the given number is Armstrong or not.
A positive integer is called an Armstrong number if the sum of cubes of individual digit is equal to that
number itself. For example:153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3 */
#include <iostream.h>
int main()
{
int origNum, num, digit, sum = 0;
cout << "Enter a positive integer: ";
cin >> origNum;
num = origNum;
while(num != 0)
{
digit = num % 10;
sum += digit * digit * digit;
num /= 10;
}
if(sum == origNum)
cout << origNum << " is an Armstrong number.";
else
cout << origNum << " is not an Armstrong number.";
return 0;
}
output:
Enter a positive integer: 371
371 is an Armstrong number.

/* 1) c) print all the prime number between 1 to n.*/

#include<iostream>
#include<conio.h>
void main()
{
clrscr();
int i,j,n,flag;
cout<<"Enter max. limit:";
cin>>n;
for(i=2;i<=n;i++)
{
flag=0;
for(j=2;j<i;j++)
{
if(i%j==0)
flag++;
}
if(flag==0)
cout<<i<<"\t";
}
getch();
}
Output:
Enter max. limit: 13
2 3 5 7 11 13

/* 2)Write a program to find largest and smallest elements in a given list of numbers and
sort the given list */
#include<iostream.h>
#include<conio.h>
void main()
{
int i,temp,j,n,a[100];
clrscr();
cout<<"enter the number of elements you want to insert:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter element:"<<i++<<":";
cin>>a[i];
}
cout<<"\n data before sorting:";
for(j=0;j<n;j++)
{
cout<<a[j];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\n data after sorting:";
for(j=0;j<n;j++)
{
cout<<a[j];
}
cout<<"\n smallest:"<<a[0];
cout<<"\n largest:"<<a[n-1];
getch();
}
Output:
Enter the number of elements you want to insert:5
Enter element 1: 13
Enter element 2:27
Enter element 3:7
Enter element 4:18
Enter element 5: 2
Data before sorting: 13277182
Data after sorting:27131827

/* 3) Write a menu driven program that can perform the following functions on strings.
(Use overloaded operators where possible). (Do not use predefined string class )
1. Compare two strings for equality (== operator)
2. Check whether first string is smaller than the second (<= operator)
3. Copy the string to another
4. Extract a character from the string (Overload [])
5. Reverse the string
6. Concatenate two strings (+ operator) */

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
class word
{
char str[20];
public:
void getdata();
void display();
int operator ==(word);
int operator <=(word);
void copy(word);
char operator[](int);
void reverse();
void operator +=(word);
};
void word :: getdata()
{
cin>>str;
}
void word :: display()
{
cout<<endl<<"\t"<<str<<endl;
}
int word :: operator ==(word s)
{
if(strcmp(str,s.str)==0)
return(1);
return(0);
}
int word :: operator <=(word s)
{
if(strcmp(str,s.str)<0)
return(1);
return(0);
}
void word :: copy (word s)
{
strcpy(str,s.str);
}

char word :: operator [](int x)


{
return(str[x-1]);
}
void word :: reverse()
{
strrev(str);
}
void word :: operator +=(word s)
{
strcat(str,s.str);
}
void main()
{
clrscr();
word a,b;
char c;
int x,choice;
do
{
cout<<"\t********************************************"<<endl;
cout<<"\t MENU "<<endl;
cout<<"\t********************************************"<<endl;
cout<<"\t1. Equality(==)"<<endl;
cout<<"\t2. Smaller(<=)"<<endl;
cout<<"\t3. Copy"<<endl;
cout<<"\t4. Extract([])"<<endl;
cout<<"\t5. Reverse"<<endl;
cout<<"\t6. Concatenate"<<endl;
cout<<"\t7. Exit"<<endl;
cout<<"\t********************************************"<<endl;
cout<<endl;
cout<<"\tEnter your Choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<endl<<"\tEnter first string :";
a.getdata();
cout<<endl<<"\tEnter second string :";
b.getdata();

if(a==b)
cout<<endl<<"\tStrings are equal\n";
else
cout<<endl<<"\tStrings are not same\n";
break;
case 2:
cout<<endl<<"\tEnter first string :";
a.getdata();
cout<<endl<<"\tEnter second string :";
b.getdata();
if(a<=b)
cout<<endl<<"\tString first is lesser then second"<<endl;
else
cout<<endl<<"\tString second is lesser then first"<<endl;
break;
case 3:
cout<<endl<<"\tEnter string :";
b.getdata();
a.copy(b);
cout<<endl<<"\tThe copied string is ";
a.display();
break;
case 4:
cout<<endl<<"\tEnter string :";
a.getdata();
cout<<endl<<"\tEnter position of character to extract : ";
cin>>x;
c = a[x];
cout<<endl<<"\tThe extracted string is : "<<c;
break;
case 5:
cout<<endl<<"\tEnter string :";
b.getdata();
b.reverse();
b.display();
break;
case 6:
cout<<endl<<"\tEnter first string :";
a.getdata();
cout<<endl<<"\tEnter second string :";
b.getdata();
a+=b;
a.display();
break;
case 7:
cout<<endl<<"\tCreated by Jayesh P. Mehta...";
getch();
exit(1);
default:
cout<<endl<<"\tEnter proper choice...";
getch();
break;
}
getch();
clrscr();
}
while(choice!=8);
getch();
}

Output:
********************************************
MENU
********************************************
1. Equality(==)
2. Smaller(<=)
3. Copy
4. Extract([])
5. Reverse
6. Concatenate
7. Exit
********************************************
Enter your Choice :1
Enter first string: icecream
Enter second string: biscuit
Strings are not same

/*4)Write a program using friend functions and inline functions*/

#include<iostream.h>
#include<conio.h>
class base
{
int val1,val2;
public:
void get()
{
cout<<”Enter two values:”;
cin>>val1>>val2;
}
friend float mean(base ob);
};
float mean(base ob)
{
return float(ob.val1+ob.val2)/2;
}
void main()
{
clrscr();
base ob;
obj.get();
cout<<”\n mean value is:”<<mean(obj);
getch();
}

Output:
Enter two values:10 20
Mean value is:15

/* 5) Write a program to calculate area of rectangle, circle and square using constructor.
*/

#include<iostream.h>
class Area
{
private:
int a,b;
float c;
public:
void rectangle()
{
cout<<"area of rectangle is "<<a*b<<endl;
}
void circle()
{
cout<<"area of circle is ";
c=3.14*a*a;
cout<<c <<endl;
}
void square()
{
cout<<"area of square is" ;
c=a*a;
cout<<c <<endl;
}
Area(int x,int y)
{
cout<<"constructor called "<<endl;
a=x;
b=y;
}
Area(int x)
{
cout<<"constructor called "<<endl;
a=x;
}
};

int main()
{
Area obj(4,6);//1st call implicit call
Area obj1=Area(2,3);//2nd call Explicit
Area obj2=8;//if one argument can pass argument directly
obj1.rectangle();
obj2.circle();
obj2.square();
return 0;
}

Output:
Constructor called
Constructor called
Constructor called
Area of rectangle is:6
Area of circle is:200.960007
Area of square is:64

/* 6) WAP TO IMPLEMENT THE COPY CONSTRUCTOR */

#include <iostream.h>
Class Demo
{
private:
int X;
int Y;
public:
Demo (int a, int b);
Demo (const Demo &d);
void Display();
};
Demo:: Demo(int a, int b)
{
X = a;
Y = b;
}
Demo:: Demo(const Demo &d)
{
X = d.X;
Y = d.Y;
}
voidDemo:: Display()
{
cout<<endl<< "X: "<< X;
cout<<endl<< "Y: "<< Y <<endl;
}

int main()
{
Demo d1(10,20) ;
cout<<endl<<"D1 Object: "<<endl;
cout<< "Value after initialization : ";
d1.Display();
Demo d2 = Demo(d1);
cout<<endl<< "D2 Object: "<<endl;
cout<< "Value after initialization : ";
d2.Display();
return 0;
}

Output:
D1 Object:
Value after initialization:
X: 10
Y: 20
D2 Object:
Value after initialization:
X: 10
Y: 20

/* 7.)Write a program to demonstrate single inheritance and multiple inheritance*/

(i)Single Inheritance
#include<iostream.h>
class AddData //Base Class
{
protected:
int num1, num2;
public:
void accept()
{
cout<<"\n Enter First Number : ";
cin>>num1;
cout<<"\n Enter Second Number : ";
cin>>num2;
}
};
class Addition: public AddData //Class Addition – Derived Class
{
int sum;
public:
void add()
{
sum = num1 + num2;
}
void display()
{
cout<<"\n Addition of Two Numbers : "<<sum;
}
};
int main()
{
Addition a;
a.accept();
a.add();
a.display();
return 0;
}

Output:
Enter first number:7
Enter second number:4
Addition of two numbers:11

7(ii)Multiple Inheritance
#include<iostream.h>
class mammal
{
public:
mammal()
{
cout<<”Mammals can give direct birth.”<<endl;
}
};
class wingedanimal
{
public:
wingedanimal()
{
cout<<”Winged animal can flap.”<<endl;
}
};
class bat: public mammal,public wingedanimal
{
};

int main()
{
bat b1;
return 0;
}

Output:
Mammals can give direct birth. Winged animal can flap.

/*8) Write a program to demonstrate hierarchical inheritance and multipath


inheritance(using virtual functions)*/
(i) C++ Program For Hierarchical Inheritance
#include<iostream.h>
#include<conio.h>

class polygon
{
protected:
int width, height;
public:
void input(int x, int y)
{
width = x;
height = y;
}
};

class rectangle : public polygon


{
public:
int areaR ()
{
return (width * height);
}
};

class triangle : public polygon


{
public:
int areaT ()
{
return (width * height / 2);
}
};

void main ()
{
clrscr();
rectangle rect;
triangle tri;
rect.input(6,8);
tri.input(6,10);
cout <<"Area of Rectangle: "<<rect.areaR()<< endl;
cout <<"Area of Triangle: "<<tri.areaT()<< endl;
getch();
}

Output:
Area of rectangle:48
Area of triangle:30

(ii) Multipath Inheritance


#include <iostream.h>
#include <conio.h>
class person
{
public:
char name[100];
int code;
void input()
{
cout<<"\nEnter the name of the person : ";
cin>>name;
cout<<endl<<"Enter the code of the person : ";
cin>>code;
}
void display()
{
cout<<endl<<"Name of the person : "<<name;
cout<<endl<<"Code of the person : "<<code;
}
};

class account:virtual public person


{
public:
float pay;
void getpay()
{
cout<<endl<<"Enter the pay : ";
cin>>pay;
}
void display()
{
cout<<endl<<"Pay : "<<pay;
}
};
class admin:virtual public person
{
public:
int experience;
void getexp()
{
cout<<endl<<"Enter the experience : ";
cin>>experience;
}
void display()
{
cout<<endl<<"Experience : "<<experience;
}
};
class master:public account,public admin
{
public:
char n[100];
void gettotal()
{
cout<<endl<<"Enter the company name : ";
cin>>n;
}
void display()
{
cout<<endl<<"Company name : "<<n;
}
};
void main()
{
master m1;
m1.input();
m1.getpay();
m1.getexp();
m1.gettotal();
m1.person::display();
m1.account::display();
m1.admin::display();
m1.display();
getch();
}

Output:
Enter the name of the person: xyz
Enter the code of the person:999
Enter the pay:20000
Enter the experience:5
Enter the company name: Infosys
Name of the person: xyz
Code of the person:999
Pay:20000
Experience:5
Company name: Infosys

/*9)Write a program to demonstrate static polymorphism


using method overloading.*/
#include <stdio.h>
class Geeks
{
public:
void func(int x)
{
cout << "value of x is " << x << endl;
}

void func(double x)
{
cout << "value of x is " << x << endl;
}
void func(int x, int y)
{
cout << "value of x and y is " << x << ", " << y <<endl;
}
};
int main()
{

Geeks obj1;
obj1.func(7);
obj1.func(9.132);
obj1.func(85,64);
return 0;
}
Output:
Value of x is 7
Value of y is 9.132
Value of x and y is 85,64

/*10 write a program to demonstrate dynamic polymorphism using method overriding


and dynamic method dispatch*/
(i) Method Overriding

#include<iostream.h>
#include<conio.h>
class baseclass
{
public:
{
void display()
{
cout<<"\n\tThis is Display() method of BaseClass";
}
void Show()
{
cout<<"\n\tThis is Show() method of BaseClass";
}
};
class DerivedClass : public BaseClass
{
public:
void Display()
{
cout<<"\n\tThis is Display() method of DerivedClass";
}
};
void main()
{
DerivedClass Dr;
Dr.Display();
Dr.Show();
}
OUTPUT:
This is Display() method of DerivedClass
This is Show() method of BaseClass

10.(ii)Method Dispatch
#include<iostream.h>
class Base
{
public:
Base()
{
cout<<”\n\nBase ctr”;
fun();
}
virtual void fun()
{
cout<<”\n\nBase’s fun()”;
}
};
class Derived:public Base
{
public:
Derived()
{
cout<<”\n\nDerived ctr”;
fun();
}
virtual void fun()
{
cout<<”\n\nDerived’s fun()”;
}
};
int main()
{
Derived obj;
return 0;
}
output:
Base ctr
Base's fun()
Derived ctr
Derived's fun()

/*11(i) C++ program to add two numbers using function template. */


#include <iostream.h>
#include <conio.h>
template<class t1,class t2>
void sum(t1 a,t2 b) // defining template function
{
cout<<"Sum="<<a+b<<endl;
}
int main()
{
int a,b;
float x,y;
cout<<"Enter two integer data: ";
cin>>a>>b;
cout<<"Enter two float data: ";
cin>>x>>y;
sum(a,b); // adding two integer type data
sum(x,y); // adding two float type data
sum(a,x); // adding a float and integer type data
getch();
return 0;
}
Output:
Enter two integer data:98 65
Enter two float data: 56 57
Sum=163
Sum=113
Sum=154

/*11(ii) C++ program to use class template */


#include<iostream.h>
#include<conio.h>
template<class t1,class t2>
class sample
{
t1 a;
t2 b;
public:
void getdata()
{
cout<<”Enter a and b”;
cin>>a>>b;
}
void display()
{
cout<<”Displaying values”<<endl;
cout<<”a=”<<a<<endl;
cout<<”b=”<<b<<endl;
}
};
int main()
{
sample<int,int>s1;
sample<int,char>s2;
sample<int,float>s3;
cout<<”Two integer data:”<<endl;
s1.getdata();
s1.display();
cout<<”Integer and character data:”<<endl;
s2.getdata();
s2.display();
cout<<”Integer and float data:”<<endl;
s3.getdata();
s3.display();
getch();
return 0;
}

Output:
Two integer data:
Enter a and b: 3 4
Displaying values:
a=3
b=4
Integer and character data
Enter a and b: 3 c
Displaying values:
a=3
b=c
Integer and float data:
Enter a and b: 4 1.2
Displaying values:
a=4
b=1.3

/* 12)Write a program to menu driven program for accepting two numbers and perform
calculator operations addition, subtraction, multiplication, division and remainder usin
g function template*/
#include<iostream.h>
template<class T>
T add(T a,T b)
{
cout<<"after addtion:"<<a+b<<endl;
return 0;
}
template<class T>
T sub(T a,T b)
{
cout<<"after subtraction:"<<a-b<<endl;
return 0;
}
template<class T>
T mul(T a,T b)
{
cout<<"After multiplication:"<<a*b<<endl;
return 0;
}
template<class T>
T divide(T a,T b)
{
cout<<"after division:"<<a/b<<endl;
return 0;
}
template<class T>
T remainder(T a,T b)
{
cout<<"after modulus:"<<a%b<<endl;
return 0;
}
int main()
{
int x,y;
cout<<"enter first number:";
cin>>x;
cout<<"enter second number:";
cin>>y;
cout<<"press + for addition"<<endl;
cout<<"press - for subtraction"<<endl;
cout<<"press * for multiplication"<<endl;
cout<<"press / for division"<<endl;
cout<<"press % for remainder"<<endl;
char choice;
cout<<"enter your choice";
cin>>choice;
if(choice=='+')
add(x,y);
else if(choice=='-')
sub(x,y);
else if(choice=='*')
mul(x,y);
else if(choice=='/')
divide(x,y);
else if(choice=='%')
remainder(x,y);
return 0;
}
Output:
Enter first number:4
Enter second number:6
press + for addition
press - for subtraction
press * for multiplication
press / for division
press % for remainder
Enter your choice +
After addition 10

/*13) Write a program to demonstrate exception handling*/


#include<iostream.h>
#include<conio.h>
void main()
{
int n1,n2,result;
cout<<"\nEnter 1st number : ";
cin>>n1;
cout<<"\nEnter 2nd number : ";
cin>>n2;
try
{
if(n2==0)
throw n2;
else
{
result = n1 / n2;
cout<<"\nThe result is : "<<result;
}
}
catch(int x)
{
cout<<"\nCan't divide by : "<<x;
}
cout<<"\nEnd of program.";
}
Output :
Enter 1st number : 45
Enter 2nd number : 0
Can't divide by : 0
End of program

/* 14)Write a program to demonstrate various input output manipulators*/


#include<iostream.h>
#include<iomanip.h>
int main()
{
cout<<"using setw()..\n";
cout<<setw(10)<<11<<"\n";
cout<<setw(10)<<2222<<"\n";
cout<<setw(10)<<33333<<"\n";
cout<<setw(10)<<4<<endl;
cout<<"using setw() and setfill() [type-I]..\n";
cout<<setfill('0');
cout<<setw(10)<<11<<"\n";
cout<<setw(10)<<2222<<"\n";
cout<<setw(10)<<33333<<"\n";
cout<<setw(10)<<4<<endl;
cout<<"using setw() and setfill() [type-II]..\n";
cout<<setfill('_')<<setw(10)<<11<<"\n";
cout<<setfill('*')<<setw(10)<<2222<<"\n";
cout<<setfill('@')<<setw(10)<<33333<<"\n";
cout<<setfill('#')<<setw(10)<<4<<endl;
return(0);
}
Output:
Using setw…..
11
2222
33333
4
Using setw() and setfill[type-I]….
0000000011
0000002222
0000033333
0000000004
Using setw() and setfill[type-II]….
--------11
******2222
@@@@@33333
#########4

/* 15) Write C++ programs to implement the Stack ADT using an array */
#include<iostream>
#include<conio.h>
#include<stdlib.h>
class stack
{
int stk[5];
int top,i;
public:
stack()
{
top=-1;
}
if(top > 4)
{
cout <<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty";
return;
}
for(i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};
main()
{
int ch;
stack st;
while(1)
{
cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return (0);
}

OUTPUT
1.push 2.pop 3.display 4.exit
Enter ur choice2
stack under flow
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element2
inserted2
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element3
inserted3

/* 16) Write a program to demonstrate array of objects*/


#include<iostream.h>
#include<conio.h>
class rec
{
private:
int I;
int b;
public:
rec(int a,int c)
{
I=a;
b=c;
}
void put()
{
cout<<"Area is : "<<I*b <<endl;
}
};
void main()
{
clrscr();
rec obj[3]={rec(3,6),rec(2,5),rec(5,5)};
cout<<"Displaying Areas of Rectangles : \n";
for(int i=0;i<3;i++)
obj[i].put();
getch();
}
Output:
Displaying areas of rectangle:
Area is:18
Area is:10
Area is:25

You might also like