Professional Documents
Culture Documents
(BTCS 302-18)
ROLLNO: 1902168
SEMESTER: 3rd
GROUP: 3V1
MS NAVDEEP KAUR
(ASSISTANT PROFESSOR)
4 Program to calculate area of triangle, rectangle, square and circle using switch statement. 10-12
23 Program to print the element which occurs more than one time in an array. 41-42
37 Program that uses a class in which member functions are defined outside the class. 61-62
PROGRAM:
#include<iostream>
int main()
int choice;
"\n1 = Addition"
"\n2 = Subtraction"
"\n3 = Multiplication"
"\n4 = Division"
"\n5 = Squares"
"\n6 = exit"
"\n\nChoice: ";
"\nChoice: "<<endl;
case 1:
x = num1 + num2;
break;
case 2:
x = num1 - num2;
break;
case 3:
x = num1 * num2;
break;
while(num2 == 0)
x = num1 / num2;
break;
case 5:
x = num1 * num1;
break;
case 6: return 0;
OUTPUT:
PROGRAM:
#include <iostream>
int main(){
int num=66;
else {
return 0;
OUTPUT:
PROGRAM:
#include <iostream>
int main(){
int num=90;
return 0;
OUTPUT:
PROGRAM:
#include<iostream>
#include<math.h>
int main()
int ch;
cout<<"1.Area Of Circle";
cout<<"\n2.Area Of Rectangle";
cin>>ch;
switch(ch)
case 1:
cin>>radius;
area=3.14159*radius*radius;
10
break;
case 2:
cin>>a>>b;
area=a*b;
break;
case 3:
cin>>a>>b>>c;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
break;
break;
11
OUTPUT:
12
PROGRAM:
#include<iostream>
int main(){
int i,n;
cin>>n;
for(i=1;i<=10;++i)
cout<<endl;
return 0;
OUTPUT:
13
PROGRAM:
#include<iostream>
int main()
int i,j,n,s;
cin>>n;
s=n;
for(i=1;i<=n;i++)
s=s-i;
for(j=i;j>=1;j--)
while(s>0)
cout<<"*";
s--;
cout<<j;
14
cout<<endl;
s=n;
return 0;
OUTPUT:
15
PROGRAM:
#include <iostream>
int main() {
cin >> n;
while(n != 0) {
remainder = n%10;
n /= 10;}
return 0;
OUTPUT:
16
PROGRAM:
#include <iostream>
int main() {
temp = a;
a = b;
b = temp;
cout<<"Value of a is "<<a<<endl;
cout<<"Value of b is "<<b<<endl;
return 0;
OUTPUT:
17
PROGRAM:
#include <iostream>
int main() {
int n1=0,n2=1,n3,i,number;
cin>>number;
for(i=2;i<number;++i) {
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3; }
cout<<endl;
return 0; }
OUTPUT:
18
PROGRAM:
#include <iostream>
int res = 1, i;
res *= i;
return res;
int main() {
int num = 5;
return 0; }
OUTPUT:
19
PROGRAM:
#include <iostream>
int main()
n = num;
do
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
20
else
cout<<endl;
return 0;
OUTPUT:
21
PROGRAM:
#include <iostream>
int main()
originalNum = num;
while (originalNum != 0)
originalNum /= 10;
if (result == num)
else
22
cout<<endl;
return 0;
OUTPUT:
23
PROGRAM:
#include <iostream>
int main(){
char ascii;
int numeric;
cout << "Its ascii value is: " << (int) ascii << endl;
cout << "The ascii value of " << numeric << " is " << (char) numeric;
cout<<endl;
return 0;
OUTPUT:
24
PROGRAM:
#include <iostream>
int main(){
int x,y,sum;
float average;
cin>>x>>y;
sum=x+y;
average=sum/2;
cout << "The average of " << x << " and " << y << " is " << average << "." << endl;
OUTPUT:
25
PROGRAM:
#include <iostream>
int main() {
return 0;
26
OUTPUT:
PROGRAM: 16
AIM: Program to check whether number is prime or not.
PROGRAM:
#include <iostream>
int main(){
int n, i, c = 0;
if (n % i == 0) {
c++;}
if (c == 2){
else {
return 0;
27
OUTPUT:
PROGRAM: 17
AIM: Program to print days of a week.
PROGRAM:
#include<iostream>
int main(){
int week;
if(week == 1) {
else if(week == 2)
else if(week == 3)
28
else if(week == 4)
else if(week == 5)
else if(week == 6)
else if(week == 7)
else{
cout<<endl;
return 0;
29
OUTPUT:
PROGRAM: 18
AIM: Program to check whether a year is a leap year or not.
PROGRAM:
#include <iostream>
int main() {
int year;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
else
else
30
else
cout<<endl;
return 0;
OUTPUT:
31
PROGRAM:
#include<iostream>
int main()
int arr[20],i,n,sum=0;
cin>>n;
for(i=0;i<n;i++)
cin>>arr[i];
32
sum=sum+arr[i];
for(i=0;i<n;i++)
cout<<sum;
cout<<endl;
return 0;
OUTPUT:
33
PROGRAM:
#include<iostream>
int main()
int i,a[10],temp,j;
for(i=0;i<=5;i++)
cin>>a[i];
for(j=0;j<5;j++)
cout<<a[j];
34
for(i=0;i<=5;i++)
for(j=0;j<=5-i;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
for(j=0;j<5;j++)
cout<<a[j];
cout<<endl;
return 0;
OUTPUT:
35
PROGRAM:
#include <iostream>
int mn = a[0];
mn = min(mn, a[i]);
return mn;
36
int mx = a[0];
mx = max(mx, a[i]);
return mx;
int main()
int n;
cin >> n;
cout << "\n\n Enter the " << n << " elements of the array: \n\n";
37
cout << "\n\n The " << n << " elements of the array are : ";
cout << "\n\n\nThe Smallest element in the entered array is: " << mn;
cout << "\n\nThe Largest element in the entered array is: " << mx << "\n\n\n";
return 0;
OUTPUT:
38
PROGRAM:
#include<iostream>
int main()
cin>>size;
39
cin>>arr[i];
j=i-1;
i=0;
while(i<j)
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
cout<<arr[i]<<" ";
return 0;
OUTPUT:
40
PROGRAM:
#include <bits/stdc++.h>
41
if (mp[arr[i]] > 1) {
mp[arr[i]] = 0;
int main()
printRepeating(arr, n);
return 0;
OUTPUT:
42
PROGRAM:
#include<iostream>
int main()
int arr[20],i,n,even=0,odd=0;
cin>>n;
43
{ cin>>arr[i]; }
cout<<"Even number:";
if(arr[i]%2==0)
{ even++;
cout<<arr[i]<<" "; }
cout<<"\nOdd number:";
if(arr[i]%2!=0)
{ odd++;
cout<<arr[i]<<" "; }
return 0;
OUTPUT:
44
PROGRAM:
#include<iostream>
int main(){
int arr[20],i,n,even=0,odd=0;
45
cin>>n;
cin>>arr[i]; }
if(n%2!=0) {
else {
OUTPUT:
PROGRAM: 26
AIM: Program to add two matrices.
PROGRAM:
#include<iostream>
int main()
46
cin>>m>>n;
for(c=0;c<m;c++)
for(d=0;d<n;d++)
cin>>first[c][d];
for(c=0;c<m;c++)
for(d=0;d<n;d++)
cin>>second[c][d];
for(c=0;c<m;c++)
for(d=0;d<n;d++)
sum[c][d]=first[c][d]+second[c][d];
for(c=0;c<m;c++)
{ for(d=0;d<n;d++)
cout<<sum[c][d]<<"\t";
return 0;
OUTPUT:
47
PROGRAM:
#include<iostream>
int main()
48
string str;
cin>>str;
cout<<"Entered String="<<str;
int n=str.size();
cout<<"\nString Length="<<n<<endl;
return 0;
OUTPUT:
PROGRAM: 28
AIM: Program to find reverse of a string.
PROGRAM:
#include<iostream>
#include<string>
#include<bits/stdc++.h>
49
int main()
string str;
getline(cin,str);
reverse(str.begin(),str.end());
OUTPUT:
PROGRAM:29
AIM: Program to compare two strings.
PROGRAM:
#include <iostream>
#include<string>
#include<bits/stdc++.h>
50
int main() {
string stra,strb;
getline(cin,stra);
getline(cin,strb);
if(stra.compare(strb)==0)
else
OUTPUT:
PROGRAM: 30
AIM: Program to concatenate two strings.
PROGRAM:
#include <iostream>
#include<string>
#include<bits/stdc++.h>
51
int main()
string stra,strb,strc;
getline(cin,stra);
getline(cin,strb);
strc=stra+strb;
OUTPUT:
PROGRAM: 31
AIM: Program to check whether the character is a vowel or a consonant.
PROGRAM:
#include<iostream>
int main(){
52
int lower_case,upper_case;
cin>>ch;
lower_case=(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u');
upper_case=(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U');
if(lower_case||upper_case)
cout<<ch<<" is a Vowel.";
else
cout<<ch<<" is a Consonent."<<endl;
return 0; }
OUTPUT:
PROGRAM:32
AIM: Program to print all prime numbers between 0 to 100.
PROGRAM:
#include<iostream>
int main()
53
int i,j;
for(i=2;i<=100;i++)
{ int m=0;
for(j=2;j<i;j++)
if(i%j==0)
m=1;
if(m==0)
cout<<i<<" "<<endl;
return 0;
OUTPUT:
54
55
PROGRAM:
#include<iostream>
long fact(int);
int main(){
int n;
cin>>n;
cout<<endl;
return 0;}
if(n>1)
return n*fact(n-1);
else
return 1;}
OUTPUT:
PROGRAM: 34
56
PROGRAM:
#include<iostream>
int fibonacci(int);
int main(){
int n,i=0;
cin>>n;
cout<<"Fibonacci Series:\n";
while(i<n)
cout<<" "<<fibonacci(i);
i++;
cout<<endl;
return 0;
int fibonacci(int n)
if((n==1)||(n==0))
return(n);
57
else{
return(fibonacci(n-1)+fibonacci(n-2));
OUTPUT:
PROGRAM: 35
58
PROGRAM:
#include<iostream>
class person {
public:
string name;
int number;
};
int main() {
person obj;
cin>>obj.name;
cin>>obj.number;
return 0;}
OUTPUT:
PROGRAM: 36
59
PROGRAM:
#include<iostream>
class sum
private:
int a,b,total;
public:
void getdata ()
cin>>a;
cin>>b;
void display()
total=a+b;
};
int main ()
60
c.getdata ();
c.display ();
cout<<endl;
OUTPUT:
PROGRAM: 37
61
PROGRAM:
#include<iostream>
class sum
int a,b,total;
public:
void getdata();
void display();
};
void sum::getdata()
cin>>a;
cin>>b;
void sum::display ()
total=a+b;
int main()
62
sum c;
c.getdata ();
c.display ();
cout<<endl;
OUTPUT:
63
PROGRAM:
#include<iostream>
class item
public:
void counter()
{
count++;
cout<<"Nishtha>>"<<count<<endl;
};
int item::count;
int main()
item a,b,c;
a.counter();
b.counter();
c.counter();
64
return 0;
OUTPUT:
65
PROGRAM:
#include<iostream>
class Distance
public:
void setDist()
cin>>feet>>inches;
cout<<"Feet:"<<feet<<"-"<<"Inches:"<<inches<<"\n";
};
int main()
66
int a,b;
cin>>a;
cin>>b;
football.showDist();
cout<<endl;
OUTPUT:
67
PROGRAM:
#include <iostream>
class DemoDC
private:
public:
DemoDC()
num1 = 10;
num2 = 20;
void display() {
};
int main()
68
DemoDC obj;
obj.display();
cout<<endl;
return 0;
OUTPUT:
69
PROGRAM:
#include <iostream>
class ABC
int a, b;
public:
ABC(int i, int j)
a=i;
b=j;
void show()
};
int main()
70
obj.show();
cout<<endl;
return 0;
OUTPUT:
71
PROGRAM:
#include <iostream>
class ABC
int * p;
public:
ABC()
p=new int;
*p=10;
ABC(int v)
p=new int;
*p=v;
int dis()
{ return(*p);
72
int main()
ABC s, s1(9);
cout<<s.dis();
cout<<endl;
return 0;
OUTPUT:
73
PROGRAM:
#include<iostream>
class Complex
private:
double real;
double imag;
public:
};
int main()
if (com1 == 3.0)
else
74
cout<<endl;
return 0;
OUTPUT:
75
PROGRAM:
#include<iostream>
class construct
public:
float area;
construct()
area=0;
construct(int a,int b)
area=a*b;
void disp()
76
cout<<area<<endl;
};
int main()
construct o;
construct o2(10,20);
o.disp();
o2.disp();
cout<<endl;
return 1;
OUTPUT:
77
PROGRAM:
#include<iostream>
class B
int x;
public:
B(int data):x(data)
{};
void print()
};
class A
int a;
B b;
78
A():a(100),b(a)
cout<<"Value of a:"<<a<<endl;
b.print();
};
int main()
A a;
cout<<endl;
cout<<"Nishtha Singh";
return 0;
OUTPUT:
79
PROGRAM:
#include<iostream>
class Increment
{ int count;
public:
Increment( )
{ count=0; }
Increment(int C)
{ count=C; }
Increment operator++( )
{ count++;
return Increment(count);}
void display(void)
};
80
{ Increment l1,l2(5),l3,l4 ;
l1.display( );
++l1 ;++l2;
l1.display( );
l3= ++l1 ;
cout<<endl;
OUTPUT:
81
PROGRAM:
#include<iostream>
class Check
{ int i;
public:
Check(): i(3) { }
Check operator -- ()
82
temp.i = --i;
return temp; }
{ Check temp;
temp.i = i--;
return temp; }
void Display()
};
int main()
obj.Display();
obj1.Display();
obj1 = --obj;
obj.Display();
obj1.Display();
obj1 = obj--;
obj.Display();
obj1.Display();
cout<<endl;
return 0;
83
OUTPUT:
PROGRAM: 48
AIM: Program to demonstrate overloading of binary arithmetic operators.
PROGRAM:
#include<iostream>
public: FLOAT(){}
84
FLOAT FLOAT::operator+(FLOAT a)
return temp; }
FLOAT FLOAT::operator*(FLOAT b)
return temp;}
FLOAT FLOAT::operator-(FLOAT b)
return temp;}
FLOAT FLOAT::operator/(FLOAT b)
return temp;}
int main()
FLOAT a,b,c;
cout<<"\nAddition:"; c.putdata();
cout<<"\nMultiplication:"; c=a*b;
c.putdata();
cout<<endl;
85
OUTPUT:
PROGRAM: 49
AIM: Program to demonstrate overloading of memory management operators.
PROGRAM:
#include<iostream>
int main()
int num;
86
float* ptr;
cout << "Student" << i + 1 << " :" << *(ptr + i) << endl;
delete [] ptr;
cout<<endl;
return 0;
OUTPUT:
87
PROGRAM:
#include<iostream>
88
int hrs,min;
public:
Time(int);
void display();
};
Time :: Time(int t)
hrs=t/60;
min=t%60;
void Time::display()
int main()
int duration;
cin>>duration;
Time t1=duration;
89
cout<<endl;
OUTPUT:
PROGRAM: 51
AIM: Program to demonstrate the typecasting of class type to basic type.
PROGRAM:
#include<iostream>
90
{ int hrs,min;
public:
Time(int ,int);
operator int();
~Time()
{ cout<<"\nDestructor called..."<<endl;} };
Time::Time(int a,int b)
hrs=a;
min=b;}
return(hrs*60+min);}
int main()
{ int h,m,duration;
cin>>h;
cout<<endl;
91
OUTPUT:
PROGRAM: 52
AIM: Program to demonstrate the typecasting of class type to class.
PROGRAM:
#include<iostream>
92
public:
inventory1()
int getino()
{ return(ino); }
float getamt()
{ return(qty*rate); }
void display()
};
class inventory2
public:
void operator=(inventory1 I)
{ ino=I.getino(); amount=I.getamt();
void display()
93
};
int main()
I2=I1;
I1.display(); I2.display();
cout<<endl;
OUTPUT:
PROGRAM: 53
AIM: Program to demonstrate the single inheritence.
PROGRAM:
#include <iostream>
94
public:
int c=10;
public :
int result;
result=a+c;
int result;
result=a*c;
};
int d=20;
public:
void sum_2()
95
result=c+d;
void mul_2()
int result;
result=c*d;
};
int main()
int a,b;
cin>>a;
cin>>b;
Sum_and_mul sam;
Mul_and_sum mas;
sam.sum_1(a,b);
sam.mul_1(a,b);
mas.sum_1(a,b);
mas.mul_1(a,b);
96
mas.mul_2();
cout<<endl;
OUTPUT:
PROGRAM: 54
AIM: Program to demonstrate the multilevel inheritance.
PROGRAM:
97
class A
public:
void display()
};
class B : public A
};
class C : public B
};
int main()
C obj;
obj.display();
cout<<endl;
return 0;
98
PROGRAM: 55
AIM: Program to demonstrate the multiple inheritence.
PROGRAM:
99
class Mammal {
public:
Mammal()
};
class WingedAnimal {
public:
WingedAnimal()
};
};
int main()
Bat b1;
cout<<endl;
return 0;
100
OUTPUT:
PROGRAM: 56
AIM: Program to demonstrate the hierarchical inheritance.
101
#include<iostream>
class Number
{ int num;
{ public:
int getSquare(void)
{ int num,sqr;
num=returnNumber(); sqr=num*num;
return sqr; } };
{ public:
int getCube(void)
{ int num,cube;
num=returnNumber();
cube=num*num*num;
return cube; } };
int main()
102
objC.getNumber(); cube=objC.getCube();
return 0;
OUTPUT:
PROGRAM: 57
103
PROGRAM:
#include<iostream>
int a,b,c,d,e;
class A
{ protected:
cin>>a>>b;} };
{ cout<<"Enter c value:";
cin>>c; } };
class C { protected:
{ cout<<"Enter d value:";
cin>>d; } };
protected:
{ getab(); getc();
getd(); e=a+b+c+d;
104
int main()
{ D d1;
d1.result();
cout<<endl;
return 0;
OUTPUT:
PROGRAM: 58
105
PROGRAM:
#include<iostream>
class ClassA
public:
int a;
};
public:
int b;
};
public:
int c;
};
public:
int d;
};
106
{ ClassD obj;
obj.b = 20;
obj.c = 30;
obj.d = 40;
cout<<endl;
OUTPUT:
PROGRAM: 59
107
PROGRAM:
#include<iostream>
class Files{
public:
cout<<"Download Files"<<endl;}};
public:
void download(){
cout<<"Download Pdf"<<endl;}};
int main() {
delete f;
cout<<endl;
return 0;}
OUTPUT:
PROGRAM: 60
108
PROGRAM:
#include<iostream>
int main(){
int x = -1;
try {
if (x < 0) {
throw x;
catch (int x ) {
cout<<endl;
return 0; }
OUTPUT:
PROGRAM: 61
109
PROGRAM:
#include<iostream>
#include<fstream>
template<class t>
t temp = *x;
*x = *y;
*y = temp;}
cout << "\na and b before swaping :" << a << "\t" << b;
swap(&a,&b);
cout << "\na and b after swaping :" << a << "\t" << b;
cout << "\n\nc and d before swaping :" << c << "\t" << d;
swap(c, d);
cout << "\nc and d after swaping :" << c << "\t" << d;}
int main() {
int a, b; float c, d;
fun(a, b, c, d);
110
OUTPUT:
PROGRAM: 62
111
PROGRAM:
#include<iostream>
#include<fstream>
int main()
{ int i1,i2;
float f1,f2;
char c1,c2;
cin>>i1>>i2;
cin>>f1>>f2;
cin>>c1>>c2;
cout<<endl;
return 0;
112
PROGRAM: 63
AIM: Program to demonstrate opening and closing a file.
PROGRAM:
#include<iostream>
#include<fstream>
113
int main()
{ ofstream fout;
ifstream fin;
char fname[20];
cin.get(fname, 20);
fout.open(fname, ios::out);
if(!fout)
exit(1);}
cin.get(ch);
cin.get(rec, 80);
fout<<rec<<"\n";
fout.close();
fin.open(fname, ios::in);
if(!fin)
exit(2);}
cin.get(ch);
114
cout<<rec;
fin.close();
cout<<endl;
OUTPUT:
PROGRAM:64
AIM: Program to copy the contents of a file to another file.
PROGRAM:
#include<iostream>
115
int main()
ifstream fs;
ofstream ft;
gets(fname1);
fs.open(fname1);
if(!fs)
gets(fname2); ft.open(fname2);
if(!ft)
fs.close();
exit(2);}
while(fs.eof()==0)
{ fs>>ch; ft<<ch;}
fs.close();
ft.close();
cout<<endl;
116
OUTPUT:
PROGRAM: 65
AIM: Program to demonstrate reading and writing of a file.
PROGRAM:
117
#include<fstream>
int main()
fstream file;
file.open("sample.txt",ios::out);
if(!file)
return 0;
file<<"Nishtha Singh";
file.close();
file.open("sample.txt",ios::in);
if(!file)
return 0;
char ch;
while(!file.eof())
118
file>>ch;
cout<<ch;
file.close();
cout<<endl;
return 0;
OUTPUT:
119