Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

msc cpp

The document lists a series of programming tasks, each requiring the creation of a specific program in C++. These tasks range from basic operations like printing 'Hello World' and adding two numbers to more complex concepts such as recursion, inheritance, and operator overloading. Each task includes a brief description and sample code for implementation.

Uploaded by

lbjaatofficial
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

msc cpp

The document lists a series of programming tasks, each requiring the creation of a specific program in C++. These tasks range from basic operations like printing 'Hello World' and adding two numbers to more complex concepts such as recursion, inheritance, and operator overloading. Each task includes a brief description and sample code for implementation.

Uploaded by

lbjaatofficial
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Sr.

no Program Name Signature


1 Write a program to print hello world
2 Write a program to add two number
3 Write a program to compare two numbers
4 Write a program of simple calculator using switch statement
5 Write a program to write a table in while loop
6 Write a program to write a table in do while loop
7 Write a program to write a table in for loop
8 Write a program to print reverse of a number
9 Write a program to print the factorial a number
10 Write a program to show example of user define function
11 Write a program to show a example of pointer variable
12 Write a program to show example of pass by value
13 Write a program to show example of pass by reference
14 Write a program to find factorial using recursion
15 Write a program to add two one dimensional array
16 Write a program to show example of inline function
17 Write a program to show example of function overloading
18 Write a program to show example of constructor
19 Write a program to example of constructor overloading
20 Write a program to show use of static data member and function
21 Write a program to show example of friend function
22 Write a program to show example of single inheritance
23 Write a program to example of multiple inheritances
24 Write a program to show example of function overriding
25 Write a program to show example of operator overloading
26 Write a program to show example of virtual function
27 Write a program to example of templates
28 Write a Program to linear search in list
29 Write a Program to binary search in list
30 Write a Program of Bubble sort in array
31 Write a Program of Merge sorting in array
32 Write a Program of Quick sort
33 Write a Program of Selection sort
34 Write a Program of radix sort
35 Write a Program of insertion sort
Output

Hello world

1.Write a program to print hello world

#include<iostream.h>

#include<conio.h>

void main()

cout<<"Hello world";

getch();

}
Output

enter first number 10

enter second number 20

the sum is 30

2. Write a program to add two numbers

#include<iostream.h>

#include<conio.h>

void main()

int a;

int b;

cout<<"enter first number ";

cin>>a;

cout<<"enter second number ";

cin>>b;

cout<<"the sum is "<<a+b;

getch();

}
Output

enter first number 10

enter second number 5

First number is bigger than second

3. Write a program to compare two numbers

#include <iostream.h>

#include<conio.h>

void main()

int a;

int b;

cout<<"enter first number ";

cin>>a;

cout<<"enter second number ";

cin>>b;

if (a>b)

cout<<"First number is bigger than second";

else if (a<b)

cout<<"second number is bigger than first";

else

cout<<"both numbers are equal";

getch();

}
Output

enter oprator: p,s,m,d m

enter two numbers 10 20

10*20=200

4.Write a program of simple calculator using switch statement

#include<iostream.h>

#include<conio.h>

void main()

char op;

int a,b;

cout<<"enter oprator: p,s,m,d ";

cin>>op;

cout<<"enter two numbers ";

cin>>a>>b;

switch(op)

case 'p': cout<<a<<" + "<<b<< " = "<<a+b;

break;

case 's':cout<<a<<" - "<<b<< " = "<<a-b;

break;

case 'm': cout<<a<<" * "<<b<< " = "<<a*b;

break;

case 'd':cout<<a<<" / "<<b<< " = "<<a/b;

break;

default :cout<<"wrong operator entered ";

break;

getch();

}
Output

enter a number 5

the table of 5 is

10

15

20

25

30

35

40

45

50

5. Write a program to write a table in while loop

#include<iostream.h>

#include<conio.h>

void main()

int a;

int i=1;

