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

C++ PRACTICAL FILE

S.no Program name Teacher.


Sign
1 WAP TO FIND THE GREATEST OF 3 NUMBERS
2 WAP TO CALCULATE FACTORIAL OF A NUMBER.
3 WAP TO PRINT FIBONACCI SERIES OF n UMBERS,WHERE N
IS GIVEN BY THE PROGRAMMER
4 WAP TO CHECK WHETHER A NUMBER IS PRIME OR NOT
5 WAP TO COUNT CHARACTERS IN A STRING
6 WAP TO READ A SET OF NUMBERS IN AN ARRAY AND TO
FIND THE LARGEST OF THEM
7 WAP TO IMPLEMENT BUBBLE SORT USING ARRAYS
8 WAP TO READ A SET OF NUMBERS FROM KEYBOARD AND
TO FIND SUM OF ALL ELEMENTS OF THE GIVEN ARRAY
USING A FUNCTION
9 WAP TO SWAP VARIABLE USING CALL BY VALUE
10 WAP TO SWAP VARIABLES USING CALL BY REFERENCE
11 WAP TO FIND THE SUM OF THREE NUMBERS USING
POINTER TO FUNCTION METHOD
12 WAP TO DISPLAY CONTENT OF AN ARRAY USING POINTER
ARITHMETIC
13 WAP TO FIND AREA OF CIRCLE RECTANGLE,SQUARE
&TRIANGLE USING FUNCTION OVERLOADING
14 WAP TO DEFINE NESTED CLASS STUDENT_INFO WHICH
CONTAINS DATA MEMBERS SUCH AS NAME, ROLL NUMBER
AND SEX AND ALSO CONSISTS OF ONE MORE CLASS DATE
WHOSE DATA MEMBERS ARE DAY, MONTH AND YEAR. THIS
DATA IS TO BE READ FROM THE KEYBOARD & DISPLAYED
ON THE SCREEN
15 WAP TO GENERATE A SERIES OF FIBONACCI NUMBERS
USING COPY CONSTRUCTOR WHERE IT IS DEFINED THE
CLASS USING SCOPE RESOLUTION OPERATOR
16 WAP TO ADD TWO COMPLEX NUMBERS WITH A FRIEND
FUNCTION
17 WRITE A CLASS STRING TO COMPARE TWO STRINGS,
OVERLOAD(==) OPERATOR
18 WRITE A CLASS TO CONCATENATE TWO STRINGS,

Page 2
OVERLOAD(+) OPERATOR.
19 CREATE A CLASS ITEM, HAVING TWO DATA MEMBERS X & Y,
OVERLOAD -(UNARY OPERATOR) TO CHANGE THE SIGN OF
X AND Y
20 CREATE A CLASS EMPLOYEE. DERIVE 3 CLASSES FROM THIS
CLASS NAMELY, PROGRAMMER, ANALYST & PROJECT
LEADER. TAKE ATTRIBUTES AND OPERATIONS ON YOUR
OWN. WAP TO IMPLEMENT THIS WITH ARRAY OF
POINTERS.
21 CREATE TWO CLASSES NAMELY EMPLOYEE AND
QUALIFICATION. USING MULTIPLE INHERITANCE DERIVE
TWO CLASSES SCIENTIST AND MANAGER. TAKE SUITABLE
ATTRIBUTES & OPERATIONS. WAP TO IMPLEMENT THIS
CLASS HIERARCHY
22 WAP TO READ DATA FROM KEYWORD & WRITE IT TO THE
FILE. AFTER WRITING IS COMPLETED, THE FILE IS CLOSED.
THE PROGRAM AGAIN OPENS THE SAME FILE, READS

Page 3
1.WAP TO FIND THE GREATEST OF 3 NUMBERS.
CODING:
#include<iostream.h>

Void main() {
Int<<enter 3 numbers:;
Cin>>x>>y>>z;
Cout<<endl;

If(x>y && x>z)


Cout<<x<<is largest<<endl;
}
Else if(y>x && y>z)
Cout<<y<< is largest<<endl:
}
Else {
Cout<<z<<is largest<<endl;
}
}

OUTPUT:
Enter the number:4 7 2
7 is largest

