You are on page 1of 119

Chandigarh Engineering College

Department of computer science and engineering


BACHELOR OF TECHNOLOGY
(Computer Science and Engineering)

OOPS Lab File

(BTCS 302-18)

NAME: NISHTHA SINGH

ROLLNO: 1902168

SEMESTER: 3rd

GROUP: 3V1

UNDER THE GUIDANCE OF

MS NAVDEEP KAUR

(ASSISTANT PROFESSOR)

NISHTHA SINGH 1902168


TABLE OF CONTENT

S.NO LIST OF PROGRAMS PAGE NO.

1 Program to implement basic calculations (+,-,*,/ ) 4-7

2 Program to implement if-else statement. 8

3 Program to implement nested-if statement 9

4 Program to calculate area of triangle, rectangle, square and circle using switch statement. 10-12

5 Program to print table of a number. 13

6 Program to print 4 different patterns of stars and numbers. 14-15

7 Program to find reverse of a number. 16

8 Program to swap two numbers. 17

9 Program to print Fibonacci series. 18

10 Program to calculate factorial of a number. 19

11 Program to check whether the number is palindrome or not. 20-21

12 Program to check whether the number is Armstrong or not. 22-23

13 Program to find ASCII value of a number. 24

14 Program to calculate average of 10 numbers. 25

15 Program to find greatest/largest of 3 numbers. 26

16 Program to check whether the number is prime or not. 27

17 Program to print days of a week. 28-29

18 Program to check whether a year is a Leap Year or not. 30-31

19 Program to calculate sum of numbers of an array. 32-33

20 Program to sort an array in ascending order. 34-35

21 Program to find largest and smallest number of an array. 36-38

22 Program to print reverse of an array. 39-40

23 Program to print the element which occurs more than one time in an array. 41-42

24 Program to print odd/even elements of an array. 43-44

25 Program to print mid element of an array. 45

26 Program to add two matrices. 46-47

27 Program to find length of a string. 48

28 Program to find reverse of a string. 49

29 Program to compare two strings. 50

30 Program to concatenate two strings. 51

31 Program to check whether the character is a vowel or a consonant. 52

32 Program to print all prime numbers between 0 to 100. 53-54

33 Program to calculate factorial of a number using recursion. 55

34 Program to print Fibonacci series using recursion. 56-57

35 Program to implement a class. 58

NISHTHA SINGH 1902168


36 Program that uses a class in which member functions are defined inside the class. 59-60

37 Program that uses a class in which member functions are defined outside the class. 61-62

38 Program to demonstrate the use of static data members. 63-64

39 Program to demonstrate the use of const data members. 65-66

40 Program to demonstrate the use of default constructor. 67-68

41 Program to demonstrate the use of parameterized constructor. 69-70

42 Program to demonstrate the use of dynamic constructor. 71-72

43 Program to demonstrate the use of explicit constructor. 73-74

44 Program to demonstrate constructor overloading. 75-76

45 Program to demonstrate the use of initializer list. 77-78

46 Program to demonstrate overloading of increment operator. 79-80

47 Program to demonstrate overloading of decrement operator. 81-82

48 Program to demonstrate overloading of binary arithmetic operators. 83-84

49 Program to demonstrate overloading of memory management operators. 85-86

50 Program to demonstrate the typecasting of basic type to class type. 87-88

51 Program to demonstrate the typecasting of class type to basic type. 89-90

52 Program to demonstrate the typecasting of class type to class type. 91-92

53 Program to demonstrate the single inheritance. 93-95

54 Program to demonstrate the multilevel inheritance. 96-97

55 Program to demonstrate the multiple inheritances. 98-99

56 Program to demonstrate the hierarchical inheritance. 100-101

57 Program to demonstrate the hybrid inheritance. 102-103

58 Program to demonstrate the virtual derivation of a class. 104-105

59 Program to demonstrate the run time polymorphism. 106

60 Program to demonstrate the exception handling. 107

61 Program to demonstrate the use of function template. 108-109

62 Program to demonstrate the use of class template. 110-111

63 Program to demonstrate opening and closing a file. 112-113

64 Program to copy the contents of a file to another file. 114-115

65 Program to demonstrate reading and writing of a file. 116-117

NISHTHA SINGH 1902168


PROGRAM: 1
AIM: Program to implement basic calculations (+,-,*,/ ).

PROGRAM:

#include<iostream>

using namespace std;

int main()

int choice;

long num1, num2, x;

cout << "Please choose your option:"

"\n1 = Addition"

"\n2 = Subtraction"

"\n3 = Multiplication"

"\n4 = Division"

"\n5 = Squares"

"\n6 = exit"

"\n\nChoice: ";

cin >> choice;

while(choice < 1 || choice > 6)

cout << "\nPlease choose the above mentioned option."

"\nChoice: "<<endl;

cin >> choice;

NISHTHA SINGH 1902168


switch (choice)

case 1:

cout << "Enter two numbers: \n";

cin >> num1 >> num2;

x = num1 + num2;

cout << "Sum = " << x;

break;

case 2:

cout << "Enter two numbers: \n";

cin >> num1 >> num2;

x = num1 - num2;

cout << "Subtraction = " << x<<endl;

break;

case 3:

cout << "Enter two numbers: \n";

cin >> num1 >> num2;

x = num1 * num2;

cout << "Product = " << x;

break;

NISHTHA SINGH 1902168


case 4:

cout << "Enter Dividend: ";

cin >> num1;

cout << "Enter Divisor: ";

cin >> num2;

while(num2 == 0)

cout << "\nDivisor cannot be zero."

"\nEnter divisor once again: ";

cin >> num2;

x = num1 / num2;

cout << "\nQuotient = " << x;

break;

case 5:

cout << "Enter any number: \n";

cin >> num1;

x = num1 * num1;

cout << "Square = " << x;

break;

case 6: return 0;

NISHTHA SINGH 1902168


default: cout << "\nError";

cout<<"Nishtha Singh 1902168";

OUTPUT:

NISHTHA SINGH 1902168