cout<<"enter a number ";

cin>>a;

cout<<"the table of "<< a<<" is"<<endl ;

while (i<=10)

cout<<a*i<<endl;

i=i+1;

getch();

}
Output

enter a number 5

the table of 5 is

10

15

20

25

30

35

40

45

50

6. Write a program to write a table in do while loop

#include<iostream.h>

#include<conio.h>

void main()

int a;

int i=1;

cout<<"enter a number ";

cin>>a;

cout<<"the table of "<< a<<" is"<<endl ;

do

cout<<a*i<<endl;

i=i+1;

} while (i<=10);

getch();

}
Output

enter a number 4

4*1=4

4*2=8

4*3=12

4*4=16

4*5=20

4*6=24

4*7=28

4*8=32

4*9=36

4*10=40

7. Write a program to write a table in for loop

#include<iostream.h>

#include<conio.h>

void main()

int a;

cout<<"enter a number :";

cin>>a;

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

cout<< a <<" * "<<i<< " = "<<a*i<<endl;

getch();

}
Output

Enter a number 234

reversed number = 432

8. Write a program to print reverse of a number

#include<iostream.h>

#include<conio.h>

void main()

int a, rev=0,rem;

cout<<"enter a number ";

cin>>a;

while (a !=0)

rem = a%10;

rev= rev*10 + rem;

a/=10;

cout<<"reversed number = "<< rev;

getch();

}
Output

Enter a positive integer: 5

Factorial of 5= 120

9. Write a program to print the factorial a number

#include <iostream.h>

#include<conio.h>

void main()

int n;

long factorial = 1;

cout << "Enter a positive integer: ";

cin >> n;

if (n < 0)

cout << "Error! Factorial of a negative number doesn't exist.";

Else

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

factorial *= i;

cout << "Factorial of " << n << " = " << factorial;

getch();

}
Output

hello world

10. Write a program to show example of user define function

#include<iostream.h>

#include<conio.h>

void hello()

cout<<"hello world";

void main()

hello();

getch();

}
Output

0X00410

0X00410

0X02010

11. Write a program to show a example of pointer variable

#include<iostream.h>

#include<conio.h>

void main()

int a=1;

int* b;

b=&a;

cout<<a<<endl; //print the value of 'a'

cout<<b<<endl; //print the memory location of 'a'

cout<<&a<<endl; //print the memory location of 'a'

cout<<&b<<endl; //print the memory location of 'b'

cout<<*b<<endl; //print the value of address stored in 'b'

getch();

}
Output

10

12. Write a program to show example of pass by value

#include <iostream.h>

#include<conio.h>

void increment(int i)

i++;

void main()

int a = 10;

increment(a);

cout << a << endl; // Outputs 10

getch();

}
Output

11

13.Write a program to show example of pass by reference

#include <iostream.h>

#include<conio.h>

void increment(int *a)

a++;

void main()

int a = 10;

increment(&a);

cout << a << endl; // Outputs 11

getch();

}
Output

enter a number 5

factorial of 5 is 120

14. Write a program to find factorial using recursion

#include<iostream.h>

#include<conio.h>

void main()

int factorial(int a);

int a;

cout<<"enter a number ";

cin>>a;

cout<<"factorial of "<<a<<" is "<<factorial(a);

int factorial(int a)

if(a>1)

return a*factorial(a-1);

Else

return 1;

}
Output

Enter the size of the arrays: 2 2

Enter the elements of first array: 1

Enter the elements of second array:5

The sum of the arrays is: 6

10

12

15. Write a program to add two one dimensional arrays

#include <iostream.h>

#include<conio.h>

void main()

int n;

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

cin >> n;

int a[n], b[n], c[n];

cout << "Enter the elements of first array: ";

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

cin >> a[i];

cout << "Enter the elements of second array: ";

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


