Sem II C++ Programs
Sem II C++ Programs
Sem II C++ Programs
#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.
#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);
}
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
#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
#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
(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.
class polygon
{
protected:
int width, height;
public:
void input(int x, int y)
{
width = x;
height = y;
}
};
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
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
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
#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()
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
/* 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