PROGRAM: 2
AIM: Program to implement if-else statement.

PROGRAM:

#include <iostream>

using namespace std;

int main(){

int num=66;

if( num < 50 ){

cout<<"num is less than 50";

else {

cout<<"num is greater than or equal 50"<<endl;

cout<<"Nishtha Singh 1902168"<<endl;

return 0;

OUTPUT:

NISHTHA SINGH 1902168


PROGRAM: 3
AIM: Program to implement nested-if statement.

PROGRAM:

#include <iostream>

using namespace std;

int main(){

int num=90;

if( num < 100 ){

cout<<"number is less than 100"<<endl;

if(num > 50){

cout<<"number is greater than 50"<<endl;

cout<<"Nishtha Singh 190168"<<endl;

return 0;

OUTPUT:

NISHTHA SINGH 1902168


PROGRAM: 4
AIM: Program to calculate area of triangle, rectangle, square and circle using switch
statement.

PROGRAM:

#include<iostream>

#include<math.h>

using namespace std;

int main()

float a, b, c, s, radius, area;

int ch;

cout<<"1.Area Of Circle";

cout<<"\n2.Area Of Rectangle";

cout<<"\n3.Area Of Triangle \n";

cout<<"\nEnter Your Choice :";

cin>>ch;

switch(ch)

case 1:

cout<<"\nEnter the Radius of Circle: ";

cin>>radius;

area=3.14159*radius*radius;

10

NISHTHA SINGH 1902168


cout<<"Area of Circle = "<<area<<endl;

break;

case 2:

cout<<"\nEnter the Length and Breadth of Rectangle:";

cin>>a>>b;

area=a*b;

cout<<"Area of Rectangle = "<<area<<endl;

break;

case 3:

cout<<"\nEnter All Three Sides of Triangle with 3 Sides:";

cin>>a>>b>>c;

s=(a+b+c)/2;

area=sqrt(s*(s-a)*(s-b)*(s-c));

cout<<"Area of Triangle = "<<area<<endl;

break;

default: cout<<"\n Invalid Choice Try Again...!!!";

break;

cout<<"Nishtha Singh 1902168";

11

NISHTHA SINGH 1902168


return 0;

OUTPUT:

12

NISHTHA SINGH 1902168


PROGRAM: 5
AIM: Program to print table of a number.

PROGRAM:

#include<iostream>

using namespace std;

int main(){

int i,n;

cout<<"Enter any number:";

cin>>n;

for(i=1;i<=10;++i)

cout<<"\n"<<n<<" * "<<i<<" = "<<n*i;

cout<<endl;

cout<<"Nishtha Singh 190168";

return 0;

OUTPUT:

13

NISHTHA SINGH 1902168


PROGRAM: 6
AIM: Program to print 4 different patterns of stars and numbers.

PROGRAM:

#include<iostream>

using namespace std;

int main()

int i,j,n,s;

cout<<"Enter the value of n : ";

cin>>n;

s=n;

cout<<"The star - number square pattern :\n";

for(i=1;i<=n;i++)

s=s-i;

for(j=i;j>=1;j--)

while(s>0)

cout<<"*";

s--;

cout<<j;

14

NISHTHA SINGH 1902168


}

cout<<endl;

s=n;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

15

NISHTHA SINGH 1902168


PROGRAM: 7
AIM: Program to find reverse of a number.

PROGRAM:

#include <iostream>

using namespace std;

int main() {

int n, reversedNumber = 0, remainder;

cout << "Enter an integer: ";

cin >> n;

while(n != 0) {

remainder = n%10;

reversedNumber = reversedNumber*10 + remainder;

n /= 10;}

cout << "Reversed Number = " << reversedNumber<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

16

NISHTHA SINGH 1902168


PROGRAM: 8
AIM: Program to swap two numbers.

PROGRAM:

#include <iostream>

using namespace std;

int main() {

int a = 10, b = 5, temp;

temp = a;

a = b;

b = temp;

cout<<"Value of a is "<<a<<endl;

cout<<"Value of b is "<<b<<endl;

Cout<<”Nishtha Singh 190168”<<endl;

return 0;

OUTPUT:

17

NISHTHA SINGH 1902168


PROGRAM: 9
AIM: Program to print fibonacci series.

PROGRAM:

#include <iostream>

using namespace std;

int main() {

int n1=0,n2=1,n3,i,number;

cout<<"Enter the number of elements: ";

cin>>number;

cout<<n1<<" "<<n2<<" ";

for(i=2;i<number;++i) {

n3=n1+n2;

cout<<n3<<" ";

n1=n2;

n2=n3; }

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0; }

OUTPUT:

18

NISHTHA SINGH 1902168


PROGRAM: 10
AIM: Program to calculate factorial of a number.

PROGRAM:

#include <iostream>

using namespace std;

unsigned int factorial(unsigned int n) {

int res = 1, i;

for (i = 2; i <= n; i++)

res *= i;

return res;

int main() {

int num = 5;

cout << "Factorial of "

<< num << " is "

<< factorial(num) << endl;

Cout<<”Nishtha Singh 190168”;

return 0; }

OUTPUT:

19

NISHTHA SINGH 1902168


PROGRAM: 11
AIM: Program to check whether given number is palindrome or not.

PROGRAM:

#include <iostream>

using namespace std;

int main()

int n, num, digit, rev = 0;

cout << "Enter a positive number: ";

cin >> num;

n = num;

do

digit = num % 10;

rev = (rev * 10) + digit;

num = num / 10;

while (num != 0);

cout << " The reverse of the number is: " << rev << endl;

if (n == rev)

20

NISHTHA SINGH 1902168


cout << " The number is a palindrome.";

else

cout << " The number is not a palindrome.";

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

21

NISHTHA SINGH 1902168


PROGRAM: 12
AIM: Program to check whether given number is armstrong or not.

PROGRAM:

#include <iostream>

using namespace std;

int main()

int num, originalNum, remainder, result = 0;

cout << "Enter a three-digit integer: ";

cin >> num;

originalNum = num;

while (originalNum != 0)

remainder = originalNum % 10;

result += remainder * remainder * remainder;

originalNum /= 10;

if (result == num)

cout << num << " is an Armstrong number.";

else

22

NISHTHA SINGH 1902168


cout << num << " is not an Armstrong number.";

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

23

NISHTHA SINGH 1902168


PROGRAM: 13
AIM: Program to find ASCII value of a number.

PROGRAM:

#include <iostream>

using namespace std;

int main(){

char ascii;

int numeric;

cout << "Give character: ";

cin >> ascii;

cout << "Its ascii value is: " << (int) ascii << endl;

cout << "Give a number to convert to ascii: ";

cin >> numeric;

cout << "The ascii value of " << numeric << " is " << (char) numeric;

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

24

NISHTHA SINGH 1902168


PROGRAM: 14
AIM: Program to calculate average of two numbers.

PROGRAM:

#include <iostream>

using namespace std;

int main(){

int x,y,sum;

float average;

cout << "Enter 2 integers : " << endl;

cin>>x>>y;

sum=x+y;

average=sum/2;

cout << "The average of " << x << " and " << y << " is " << average << "." << endl;

cout<<"Nishtha Singh 1902168";

OUTPUT:

25

NISHTHA SINGH 1902168


PROGRAM: 15
AIM: Program to find largest/greatest of three numbers.

PROGRAM:

#include <iostream>

using namespace std;

int main() {

float n1, n2, n3;

cout << "Enter three numbers: ";

cin >> n1 >> n2 >> n3;

if(n1 >= n2 && n1 >= n3)

cout << "Largest number: " << n1;

if(n2 >= n1 && n2 >= n3)

cout << "Largest number: " << n2;

if(n3 >= n1 && n3 >= n2)

cout << "Largest number: " << n3;

return 0;

26

NISHTHA SINGH 1902168


}

OUTPUT:

PROGRAM: 16
AIM: Program to check whether number is prime or not.

PROGRAM:

#include <iostream>

using namespace std;

int main(){

int n, i, c = 0;

cout << "Enter any number n: "; cin>>n;

for (i = 1; i <= n; i++){

if (n % i == 0) {

c++;}

if (c == 2){

cout << "n is a Prime number" << endl; }

else {

cout << "n is not a Prime number" << endl; }

cout<<"Nishtha Singh 1902168";

return 0;

27

NISHTHA SINGH 1902168


}

OUTPUT:

PROGRAM: 17
AIM: Program to print days of a week.

PROGRAM:

#include<iostream>

using namespace std;

int main(){

int week;

cout<<"Enter week number (1-7):";

cin >> week;

if(week == 1) {

cout << "Monday";

else if(week == 2)

cout << "Tuesday";

else if(week == 3)

28

NISHTHA SINGH 1902168


cout << "Wednesday";

else if(week == 4)

cout << "Thursday";

else if(week == 5)

cout << "Friday";

else if(week == 6)

cout << "Saturday";

else if(week == 7)

cout << "Sunday";

else{

cout << "Invalid Input! Please enter week in between 1-7.";

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

29

NISHTHA SINGH 1902168


}

OUTPUT:

PROGRAM: 18
AIM: Program to check whether a year is a leap year or not.

PROGRAM:

#include <iostream>

using namespace std;

int main() {

int year;

cout << "Enter a year: ";

cin >> year;

if (year % 4 == 0) {

if (year % 100 == 0) {

if (year % 400 == 0)

cout << year << " is a leap year.";

else

cout << year << " is not a leap year.";

else

30

NISHTHA SINGH 1902168


cout << year << " is a leap year.";

else

cout << year << " is not a leap year.";

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

31

NISHTHA SINGH 1902168


PROGRAM: 19
AIM: Program to calculate sum of numbers of an array.

PROGRAM:

#include<iostream>

using namespace std;

int main()

int arr[20],i,n,sum=0;

cout<<"How many elements you want to enter: ";

cin>>n;

cout<<"Enter any "<<n<<" elements in Array: ";

for(i=0;i<n;i++)

cin>>arr[i];

cout<<"Sum of all Elements are: ";

32

NISHTHA SINGH 1902168


for(i=0;i<n;i++)

sum=sum+arr[i];

for(i=0;i<n;i++)

cout<<sum;

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

33

NISHTHA SINGH 1902168


PROGRAM: 20
AIM: Program to sort an array in ascending order.

PROGRAM:

#include<iostream>

using namespace std;

int main()

int i,a[10],temp,j;

cout<<"Enter any 5 num in array: \n";

for(i=0;i<=5;i++)

cin>>a[i];

cout<<"\nData before sorting: ";

for(j=0;j<5;j++)

cout<<a[j];

34

NISHTHA SINGH 1902168


}

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;

cout<<"\nData after sorting: ";

for(j=0;j<5;j++)

cout<<a[j];

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

35

NISHTHA SINGH 1902168


PROGRAM: 21
AIM: Program to find largest and smallest number of an array.

PROGRAM:

#include <iostream>

using namespace std;

int findMinimum(int a[], int n)

int mn = a[0];

for (int i = 0; i < n; i++)

mn = min(mn, a[i]);

return mn;

36

NISHTHA SINGH 1902168


int findMaximum(int a[], int n)

int mx = a[0];

for (int i = 0; i < n; i++)

mx = max(mx, a[i]);

return mx;

int main()

int n;

cout << " Enter the size of the array: ";

cin >> n;

int arr[n], i, pos;

cout << "\n\n Enter the " << n << " elements of the array: \n\n";

for (i = 0; i < n; i++)

cin >> arr[i];

37

NISHTHA SINGH 1902168


}

cout << "\n\n The " << n << " elements of the array are : ";

for (i = 0; i < n; i++)

cout << arr[i] << " ";

int mn = findMinimum(arr, n);

int mx = findMaximum(arr, n);

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";

Cout<<Nishtha Singh 1902168”;

return 0;

OUTPUT:

38

NISHTHA SINGH 1902168


PROGRAM: 22
AIM: Program to print reverse of an array.

PROGRAM:

#include<iostream>

using namespace std;

int main()

int arr[50], size, i, j, temp;

cout<<"Enter array size : ";

cin>>size;

cout<<"Enter array elements : ";

for(i=0; i<size; i++)

39

NISHTHA SINGH 1902168


{

cin>>arr[i];

j=i-1;

i=0;

while(i<j)

temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

i++;

j--;

cout<<"Now the Reverse of the Array is : \n";

for(i=0; i<size; i++)

cout<<arr[i]<<" ";

Cout<<”Nishtha Singh 1902168”;

return 0;

OUTPUT:

40

NISHTHA SINGH 1902168


PROGRAM: 23
AIM: Program to print the element which occurs more than one time in an array.

PROGRAM:

#include <bits/stdc++.h>

using namespace std;

void printRepeating(int arr[], int n)

unordered_map<int, int> mp;

for (int i = 0; i < n; i++)

41

NISHTHA SINGH 1902168


mp[arr[i]]++;

for (int i = 0; i < n; i++) {

if (mp[arr[i]] > 1) {

cout << arr[i] << " ";

mp[arr[i]] = 0;

int main()

int arr[] = { 12, 10, 9, 45, 2, 10, 10, 45 };

int n = sizeof(arr) / sizeof(arr[0]);

printRepeating(arr, n);

Cout<<”Nishtha Singh 190168”;

return 0;

OUTPUT:

42

NISHTHA SINGH 1902168


PROGRAM: 24
AIM: Program to print odd/even elements of an array.

PROGRAM:

#include<iostream>

using namespace std;

int main()

int arr[20],i,n,even=0,odd=0;

cout<<"How many elements you want to enter: ";

cin>>n;

cout<<"Enter any "<<n<<" elements in Array:\n";

43

NISHTHA SINGH 1902168


for(i=0; i<n ;i++)

{ cin>>arr[i]; }

cout<<"Even number:";

for(i=0; i<n ;i++)

if(arr[i]%2==0)

{ even++;

cout<<arr[i]<<" "; }

cout<<"\nOdd number:";

for(i=0; i<n ;i++)

if(arr[i]%2!=0)

{ odd++;

cout<<arr[i]<<" "; }

Cout<<”Nishtha Singh 1902168”;

return 0;

OUTPUT:

44

NISHTHA SINGH 1902168


PROGRAM: 25
AIM: Program to print mid element of an array.

PROGRAM:

#include<iostream>

using namespace std;

int main(){

int arr[20],i,n,even=0,odd=0;

45

NISHTHA SINGH 1902168


cout<<"How many elements you want to enter: ";

cin>>n;

cout<<"Enter any "<<n<<" elements in Array:\n";

for(i=0; i<n ;i++) {

cin>>arr[i]; }

if(n%2!=0) {

cout<<"Middle element is "<<arr[n/2]; }

else {

cout<<"Middle element is ";

cout<<arr[n/2-1]<<" and "<<arr[n/2]<<endl;

cout<<"Nishtha Singh 1902168"; }

OUTPUT:

PROGRAM: 26
AIM: Program to add two matrices.

PROGRAM:

#include<iostream>

using namespace std;

int main()

46

NISHTHA SINGH 1902168


int m,n,c,d,first[10][10],second[10][10],sum[10][10];

cout<<"Enter the number of rows and columns of matrix ";

cin>>m>>n;

cout<<"Enter the elements of first matrix\n";

for(c=0;c<m;c++)

for(d=0;d<n;d++)

cin>>first[c][d];

cout<<"Enter the elements of second matrix\n";

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];

cout<<"Sum of entered matrices:-\n";

for(c=0;c<m;c++)

{ for(d=0;d<n;d++)

cout<<sum[c][d]<<"\t";

cout << endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

47

NISHTHA SINGH 1902168


PROGRAM: 27
AIM: Program to find length of a string.

PROGRAM:

#include<iostream>

using namespace std;

int main()

48

NISHTHA SINGH 1902168


{

string str;

cin>>str;

cout<<"Entered String="<<str;

int n=str.size();

cout<<"\nString Length="<<n<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

PROGRAM: 28
AIM: Program to find reverse of a string.

PROGRAM:

#include<iostream>

#include<string>

#include<bits/stdc++.h>

49

NISHTHA SINGH 1902168


using namespace std;

int main()

string str;

cout<<"Enter any string: ";

getline(cin,str);

reverse(str.begin(),str.end());

cout<<"Reverse of string: "<<str<<endl;

cout<<"Nishtha Singh 1902168";

OUTPUT:

PROGRAM:29
AIM: Program to compare two strings.

PROGRAM:

#include <iostream>

#include<string>

#include<bits/stdc++.h>

50

NISHTHA SINGH 1902168


using namespace std;

int main() {

string stra,strb;

cout<<"Enter first string: ";

getline(cin,stra);

cout<<"Enter second string: ";

getline(cin,strb);

if(stra.compare(strb)==0)

cout<<"Strings are same";

else

cout<<"Strings are not same"<<endl;

cout<<"Nishtha Singh 1902168";}

OUTPUT:

PROGRAM: 30
AIM: Program to concatenate two strings.

PROGRAM:

#include <iostream>

#include<string>

#include<bits/stdc++.h>

51

NISHTHA SINGH 1902168


using namespace std;

int main()

string stra,strb,strc;

cout<<"Enter first string: ";

getline(cin,stra);

cout<<"Enter second string: ";

getline(cin,strb);

strc=stra+strb;

cout<<"Concatinated String: "<<strc<<endl;

cout<<"Nishtha Singh 1902168";

OUTPUT:

PROGRAM: 31
AIM: Program to check whether the character is a vowel or a consonant.

PROGRAM:

#include<iostream>

using namespace std;

int main(){

52

NISHTHA SINGH 1902168


char ch;

int lower_case,upper_case;

cout<<"Enter any Character:";

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;

cout<<"Nishtha Singh 1902168";

return 0; }

OUTPUT:

PROGRAM:32
AIM: Program to print all prime numbers between 0 to 100.

PROGRAM:

#include<iostream>

using namespace std;

int main()

53

NISHTHA SINGH 1902168


{

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;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

54

NISHTHA SINGH 1902168


PROGRAM: 33

55

NISHTHA SINGH 1902168


AIM: Program to calculate factorial of a number using recursion.

PROGRAM:

#include<iostream>

using namespace std;

long fact(int);

int main(){

int n;

cout<<"Enter a positive integer:";

cin>>n;

cout<<"Factorial of "<<n<<" = "<<fact(n);

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;}

long fact(int n){

if(n>1)

return n*fact(n-1);

else

return 1;}

OUTPUT:

PROGRAM: 34

56

NISHTHA SINGH 1902168


AIM: Program to print fibonacci series using recursion.

PROGRAM:

#include<iostream>

using namespace std;

int fibonacci(int);

int main(){

int n,i=0;

cout<<"Number of terms for Fibonacci Series:";

cin>>n;

cout<<"Fibonacci Series:\n";

while(i<n)

cout<<" "<<fibonacci(i);

i++;

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

int fibonacci(int n)

if((n==1)||(n==0))

return(n);

57

NISHTHA SINGH 1902168


}

else{

return(fibonacci(n-1)+fibonacci(n-2));

OUTPUT:

PROGRAM: 35

58

NISHTHA SINGH 1902168


AIM: Program to implement a class.

PROGRAM:

#include<iostream>

using namespace std;

class person {

public:

string name;

int number;

};

int main() {

person obj;

cout<<"Enter the Name:";

cin>>obj.name;

cout<<"Enter the Number:";

cin>>obj.number;

cout<<"Name entered="<<obj.name<<"\nNumber entered="<<obj.number<<endl;

cout<<"Nishtha Singh 1902168";

return 0;}

OUTPUT:

PROGRAM: 36

59

NISHTHA SINGH 1902168


AIM: Program that uses a class in which member functions are defined inside the class.

PROGRAM:

#include<iostream>

using namespace std;

class sum

private:

int a,b,total;

public:

void getdata ()

cout<<"Enter the value of A:";

cin>>a;

cout<<"Enter the value of B:";

cin>>b;

void display()

total=a+b;

cout<<"Sum of A and B="<<total;

};

int main ()

60

NISHTHA SINGH 1902168


sum c;

c.getdata ();

c.display ();

cout<<endl;

cout<<"Nishtha Singh 1902168";

OUTPUT:

PROGRAM: 37

61

NISHTHA SINGH 1902168


AIM: Program that uses a class in which member functions are defined outside the class.

PROGRAM:

#include<iostream>

using namespace std;

class sum

int a,b,total;

public:

void getdata();

void display();

};

void sum::getdata()

cout<<"Enter the value of A:";

cin>>a;

cout<<"Enter the value of B:";

cin>>b;

void sum::display ()

total=a+b;

cout<<"Sum of A and B="<<total;

int main()

62

NISHTHA SINGH 1902168


{

sum c;

c.getdata ();

c.display ();

cout<<endl;

cout<<"Nishtha Singh 1902168";

OUTPUT:

63

NISHTHA SINGH 1902168


PROGRAM: 38
AIM: Program to demonstrate the use of static data members.

PROGRAM:

#include<iostream>

using namespace std;

class item

static int count;

public:

void counter()
{

count++;

cout<<"Nishtha>>"<<count<<endl;

};

int item::count;

int main()

item a,b,c;

a.counter();

b.counter();

c.counter();

64

NISHTHA SINGH 1902168


cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

65

NISHTHA SINGH 1902168


PROGRAM: 39

AIM: Program to demonstrate the use of const data members.

PROGRAM:

#include<iostream>

using namespace std;

class Distance

int feet; float inches;

public:

Distance(int ft, float in): feet(ft), inches(in)

void setDist()

cin>>feet>>inches;

void showDist() const

cout<<"Feet:"<<feet<<"-"<<"Inches:"<<inches<<"\n";

};

int main()

66

NISHTHA SINGH 1902168


{

int a,b;

cout<<"Please Enter inches\n";

cin>>a;

cout<<"Please Enter feet\n";

cin>>b;

const Distance football(b, a);

football.showDist();

cout<<endl;

cout<<"Nishtha Singh 1902168";

OUTPUT:

67

NISHTHA SINGH 1902168


PROGRAM: 40
AIM: Program to demonstrate the use of default constructor.

PROGRAM:

#include <iostream>

using namespace std;

class DemoDC

private:

int num1, num2 ;

public:

DemoDC()

num1 = 10;

num2 = 20;

void display() {

cout<<"num1 = "<< num1 <<endl;

cout<<"num2 = "<< num2 <<endl;

};

int main()

68

NISHTHA SINGH 1902168


{

DemoDC obj;

obj.display();

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

69

NISHTHA SINGH 1902168


PROGRAM: 41
AIM: Program to demonstrate the use of parameterized constructor.

PROGRAM:

#include <iostream>

using namespace std;

class ABC

int a, b;

public:

ABC(int i, int j)

a=i;

b=j;

void show()

cout<< a << " " << b;

};

int main()

70

NISHTHA SINGH 1902168


ABC obj(27, 7);

obj.show();

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

71

NISHTHA SINGH 1902168


PROGRAM: 42
AIM: Program to demonstrate the use of dynamic constructor.

PROGRAM:

#include <iostream>

using namespace std;

class ABC

int * p;

public:

ABC()

p=new int;

*p=10;

ABC(int v)

p=new int;

*p=v;

int dis()

{ return(*p);

72

NISHTHA SINGH 1902168


};

int main()

ABC s, s1(9);

cout<<"The value of object s's p is:";

cout<<s.dis();

cout<<"\nThe value of object s1's p is:"<<s1.dis();

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

73

NISHTHA SINGH 1902168


PROGRAM: 43
AIM: Program to demonstrate the use of explicit constructor.

PROGRAM:

#include<iostream>

using namespace std;

class Complex

private:

double real;

double imag;

public:

Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

bool operator == (Complex rhs) {

return (real == rhs.real && imag == rhs.imag)? true : false;

};

int main()

Complex com1(3.0, 0.0);

if (com1 == 3.0)

cout << "Same";

else

74

NISHTHA SINGH 1902168


cout << "Not Same";

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

75

NISHTHA SINGH 1902168


PROGRAM: 44
AIM: Program to demonstrate constructor overloading.

PROGRAM:

#include<iostream>

using namespace std;

class construct

public:

float area;

construct()

area=0;

construct(int a,int b)

area=a*b;

void disp()

76

NISHTHA SINGH 1902168


{

cout<<area<<endl;

};

int main()

construct o;

construct o2(10,20);

o.disp();

o2.disp();

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 1;

OUTPUT:

77

NISHTHA SINGH 1902168


PROGRAM: 45
AIM: Program to demonstrate the use of initializer list.

PROGRAM:

#include<iostream>

using namespace std;

class B

int x;

public:

B(int data):x(data)

{};

void print()

{cout<<"Value of x is: "<<x<<endl;}

};

class A

int a;

B b;

78

NISHTHA SINGH 1902168


public:

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

NISHTHA SINGH 1902168


PROGRAM: 46
AIM: Program to demonstrate overloading of increment operator.

PROGRAM:

#include<iostream>

using namespace std;

class Increment

{ int count;

public:

Increment( )

{ count=0; }

Increment(int C)

{ count=C; }

Increment operator++( )

{ count++;

return Increment(count);}

void display(void)

{ cout << count << endl ; }

};

80

NISHTHA SINGH 1902168


int main(void)

{ Increment l1,l2(5),l3,l4 ;

cout <<"\n Before Activating the Operator ++( )\nl1=" ;

l1.display( );

cout << "l2= "; l2.display( );

++l1 ;++l2;

cout << "\n After Activating the Operator ++(.)\nl1=" ;

l1.display( );

cout << "l2= " ; l2.display( ) ;

l3= ++l1 ;

cout << "l3= "; l3.display( ) ;

cout << "l4= "; l4.display( ) ;

cout<<endl;

cout<<"Nishtha Singh 1902168";

OUTPUT:

81

NISHTHA SINGH 1902168


PROGRAM: 47
AIM: Program to demonstrate overloading of decrement operator.

PROGRAM:

#include<iostream>

using namespace std;

class Check

{ int i;

public:

Check(): i(3) { }

Check operator -- ()

82

NISHTHA SINGH 1902168


{ Check temp;

temp.i = --i;

return temp; }

Check operator -- (int)

{ Check temp;

temp.i = i--;

return temp; }

void Display()

{ cout << "i = "<< i <<endl; }

};

int main()

Check obj, obj1;

obj.Display();

obj1.Display();

obj1 = --obj;

obj.Display();

obj1.Display();

obj1 = obj--;

obj.Display();

obj1.Display();

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

83

NISHTHA SINGH 1902168


}

OUTPUT:

PROGRAM: 48
AIM: Program to demonstrate overloading of binary arithmetic operators.

PROGRAM:

#include<iostream>

using namespace std;

class FLOAT { float no;

public: FLOAT(){}

void getdata() { cout<<"Enter any float number:"; cin>>no;}

void putdata() {cout<<"\nAnswer is:"<<no;}

84

NISHTHA SINGH 1902168


FLOAT operator+(FLOAT); FLOAT operator*(FLOAT);

FLOAT operator-(FLOAT); FLOAT operator/(FLOAT); };

FLOAT FLOAT::operator+(FLOAT a)

{ FLOAT temp; temp.no=no+a.no;

return temp; }

FLOAT FLOAT::operator*(FLOAT b)

{ FLOAT temp; temp.no=no*b.no;

return temp;}

FLOAT FLOAT::operator-(FLOAT b)

{ FLOAT temp; temp.no=no-b.no;

return temp;}

FLOAT FLOAT::operator/(FLOAT b)

{ FLOAT temp; temp.no=no/b.no;

return temp;}

int main()

FLOAT a,b,c;

a.getdata(); b.getdata(); c=a+b;

cout<<"\nAddition:"; c.putdata();

cout<<"\nMultiplication:"; c=a*b;

c.putdata(); cout<<"\nSubstraction:"; c=a-b;

c.putdata(); cout<<"\nDivision:"; c=a/b;

c.putdata();

cout<<endl;

85

NISHTHA SINGH 1902168


cout<<”Nishtha Singh”;

OUTPUT:

PROGRAM: 49
AIM: Program to demonstrate overloading of memory management operators.

PROGRAM:

#include<iostream>

using namespace std;

int main()

int num;

cout << "Enter total number of students: ";

86

NISHTHA SINGH 1902168


cin >> num;

float* ptr;

ptr = new float[num];

cout << "Enter GPA of students." << endl;

for (int i = 0; i < num; ++i)

cout << "Student" << i + 1 << ": ";

cin >> *(ptr + i);

cout << "\nDisplaying GPA of students." << endl;

for (int i = 0; i < num; ++i) {

cout << "Student" << i + 1 << " :" << *(ptr + i) << endl;

delete [] ptr;

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

87

NISHTHA SINGH 1902168


PROGRAM: 50
AIM: Program to demonstrate the typecasting of basic type to class type.

PROGRAM:

#include<iostream>

using namespace std;

88

NISHTHA SINGH 1902168


class Time

int hrs,min;

public:

Time(int);

void display();

};

Time :: Time(int t)

cout<<"Basic Type to ==> Class Type Conversion..."<<endl;

hrs=t/60;

min=t%60;

void Time::display()

cout<<hrs<< ": Hours(s)" <<endl;

cout<<min<< " Minutes" <<endl;

int main()

int duration;

cout<<"Enter time duration in minutes";

cin>>duration;

Time t1=duration;

89

NISHTHA SINGH 1902168


t1.display();

cout<<endl;

cout<<"Nishtha Singh 1902168";

OUTPUT:

PROGRAM: 51
AIM: Program to demonstrate the typecasting of class type to basic type.

PROGRAM:

#include<iostream>

using namespace std;

90

NISHTHA SINGH 1902168


class Time

{ int hrs,min;

public:

Time(int ,int);

operator int();

~Time()

{ cout<<"\nDestructor called..."<<endl;} };

Time::Time(int a,int b)

{ cout<<"Constructor called with two parameters..."<<endl;

hrs=a;

min=b;}

Time :: operator int()

{ cout<<"Class Type to Basic Type Conversion..."<<endl;

return(hrs*60+min);}

int main()

{ int h,m,duration;

cout<<"Enter Hours ";

cin>>h;

cout<<"Enter Minutes "; cin>>m;

Time t(h,m); duration = t;

cout<<"Total Minutes are "<<duration<<"\n2nd method operator overloading


"<<endl;

duration = t.operator int();

cout<<"Total Minutes are "<<duration;

cout<<endl;

91

NISHTHA SINGH 1902168


cout<<"Nishtha Singh 1902168";

OUTPUT:

PROGRAM: 52
AIM: Program to demonstrate the typecasting of class type to class.

PROGRAM:

#include<iostream>

using namespace std;

92

NISHTHA SINGH 1902168


class inventory1

{ int ino,qty; float rate;

public:

inventory1(int n,int q,float r)

{ ino=n; qty=q; rate=r; }

inventory1()

{ cout<<"\n Inventory1's Object Created";}

int getino()

{ return(ino); }

float getamt()

{ return(qty*rate); }

void display()

{ cout<<endl<<"ino = "<<ino<<" qty = "<<qty<<" rate = "<<rate; }

};

class inventory2

{ int ino; float amount;

public:

void operator=(inventory1 I)

{ ino=I.getino(); amount=I.getamt();

void display()

{ cout<<endl<<"ino = "<<ino<<" amount = "<<amount;

93

NISHTHA SINGH 1902168


}

};

int main()

{ inventory1 I1(1001,30,75); inventory2 I2;

I2=I1;

I1.display(); I2.display();

cout<<endl;

cout<<"Nishtha Singh 1902168";

OUTPUT:

PROGRAM: 53
AIM: Program to demonstrate the single inheritence.

PROGRAM:
#include <iostream>

using namespace std;

94

NISHTHA SINGH 1902168


class Sum_and_mul

public:

int c=10;

public :

void sum_1(int a, int b)

int result;

result=a+c;

cout<<" The result for sum of a and c is: "<<result<<endl;

void mul_1(int a,int b)

int result;

result=a*c;

cout<<" The result for multiplication of a and c is: "<<result<<endl;

};

class Mul_and_sum : public Sum_and_mul

int d=20;

public:

void sum_2()

95

NISHTHA SINGH 1902168


int result;

result=c+d;

cout<<" The result for sum of c and d is: "<<result<<endl;

void mul_2()

int result;

result=c*d;

cout<<" The result for multiplication of c and d is: "<<result<<endl;

};

int main()

int a,b;

cout<<" Enter value for a: ";

cin>>a;

cout<<" Enter value for b: ";

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

NISHTHA SINGH 1902168


mas.sum_2();

mas.mul_2();

cout<<endl;

cout<<"Nishtha Singh 1902168";

OUTPUT:

PROGRAM: 54
AIM: Program to demonstrate the multilevel inheritance.

PROGRAM:

97

NISHTHA SINGH 1902168


#include<iostream>

using namespace std;

class A

public:

void display()

cout<<"Base class content.";

};

class B : public A

};

class C : public B

};

int main()

C obj;

obj.display();

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

98

NISHTHA SINGH 1902168


OUTPUT:

PROGRAM: 55
AIM: Program to demonstrate the multiple inheritence.

PROGRAM:

99

NISHTHA SINGH 1902168


#include<iostream>

using namespace std;

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;

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

100

NISHTHA SINGH 1902168


}

OUTPUT:

PROGRAM: 56
AIM: Program to demonstrate the hierarchical inheritance.

101

NISHTHA SINGH 1902168


PROGRAM:

#include<iostream>

using namespace std;

class Number

{ int num;

public: void getNumber(void)

{ cout << "Enter an integer number: ";

cin >> num; }

int returnNumber(void) { return num; } };

class Square:public Number

{ public:

int getSquare(void)

{ int num,sqr;

num=returnNumber(); sqr=num*num;

return sqr; } };

class Cube:public Number

{ public:

int getCube(void)

{ int num,cube;

num=returnNumber();

cube=num*num*num;

return cube; } };

int main()

{ Square objS; Cube objC; int sqr,cube;

102

NISHTHA SINGH 1902168


objS.getNumber(); sqr=objS.getSquare();

cout<<"Square of "<<objS.returnNumber()<<" is: "<<sqr<<endl;

objC.getNumber(); cube=objC.getCube();

cout<<"Cube of "<<objS.returnNumber()<<" is:"<<cube<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

PROGRAM: 57

103

NISHTHA SINGH 1902168


AIM: Program to demonstrate the hybrid inheritance.

PROGRAM:

#include<iostream>

using namespace std;

int a,b,c,d,e;

class A

{ protected:

public: void getab()

{cout<<"\nEnter a and b value:";

cin>>a>>b;} };

class B:public A { protected:

public: void getc()

{ cout<<"Enter c value:";

cin>>c; } };

class C { protected:

public: void getd()

{ cout<<"Enter d value:";

cin>>d; } };

class D:public B,public C {

protected:

public: void result()

{ getab(); getc();

getd(); e=a+b+c+d;

cout<<"\n Addition is :"<<e; }

104

NISHTHA SINGH 1902168


};

int main()

{ D d1;

d1.result();

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

PROGRAM: 58

105

NISHTHA SINGH 1902168


AIM: Program to demonstrate the virtual derivation of a class.

PROGRAM:

#include<iostream>

using namespace std;

class ClassA

public:

int a;

};

class ClassB : public ClassA

public:

int b;

};

class ClassC : public ClassA

public:

int c;

};

class ClassD : public ClassB, public ClassC

public:

int d;

};

106

NISHTHA SINGH 1902168


int main()

{ ClassD obj;

obj.b = 20;

obj.c = 30;

obj.d = 40;

cout<< "\n B : "<< obj.b;

cout<< "\n C : "<< obj.c;

cout<< "\n D : "<< obj.d;

cout<<endl;

cout<<"Nishtha Singh 1902168";

OUTPUT:

PROGRAM: 59

107

NISHTHA SINGH 1902168


AIM: Program to demonstrate the run time polymorphism.

PROGRAM:

#include<iostream>

using namespace std;

class Files{

public:

void virtual download(){

cout<<"Download Files"<<endl;}};

class PdfFiles:public Files{

public:

void download(){

cout<<"Download Pdf"<<endl;}};

int main() {

Files *f = new PdfFiles(); f->download();

delete f;

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;}

OUTPUT:

PROGRAM: 60

108

NISHTHA SINGH 1902168


AIM: Program to demonstrate the exception handling.

PROGRAM:

#include<iostream>

using namespace std;

int main(){

int x = -1;

cout << "Before try \n";

try {

cout << "Inside try \n";

if (x < 0) {

throw x;

cout << "After throw (Never executed) \n";}

catch (int x ) {

cout << "Exception Caught \n"; }

cout << "After catch (Will be executed) \n";

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0; }

OUTPUT:

PROGRAM: 61

109

NISHTHA SINGH 1902168


AIM: Program to demonstrate the use of function template.

PROGRAM:

#include<iostream>

#include<fstream>

using namespace std;

template<class t>

void swap(t *x, t *y) {

t temp = *x;

*x = *y;

*y = temp;}

void fun(int a, int b, float c, float d) {

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;

cout << "Enter A,B values(integer):";

cin >> a>>b;

cout << "Enter C,D values(float):";

cin >> c>>d;

fun(a, b, c, d);

110

NISHTHA SINGH 1902168


cout<<endl;

cout<<"Nishtha Singh 1902168";

OUTPUT:

PROGRAM: 62

111

NISHTHA SINGH 1902168


AIM: Program to demonstrate the use of class template.

PROGRAM:

#include<iostream>

#include<fstream>

using namespace std;

template <class T>

T Large(T n1, T n2)

{ return (n1 > n2) ? n1 : n2; }

int main()

{ int i1,i2;

float f1,f2;

char c1,c2;

cout<<"Enter two integers:\n";

cin>>i1>>i2;

cout<<Large(i1, i2)<<" is larger."<<endl;

cout<<"\nEnter two floating-point numbers:\n";

cin>>f1>>f2;

cout<<Large(f1,f2)<<" is larger.\n\nEnter two characters:\n";

cin>>c1>>c2;

cout<<Large(c1,c2)<<" has larger ASCII value.";

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

112

NISHTHA SINGH 1902168


OUTPUT:

PROGRAM: 63
AIM: Program to demonstrate opening and closing a file.

PROGRAM:

#include<iostream>

#include<fstream>

113

NISHTHA SINGH 1902168


using namespace std;

int main()

{ ofstream fout;

ifstream fin;

char fname[20];

char rec[80], ch;

cout<<"Enter file name: ";

cin.get(fname, 20);

fout.open(fname, ios::out);

if(!fout)

{ cout<<"Error in opening the file "<<fname;

exit(1);}

cin.get(ch);

cout<<"\nEnter a line to store in the file:\n";

cin.get(rec, 80);

fout<<rec<<"\n";

cout<<"\nThe entered line stored in the file successfully..!!\nPress any key to


see...\n";

fout.close();

fin.open(fname, ios::in);

if(!fin)

{ cout<<"Error in opening the file "<<fname;

cout<<"\nPress any key to exit...";

exit(2);}

cin.get(ch);

114

NISHTHA SINGH 1902168


fin.get(rec, 80);

cout<<"\nThe file contains:\n";

cout<<rec;

cout<<"\n\nPress any key to exit...\n";

fin.close();

cout<<endl;

cout<<"Nishtha Singh 1902168";

OUTPUT:

PROGRAM:64
AIM: Program to copy the contents of a file to another file.

PROGRAM:

#include<iostream>

115

NISHTHA SINGH 1902168


#include<fstream>

using namespace std;

int main()

ifstream fs;

ofstream ft;

char ch, fname1[20], fname2[20];

cout<<"Enter Source File Name With Extension : ";

gets(fname1);

fs.open(fname1);

if(!fs)

{ cout<<"Error In Opening Source File..!!"; exit(1);}

cout<<"Enter Destination File Name With Extension : ";

gets(fname2); ft.open(fname2);

if(!ft)

{ cout<<"Error In Opening Target File..!!";

fs.close();

exit(2);}

while(fs.eof()==0)

{ fs>>ch; ft<<ch;}

cout<<"File Copied Successfully..!!";

fs.close();

ft.close();

cout<<endl;

116

NISHTHA SINGH 1902168


cout<<"Nishtha Singh 1902168";

OUTPUT:

PROGRAM: 65
AIM: Program to demonstrate reading and writing of a file.

PROGRAM:

117

NISHTHA SINGH 1902168


#include<iostream>

#include<fstream>

using namespace std;

int main()

fstream file;

file.open("sample.txt",ios::out);

if(!file)

cout<<"Error in creating file!!!"<<endl;

return 0;

cout<<"File created successfully."<<endl;

file<<"Nishtha Singh";

file.close();

file.open("sample.txt",ios::in);

if(!file)

cout<<"Error in opening file!!!"<<endl;

return 0;

char ch;

cout<<"File content: ";

while(!file.eof())

118

NISHTHA SINGH 1902168


{

file>>ch;

cout<<ch;

file.close();

cout<<endl;

cout<<"Nishtha Singh 1902168";

return 0;

OUTPUT:

119

NISHTHA SINGH 1902168

You might also like