{

cin >> b[i];

// Adding the arrays

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

c[i] = a[i] + b[i];

cout << "The sum of the arrays is: ";

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

cout << c[i] << " ";

getch();

}
Output

The maximum value is: 20

16 Write a program to show example of inline function

#include <iostream.h>

#include<conio.h>

inline int max(int x, int y)

return (x > y) ? x : y;

void main()

int a = 10, b = 20;

int m = max(a, b);

cout << "The maximum value is: " << m << endl;

getch();

}
Output

The maximum integer value is: 20

The maximum double value is:20.5

17. Write a program to show example of function overloading

#include <iostream.h>

#include<conio.h>

int max(int x, int y)

return (x > y) ? x : y;

double max(double x, double y)

return (x > y) ? x : y;

void main()

int a = 10, b = 20;

cout << "The maximum integer value is: " << max(a, b) << endl;

double c = 10.5, d = 20.5;

cout << "The maximum double value is: " << max(c, d) << endl;

getch();

}
Output

The area of the rectangle is: 200

18.Write a program to show example of constructor

#include <iostream.h>

#include<conio.h>

class Rectangle

public:

int length;

int width;

Rectangle(int l, int w)

length = l;

width = w;

int area()

return length * width;

};

void main()

Rectangle rect(10, 20);

cout << "The area of the rectangle is: " <<rect.area() << endl;

getch();

}
Output

The area of rect1 is 100

The area of rect2 is: 50

The area of rect3 is: 200

19. Write a program to example of constructor overloading

#include <iostream.h>

#include<conio.h>

class Rectangle

private:

int length;

int width;

public:

Rectangle()

length =1 0;

width = 10;

Rectangle(int i)

length = i;

width = 5;

Rectangle(int l, int w)

length = l;

width = w;

}
int area()

return length * width;

};

void main()

Rectangle rect1;

cout << "The area of rect1 is: " << rect1.area() << endl;

Rectangle rect2(10);

cout << "The area of rect2 is: " << rect2.area() << endl;

Rectangle rect3(10, 20);

cout << "The area of rect3 is: " << rect3.area() << endl;

getch();

}
Output

Number of objects created: 3

20. Write a program to show use of static data member and function

#include <iostream.h>

#include<conio.h>

class Counter

private:

static int count;

public:

Counter()

count++;

static int getCount()

return count;

};

int Counter::count = 0;

void main()

Counter c1, c2, c3;

cout << "Number of objects created: " <<Counter::getCount() << endl;

getch();

}
Output

The area of the rectangle is: 200

21. Write a program to show example of friend function

#include <iostream.h>

#include<conio.h>

class Rectangle

private:

int length;

int width;

public:

Rectangle(int l, int w)

length = l;

width = w;

friend int area(Rectangle rect);

};

int area(Rectangle rect)

return rect.length * rect.width;

void main()

Rectangle rect(10, 20);

cout << "The area of the rectangle is: " << area(rect) << endl;

getch();

}
Output

The area of the rectangle is: 200

22. Write a program to show example of single inheritance

#include <iostream.h>

#include<conio.h>

class Shape

protected:

int width;

int height;

public:

Shape(int w, int h)

width = w;

height = h;

};

class Rectangle: public Shape

public:

Rectangle(int w, int h): Shape(w, h) { }

int getArea()

return width * height;

};

void main()

{
Rectangle rect(10, 20);

cout << "The area of the rectangle is: " <<rect.getArea() << endl;

getch();

}
Output

The area of the rectangle is: 200

The colour of the rectangle is: red

23. Write a program to example of multiple inheritances

#include <iostream.h>

#include<conio.h>

class Shape

protected:

int width;

int height;

public:

Shape(int w, int h)

width = w;

height = h;

};

class Colour

protected:

string color;

public:

Colour(string c)

color = c;

};

class Rectangle: public Shape, public Colour

{
public:

Rectangle(int w, int h, string c): Shape(w, h), Colour(c) { }

int getArea()

return width * height;

string getColor()

return colour;

};