Page 4
2. WAP TO CALCULATE FACTORIAL OF A NUMBER.
CODING:

#include<iostream.h>

Void factorial(int num) {


Unsigned long int fac=1;
While(num!=0) {
Fac=fac*num;
num;
}

Cout<<endl<<THE FACTORIAL IS<<fac<<endl;

Void main() {
Unsigned long int num;

Cout<<Enter number:;
Cin>>num;
Cout<<endl;
Factorial(num);

OUTPUT:
Enter number:3
The factorial is 6

Page 5
3.WAP TO PRINT FIBONACCI SERIES OF n NUMBERS,WHERE N IS
GIVEN BY THE PROGRAMMER.
CODING:

#include<iostream.h>

Void Fibonacci(int count) {


Int fib=1, i=0 j=0;
Cout<<0<< ;

While(count!=0) {
Cout<<fib<< ;
i=j;
j=fib;
fib=i+j;
count--;
}
}

Void main() {
Int n;
Cout<<enter the number of instances: ;
Cin>>n;
Cout<<endl;

Fibonacci(n);
}

OUTPUT:
Enter number of instances :8
1 1 1 2 3 5 8 13

Page 6
4.WAP TO CHECK WHETHER A NUMBER IS PRIME OR NOT
CODING:

#include<iostream.h>

Void prime(int num) {


Int I,count=0;

For(i=0; i<num/2;i++) {
If(num%2==0) {
Count++;
}
}

If(count>1) {
Cout<<endl<<the number is not prime<<endl;
}
Else {
cout <<endl<<the number is prime<<endl;
}
}
Void main() {
Int num;

Count<<enter the number:;


Cin>>num;
Cout<<endl:

Prime(num);
}

OUTPUT:
Enter number: 42
The number is not prime

Page 7
5. WAP TO COUNT CHARACTERS IN A STRING.
CODING:

#include<iostream.h>
#include<string.h>

Void main() {
Char line[100];
Int count,I,space=0;

Cout<<please enter a sentence: ;


Cin.getline(line,99);
Count=strlen(line);

For(i=0;i<count;i== {
If(line[i]== ) {
Space++;
Count++;
}
}
Cout<<endl<<the numbers of characters is=<<count<<endl;
Cout<<endl<<The numbers of words is=<<space<<endl;

OUTPUT:
Please enter a sentence:my name is kartik
The number of characters is = 24
The number of words is = 5

Page 8
6.WAP TO READ A SET OF NUMBERS IN AN ARRAY AND TO FIND
THE LARGEST OF THEM.
CODING:

#include<iostream.h>

Void main() {
Int set[20],I,large=0,limit;

Cout<<enter number of entries: ;


Cin>> limit;

For(i=1;i<limit;i++ {
Cout<<enter position <<i<<:;
Cin>>set[i];
If(set[i]>large) {
Large=set[i];
}
}
Cout<<endl<<the largest value is<<large<<endl;
}

OUTPUT:
Enter no of entries:6
Enter position 1: 23
Enter position 2:12
Enter position 3:54
Enter position 4:76
Enter position 5:36
Enter position 6:69
The largest value is 76

Page 9
7.WAP TO IMPLEMENT BUBBLE SORT USING ARRAYS.
CODING:

#include<iostream.h>

Void bubble_sort(int array[]);


Void main() {
Int matrix[10], I;

Cout<<enter elements of the erray: <<endl;


For(i=0;i<10;i++ {
Cin>>matrix[i];

Bubble_sort(matrix);
Cout<<the sorted list is: <<endl;

For(i=o;i<10;i++) {
Cout<<matrix[i]<< ;
}
}

Void bubble_sort(int array[]) {


Intj.k=1,temp;

While(k<10) {
For(j=0.j<10.j++) {
If(array[j]>array[j+1];
Temp=array[j+1];
Array[j]=array[j+1];
Array[j+1]=temp;
}
}
K++;
}
}

OUTPUT:
enter the element of an array: 5 7 3 8 12 6 9 2 4 15
the sorted list is: 2 3 4 5 6 7 8 9 12 15

Page
10
8.WAP TO READ A SET OF NUMBERS FROM KEYBOARD AND TO
FIND SUM OF ALL ELEMENTS OF THE GIVEN ARRAY USING A
FUNCTION

CODING:

#include<iostream.h>

Void add(int arr[],int n) {


Int I,sum=0;
For(i=1;i<=n;i++) {
Sum=sum+arr[i];
}
Cout<<endl<<the sum is<<sum<<endl;
}
Void main() {
Int set[10],I,sum=0,limit;

Cout<<enter number of entries: ;


Cin>>limit;

For(i=1;i<=limit;i++) {
Cout<<enter position <<i<< : ;
Cin>>set[i];
}
Add(set,limit);

OUTPUT :
Enter number of entries:5
Enter position 1: 22

Page
11
Enter position 2: 56
Enter position 3: 12
Enter position 4: 33
Enter position 5: 9

The sum is 132

9.WAP TO SWAP VARIABLE USING CALL BY VALUE


CODING
#include<iostream.h>

Void swap(int a,int b);

Void main()

{int x,y;

Page
12
Cout<<please enter elements to be swapped;

Cin>>x>>y;

Cout<<endl<<the elements are<<x<<&y;

Swap(x,y);

Void swap(int a,int b)

{a=a+b;

b= a-b;

a=a-b;

cout<<endl<<the swapped elements are<<a<<&<<b<<endl;

OUTPUT:
Please enter the elements to be swapped: 16 73
The elements are: 16 & 73
The swapped elements are: 73 &16

10.WAP TO SWAP VARIABLES USING CALL BY REFERENCE


CODING:
#include<iostream.h>

Void swap(int *a,int* b);

Void main()

{int x,y;

Page
13
Cout<<please enter elements to be swapped;

Cin>>x>>y;

Cout<<endl<<the elements are<<x<<&y;

Swap(&x,&y);

Void swap(int*a.int*b)

Int temp;

Temp=*a;

*a=*b;

*b=temp;

Cout<<endl<<the swapped elements are<<*a<<&<<*b<<endl;

OUTPUT:
Please enter the elements to be swapped: 16 73
The elements are: 16 & 73
The swapped elements are: 73 &16

11.WAP TO FIND THE SUM OF THREE NUMBERS USING POINTER TO


FUNCTION METHOD
CODING
#include<iostream.h>

#include<conio.h>

Int swap(int&n,int&b,int&c)

Page
14
{

Int s;

S=a+b+c;

Return s;

Void main()

Int a,b,c,s;

Clrscr();

Cout<<enter the no;

Cin>>a>>b>>c;

S=swap(a,b,c);

Cout<<the sum is<<s;

Getch();

OUTPUT:
Enter the no 5 7 9
The sum is 21

12.WAP TO DISPLAY CONTENT OF AN ARRAY USING POINTER


ARITHMETIC.
CODING
#include<iostream.h>

#include<conio.h>

Page
15
Void main()

Int n,a[20],i.*p=NULL,t=0;

Clrscr();

Cout<<enter the no of elements<<n;

Cout<<enter the elements;

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

Cin>>a[i];

p=&a[0];

do
{

cout<<endl<<*p;

p++;

t++;

While(t!=n);

getch();}

OUTPUT:
enter the no of elements 5
enter the element 9 7 3 10 4
9

Page
16
7
3
10
4

13.WAP TO FIND AREA OF CIRCLE ,RECTANGLE,SQUARE &TRIANGLE


USING FUNCTION OVERLOADING
CODING:
#include<iostream.h>

Page
17
Float calc(float r,float cons);

Int calc(int l,int h);

Int calc(int l);

Float calc(int l,int h,float cons);

Void main()

Int length,height;

Float radius;

Cout<<enter radius of circle:;<<radius;

Cout<<endl<<the area of circle is:<calc(radius,3.14)<<endl;

Cout<<enter the length of rectangle<<length;

Cout<<enter the height of rectangle<<height;

Cout<<endl<<the area of rectangle is:<calc(length,height)<<endl;

Cout<<enter side of square<<length;

Cout<<endl<<the area of square is:<calc(length)<<endl;

Cout<enter base of triangle;<<length;

Cout<enter height of triangle;<<height;

Cout<<endl<<the area of triangle is:<calc(length,height,0.5)<<endl;

Float calc(float r, float cons)

{return(cons*r*r);

Int calc(int l, int h)

{return(l*l);

Float calc(int l, int h,float cons)

{return (l*h*cons);

Page
18
OUTPUT:
enter radius of circle:5
the area of circle is:78.5

enter the length of rectangle:12


enter the height of rectangle:6
the area of rectangle is:72

enter side of square:10


the area of square is:100

enter base of triangle:7


enter height of triangle:12
the area of triangle is:42

14. WAP TO DEFINE NESTED CLASS STUDENT_INFO WHICH CONTAINS DATA


MEMBERS SUCH AS NAME, ROLL NUMBER AND SEX AND ALSO CONSISTS OF
ONE MORE CLASS DATE WHOSE DATA MEMBERS ARE DAY, MONTH AND YEAR.
THIS DATA IS TO BE READ FROM THE KEYBOARD & DISPLAYED ON THE SCREEN.

CODING:
#include<iostream.h>

Class student_info

Page
19
{

Private:

Char name[50],sex;

Int roll;

Public:

Void input()

Cout<<endl<<enter name:;

Cin>>day;

Cout<<endl<<enter sex:;

Cin>>sex;

Cout<<endl<<enter roll number:;

Cin>>roll;

Class date

Public:

Int day,month,year;

Void insertdate()

Cout<<endl<<enter numerical day:;

Cin>>day;

Cout<<endl<<enter numerical month;;

Cin>>month;

Cout<<endl<<enter numerical year:;

Cin>>year;

Page
20
}

}d;

Void

Display()

Cout<<endl<<name:<<name;

Cout<<endl<<sex:<<sex;

Cout<<endl<<roll:<<roll;

Cout<<endl<<the date of entry is:;

Cout<<d.day<</<<d.month<</<<d.year<<endl;

};

Void main()

Student_info s1;

Cout<<endl<<Enter details---<<endl;

S1.input();

Cout<<endl<<---Enter Date Of Entry---<<endl;

S1.d.insertdate();

Cout<<endl<<---INFORMATION---<<endl;

S1.display();

OUTPUT

---Enter Details---

Page
21
Enter Name: karan

Enter Sex: M

Enter Roll Number: 1234

---Enter Date Of Entry---

Enter Numerical day: 11

Enter Numerical Month: 09

Enter Numerical Year: 2011

---INFORMATION---

Name: karan

Sex:M

Roll:1234

The Date Of entry is 11/09/2011

15.WAP TO GENERATE A SERIES OF FIBONACCI NUMBERS USING COPY


CONSTRUCTOR WHERE IT IS DEFINED THE CLASS USING SCOPE RESOLUTION
OPERATOR.

CODING :
#include<iostream.h>

Page
22
Public:

Fibonacci():limit(0)

{}

Fibonacci(int li):limit(li)

Int fibo=1,1=0,j=0;

Cout<<0<<;

While(limit!=0)

Cout<<fibo<<;

I=j;

J=fibo;

Fibo=i+j;

Limit--;

};

Void main()

Int n;

Cout<<Enter the number of the instances: ;

Cin>>n;

Fibonacci f(n);

Cout<<endl;

OUTPUT:

Enter number of instances: 8

Page
23
0 1 1 2 3 5 8 13 21

16.WAP TO ADD TWO COMPLEX NUMBERS WITH A FRIEND FUNCTION

CODING:
#include<iostream.h>

#include<conio.h>

Class cmplx

Page
24
Int real,imagin;

Cout<<ENTER THE REAL PART: ;

Cin>>real;

Cout<<ENTER THE IMAGINARY PART: ;

Cin>>imagin;

Friend void sum(complx,complx);

};

Void sun(compx c1,complx c2)

Cout<<RESULT:;

Cout<<[<<c1.real<<+ i<<c1.imagin;

Cout<<]+[<<c2.real<<+ i<<c2.imagin;

Cout<<]=<<c1.real+c2.real<<+ i<<c1.imagin+c2.imagin;\

Void main()

Complx op1,op2;

Cout<<endl<<INPUT OPERAND 1----;

Op1.get();

Cout<<endl<<INPUT OPERAND 2----;

Op2.get();

Sum(op1,op2); }

OUTPUT :

INPUT OPERAND 1----

ENTER THE REAL PART : 5

ENTER THE IMAGINARY PAR : 4

Page
25
INPUT OPERAND 2----

ENTER THE REAL PART : 7

ENTER THE IMAGINARY PART : 2

RESULT : [5+4i]+[7+2i]=12+6i

17. WRITE A CLASS STRING TO COMPARE TWO STRINGS, OVERLOAD(==)


OPERATOR.

CODE :
#include<iostream.h>

#include<string>

Using namespace std;

Class string

Page
26
Char a[10],b[10];

Public:

Voud getdata()

` cout<<Enter the 2 strings=;

Gets(a);

Gets(b);

Void operator==(string)

If(strcmp(a,b)==0)

Cout<<\nStrings equal\n;

Else

Cout<<\nstrings not equal;

};

Int main()

String x;

x.getdata();

==x;

System(pause)

Return 0;

Page
27
OUTPUT :

Enter two strings: cloverfield

Cleverfield

Strings not equal

18.WRITE A CLASS TO CONCATENATE TWO STRINGS, OVERLOAD(+) OPERATOR.

CODING :
#include<iostream.h>

#include<string>

Using namespace std;

Class mystring

Page
28
Char a[10,b[10];

Public:

Void getdata()

Cout<<Enter first string=;

Gets(a);

Cout<<Enter second string=;

Gets(b);

};

Int main()

Mystring x;

x.getdata();

+x;

System(pause);

Return 0;

OUTPUT :

Enter first string=KARAN

Enter second string=SAGGU

KARAN SAGGU

Page
29
19.CREATE A CLASS ITEM, HAVING TWO DATA MEMBERS X & Y, OVERLOAD
-(UNARY OPERATOR) TO CHANGE THE SIGN OF X AND Y.

CODING:
#include<iostream.h>

Using namespace std;

Class item

Page
30
Int x,y;

Public:

Void getdata()

Cout<<Enter the vale of x & y=;

Cin>>x;

Cin>>y;

Void operator (void)

X= -x;

Y= -y;

Void display()

Cout<<\nx=<<x<<\ny=<<y;

};

Int main()

Item x;

x.getdata();

-x;

x.display();

cout<<endl;

Page
31
system(pause);

return 0;

OUTPUT :

Enter the value of x & y=15-30

X= -15

Y= 30

20. CREATE A CLASS EMPLOYEE. DERIVE 3 CLASSES FROM THIS CLASS NAMELY,
PROGRAMMER, ANALYST & PROJECT LEADER. TAKE ATTRIBUTES AND
OPERATIONS ON YOUR OWN. WAP TO IMPLEMENT THIS WITH ARRAY OF
POINTERS.

CODING :
#include<iostream.h>

#include<conio.h>

#include<string.h>

Page
32
Class employee

Private:

Char name[20];

Int salary;

Public :

Void putData(int sal, char nam[20])

Strepy(name,nam);

Salary=sal;

Char* getName(void)

Return name;

Int getSal(void)

Return salary;

};

Class programmer:public employee

Private:

Char skill[10];

Public:

Programmer(char name[20],int sal,char skil[20])

Page
33
{

putData(sal,name);

strepy(skill,skil);

Void display(void)

Cout<<\n\nProgrammer : \n;

Cout<<\nName : <<getName();

Cout<<\nSalary : <<getSal();

Cout<<\nSkill : <<skill;

};

Class analyst:public employee

Private:

Char type[10];

Public:

Analyst(char name[20],int sal,char typ[20])

putData(sal,name);

strepy(type,typ);

Void display(void)

Cout<<\n\n Analyst :\n;

Page
34
Cout<<\nName :<<getName();

Cout<<\nsalary :<<getSal();

Cout<<\nType :<<type;

};

Class proj_leader:public employee

Private:

Char pName[10];

Public:

Proj_leader(char name[20], int sal,char pNam[20])

putData(sal,name);

strepy(pName,pNam);

Void display(void)

Clrscr();

Proj_leader prl(akshay,10000,software Development);

Analyst al(ameeca,8600,post);

Programmer p1(ABC,12000,C++);

Pr1.display();

A1.display();

P1.display();

Getch();

Page
35
OUTPUT :

Project Leader :

Name : Akshey

Salary : 10000

Project : Software Development

Analyst :

Name : Ameeca

Salary : 8600

Type : Post

Programmer :

Name : ABC

Salary : 12000

Skill : C++

21.CREATE TWO CLASSES NAMELY EMPLOYEE AND QUALIFICATION. USING


MULTIPLE INHERITANCE DERIVE TWO CLASSES SCIENTIST AND MANAGER. TAKE
SUITABLE ATTRIBUTES & OPERATIONS. WAP TO IMPLEMENT THIS CLASS
HIERARCHY

CODING :
#include<iostream.h>

#include<conio.h.

#include<stdio.h>

Class employee

Page
36
{

Char empname[10];

Int empid;

Public:

Void getemp()

Cout<<endl<<Enter Emlpoyee Name :;

Gets(empname);

Cout<<Enter Employee Id :;

Cin>>empid;

Void display()

Cout<<endl<<Name :<<empname;

Cout<<endl<<Id :<<empid;

};

Class qualification

Int exp;

Public:

Void getqual()

Cout<<Enter Year Of Working Experience :;

Cin>>exp;

Page
37
}

Void dispqual()

Cout<<endl<<Experiece=<<exp<<years;

};

Class scientist:public employee,public qualification

Int projid;

Public:

Void getproject()

Cout<<Enter Project Id :;

Cin>>projid;

Void dispproj()

Cout<<endl<<PROJECT ID : <<projid;

};

Class manager;public employee,public qualification

Int groupid;

Public:

Void getgroup()

Cout<<Enter Group Id :;

Page
38
Cin>>groupid;

Void dispgroup()

Cout<<endl<<Group ID : <<groupid;

};

Void main()

Clrscr();

Scientist s;

Manager m;

Cout<<FOR SCIENTIST::::<<endl;

s.getemp();

s.getqual();

s.getproject();

s.display();

s.dispqual();

s.dispproj();

cout<<endl<<endl<<endl<<FOR MANAGER::::<<endl;

m.getemp();

m.getqual();

m.getgroup();

m.display();

Page
39
m.dispqual();

m.dispgroup();

getch();

OUTPUT :

FOR SCIENTIST :-

Enter Employee Name : Akash

Enter Employee Id : 13012

Enter Years Of Working Experience: 6

Enter Projext Id : 443

Name : Akash

Page
40
Id: 13012

Experience=6 years

PROJECT ID : 443

FOR MAMAGER::::

Enter Employee Name : Sameera

Enter Employee Id : 1445

Enter Years Of Working Experience : 16

Enter Group Id : 5002

Name : Sameera

Id : 1445

Experience= 16 years

Group Id : 5002

22.WAP TO READ DATA FROM FROM KEYWORD & WRITE IT TO THE FILE. AFTER
WRITING IS COMPLETED, THE FILE IS CLOSED. THE PROGRAM AGAIN OPENS THE
SAME FILE, READS.

CODING:
#include<iostream.h>

#include<fstream.h>

Void main(void)

Char string[255];

Int ch;

Page
41
Cout<<\nMENU\n)Write To File\n2)Read From File\nEnter Choice : ;

Cin>>ch;

Switch(ch)

Case 1:

Cout<<\nEnter String To Write To File :;

Cin>>string;

Ofstream fout;

Fout.open(myfile.txt);

Fout<<string;

Fout<<flush;

Fout.close();

Break;

Case 2:

Ifstream fin;

Fin.open(myfile.txt)

Fin>>string;

Cout<<\nFile Read : \n<<string;

Fin.close();

Break;

Default:

Cout<<INVALID CHOICE;

Page
42
OUTPUT :

MENU

1)Write To File

2)Read From File

Enter Choice: 1

Enter string to write to file: hi i am karan

MEMU

1)Write To File

2)Read From File

Enter Choice: 2

File Read: hi i am karan

Page
43

You might also like