void main()

Rectangle rect(10, 20, "red");

cout << "The area of the rectangle is: " <<rect.getArea() << endl;

cout << "The colour of the rectangle is: " <<rect.getColor() << endl;

getch();

}
Output

Derived Function

24. Write a program to show example of function overriding

#include <iostream.h>

#include<conio.h>

class Base

public:

void print()

cout << "Base Function" << endl;

};

class Derived : public Base

public:

void print()

cout << "Derived Function" << endl;

};

void main()

Derived derived1;

derived1.print();

getch();

}
Output

Count: 6

25.Write a program to show example of operator overloading

#include <iostream.h>

#include<conio.h>

class Count

private:

int value;

public:

Count() : value(5) {}

void operator ++ ()

++value;

void display()

cout << "Count: " << value << endl;

};

void main()

Count count1;

++count1;

count1.display();

getch();

}
Output

Base Function

26. Write a program to show example of virtual function

#include <iostream.h>

#include<conio.h>

class Base

public:

virtual void print()

cout << "Base Function" << endl;

};

class Derived : public Base

public:

void print()

cout << "Derived Function" << endl;

};

void main()

Derived derived1;

Base* base1 = &derived1;

base1->print();

getch();

}
Output

7.0

27. Write a program to example of templates

#include <iostream.h>
#include<conio.h>
template <typename T> T myMax(T x, T y)
{
return (x > y) ? x : y;
}
void main()
{
cout << myMax<int>(3, 7);
cout << myMax<double>(3.0, 7.0);

getch();
}
Output

how many number

enter the element of list

34

65

39

11

enter the item to search

39

the position of item is 3

28 Write a Program to linear search a number in list

#include<iostream.h>

#include<conio.h>

void main()

int a[50],n,i,item;

clrscr();

cout<<"how many number";

cin>>n;

cout<<"enter the element of list";

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

cin>>a[i];

cout<<"enter the item to search";

cin>>item;

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

if(a[i]==item)

break;
}

if(i==n)

cout<<"the item is not found";

else

cout<<"the position of item is"<<i+1;

getch();

}
Output

enter n

enter element of list

12

34

65

78

90

enter item

12

element is found at position 1

29 Write a Program to binary search in list

#include<iostream.h>

#include<conio.h>

void main()

int n,i,j,a[50],mid,end,item,big;

clrscr();

cout<<"enter n";

cin>>n;

cout<<"enter element of list";

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

cin>>a[i];

cout<<"enter item";

cin>>item;

big=0;

end=n-1;

mid=(big+end)/2;
while(item!=a[mid]&&big<=end)

if(item>a[mid])

big=mid+1;

else

end=mid-1;

mid=(big+end)/2;

if(big>end)

cout<<"element is not found";

else

cout<<"element is found at position="<<mid+1;

getch();

}
Output

enter the n 5

enter the element

63

33

66

18

99

sorted list is

18

33

63

66

99

30 Write a Program of bubble sorting in array

#include<iostream.h>

#include<conio.h>

void main()

int n,i,p,a[35],t;

clrscr();

cout<<"enter the n";

cin>>n;

cout<<"enter the element";

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

cin>>a[i];

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

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

if(a[i]>a[i+1])

t=a[i];

a[i]=a[i+1];

a[i+1]=t;

cout<<"sorted list is\n";

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

cout<<a[i];

getch();

}
Output

enter no of list

enter all element of list

11

09

44

12

Sorted array

09

12

11

44

31 Write a Program of Merge sorting in array

#include<iostream.h>

#include<conio.h>

void main ()

void mergesort(int[],int,int);

int n,i,a[20];

clrscr();

cout<<"enter no of list";

cin>>n;

cout<<"enter all element of list";

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

cin>>a[i];

mergesort(a,0,n-1);

cout<<"sorted array=";
for(i=0;i<n;i++)

cout<<a[i];

getch();

void mergesort(int a[],int low,int high)

int mid;

void merge(int[],int,int,int);

if(low<high)

mid=(high+low)/2;

mergesort(a,low,mid);

mergesort(a,mid+1,high);

merge(a,low,mid,high);

void merge(int a[],int low,int mid,int high)

int b[20],i,j,m,k;

i=low;

j=mid+1;

k=low;

while(i<=mid&&j<=high)

if(a[i]>a[j])

b[k]=a[j];

k++;

j++;
}

else

b[k]=a[i];

k++;

i++;

while(i<=mid)

b[k]=a[i];

k++;

i++;

while(j<=high)

b[k]=a[j];

k++;

j++;

for(i=low;i<=high;i++)

a[i]=b[i];

}
Output

enter the number of element

enter the element

90

19

44

62

Sorted list

19

44

62

90

32 Write a Program of Quick sort

#include<iostream.h>

#include<conio.h>

void main()

void quicksort(int[],int,int);

int i,n,a[10];

clrscr();

cout<<"enter the no. of elements";

cin>>n;

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

cout<<"enter the element";

cin>>a[i];

quicksort(a,0,n);

cout<<"sorted list is";


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

cout<<a[i];

getch();

void quicksort(int a[],int low,int high)

int position(int[],int,int);

int j;

if(low<high)

j=position(a,low,high);

quicksort(a,low,j-1);

quicksort(a,j+1,high);

int position(int a[], int low, int high)

int i,j,pivot,k;

pivot=a[low];

i=low;

j=high;

do

do

i=i+1;

}while(a[i]<pivot);

do

{
j=j-1;

}while(a[j]>pivot);

if(i<j)

k=a[i];

a[i]=a[j];

a[j]=k;

}while(i<j);

a[low]=a[j];

a[j]=pivot;

return j;

}
Output

enter no of list

enter all element

11

09

44

12

Sorted array

09

12

11

44

33 Write a Program of Selection sort

#include<iostream.h>

#include<conio.h>

void main()

int a[20],j,i,n,k;

clrscr();

cout<<"enter no of list";

cin>>n;

cout<<"enter all element";

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

cin>>a[i]);

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

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

if(a[i]>a[j])

k=a[i];

a[i]=a[j];

a[j]=k;

cout<<"sorted list =";

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

cout<<a[i];

getch();

}
Output

no of element

enter element

11

09

44

12

Sorted list is

09

12

11

44

34 Write a Program of radix sort

#include<iostream.h>

#include<conio.h>

void main()

void radixsort(int[],int);

int i,j,k,n,a[20];

clrscr();

cout<<"no. of elements";

cin>>n;

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

cout<<"enter element";

cin>>a[i];

radixsort(a,n);

cout<<"sorted list is\n";


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

cout<<a[i];

getch();

void radixsort(int a[],int n)

int i,j,exp,b[20],m;

int bucket[10];

m=a[0];

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

if(a[i]>m)

m=a[i];

exp=1;

while(m/exp>0)

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

bucket[i]=0;

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

bucket[a[i]/exp%10]++;

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

{
bucket[i]+=bucket[i-1];

for(i=n-1;i>=0;i--)

b[--bucket[a[i]/exp%10]]=a[i];

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

a[i]=b[i];

exp=exp*10;

Return 0;

}
output

enter no of list

enter a number

11

09

44

12

Sorted list =

09

12

11

44

35 Write a Program of insertion sort

#include<iostream.h>

#include<conio.h>

void main()

int a[20],j,i,n,k,m;

clrscr();

cout<<"enter no of list";

cin>>n;

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

cout<<"enter a no";

cin>>a[i];

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

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

{
if(a[i]<a[j])

k=a[i];

m=i;

while(m!=j)

a[m]=a[m-1];

m--;

a[j]=k;

cout<<"sorted list=";

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

cout<<a[i];

getch();

You might also like