Professional Documents
Culture Documents
Comp Practical C Practical File
Comp Practical C Practical File
Comp Practical C Practical File
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;
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 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>
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
0 1 1 2 3 5 8 13
Page 6
4.WAP TO CHECK WHETHER A NUMBER IS PRIME OR NOT
CODING:
#include<iostream.h>
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;
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;
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;
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>
Bubble_sort(matrix);
Cout<<”the sorted list is: ”<<endl;
For(i=o;i<10;i++) {
Cout<<matrix[i]<<” “;
}
}
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>
For(i=1;i<=limit;i++) {
Cout<<”enter position “<<i<<” : “;
Cin>>set[i];
}
Add(set,limit);
Page
11
OUTPUT :
Enter number of entries:5
Enter position 1: 22
Enter position 2: 56
Enter position 3: 12
Enter position 4: 33
Enter position 5: 9
Page
12
9.WAP TO SWAP VARIABLE USING CALL BY VALUE
CODING
#include<iostream.h>
Void main()
{int x,y;
Cin>>x>>y;
Swap(x,y);
{a=a+b;
b= a-b;
a=a-b;
OUTPUT:
Please enter the elements to be swapped: 16 73
The elements are: 16 & 73
The swapped elements are: 73 &16
Page
13
10.WAP TO SWAP VARIABLES USING CALL BY REFERENCE
CODING:
#include<iostream.h>
Void main()
{int x,y;
Cin>>x>>y;
Swap(&x,&y);
Void swap(int*a.int*b)
Int temp;
Temp=*a;
*a=*b;
*b=temp;
OUTPUT:
Please enter the elements to be swapped: 16 73
The elements are: 16 & 73
The swapped elements are: 73 &16
Page
14
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)
Int s;
S=a+b+c;
Return s;
Void main()
Int a,b,c,s;
Clrscr();
Cin>>a>>b>>c;
S=swap(a,b,c);
Getch();
OUTPUT:
Enter the no 5 7 9
The sum is 21
Page
15
12.WAP TO DISPLAY CONTENT OF AN ARRAY USING POINTER
ARITHMETIC.
CODING
#include<iostream.h>
#include<conio.h>
Void main()
Int n,a[20],i.*p=NULL,t=0;
Clrscr();
For(i=0;i<n;i++)
Cin>>a[i];
p=&a[0];
do
{
cout<<endl<<*p;
p++;
t++;
While(t!=n);
getch();}
Page
16
OUTPUT:
enter the no of elements 5
enter the element 9 7 3 10 4
9
7
3
10
4
Page
17
13.WAP TO FIND AREA OF CIRCLE ,RECTANGLE,SQUARE &TRIANGLE
USING FUNCTION OVERLOADING
CODING:
#include<iostream.h>
Void main()
Int length,height;
Float radius;
{return(cons*r*r);
Page
18
}
{return(l*l);
{return (l*h*cons);
OUTPUT:
enter radius of circle:5
the area of circle is:78.5
Page
19
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
Private:
Char name[50],sex;
Int roll;
Public:
Void input()
Cout<<endl<<”enter name:”;
Cin>>day;
Cout<<endl<<”enter sex:”;
Cin>>sex;
Cin>>roll;
Class date
Public:
Int day,month,year;
Void insertdate()
Page
20
Cout<<endl<<”enter numerical day:”;
Cin>>day;
Cin>>month;
Cin>>year;
}d;
Void
Display()
Cout<<endl<<”name:”<<name;
Cout<<endl<<”sex:”<<sex;
Cout<<endl<<”roll:”<<roll;
Cout<<d.day<<”/”<<d.month<<”/”<<d.year<<endl;
};
Void main()
Student_info s1;
Cout<<endl<<”—Enter details---“<<endl;
S1.input();
S1.d.insertdate();
Page
21
Cout<<endl<<”---INFORMATION---‘<<endl;
S1.display();
OUTPUT
---Enter Details---
Enter Sex: M
---INFORMATION---
Name: karan
Sex:M
Roll:1234
Page
22
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>
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;
Page
23
Cin>>n;
Fibonacci f(n);
Cout<<endl;
OUTPUT:
0 1 1 2 3 5 8 13 21
Page
24
16.WAP TO ADD TWO COMPLEX NUMBERS WITH A FRIEND FUNCTION
CODING:
#include<iostream.h>
#include<conio.h>
Class cmplx
Int real,imagin;
Cin>>real;
Cin>>imagin;
};
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;
Op1.get();
Page
25
Op2.get();
Sum(op1,op2); }
OUTPUT :
RESULT : [5+4i]+[7+2i]=12+6i
Page
26
17. WRITE A CLASS STRING TO COMPARE TWO STRINGS, OVERLOAD(==)
OPERATOR.
CODE :
#include<iostream.h>
#include<string>
Class string
Char a[10],b[10];
Public:
Voud getdata()
Gets(a);
Gets(b);
Void operator==(string)
If(strcmp(a,b)==0)
Cout<<”\nStrings equal\n”;
Else
};
Int main()
Page
27
{
String x;
x.getdata();
==x;
System(“pause”)
Return 0;
OUTPUT :
Cleverfield
Page
28
18.WRITE A CLASS TO CONCATENATE TWO STRINGS, OVERLOAD(+) OPERATOR.
CODING :
#include<iostream.h>
#include<string>
Class mystring
Char a[10,b[10];
Public:
Void getdata()
Gets(a);
Gets(b);
};
Int main()
Mystring x;
x.getdata();
+x;
System(“pause”);
Return 0;
Page
29
OUTPUT :
KARAN SAGGU
Page
30
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>
Class item
Int x,y;
Public:
Void getdata()
Cin>>x;
Cin>>y;
X= -x;
Y= -y;
Void display()
Cout<<”\nx=”<<x<<”\ny=”<<y;
};
Page
31
Int main()
Item x;
x.getdata();
-x;
x.display();
cout<<endl;
system(“pause”);
return 0;
OUTPUT :
X= -15
Y= 30
Page
32
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>
Class employee
Private:
Char name[20];
Int salary;
Public :
Strepy(name,nam);
Salary=sal;
Char* getName(void)
Return name;
Int getSal(void)
Return salary;
Page
33
};
Private:
Char skill[10];
Public:
putData(sal,name);
strepy(skill,skil);
Void display(void)
Cout<<\n\nProgrammer : \n”;
Cout<<”\nName : “<<getName();
Cout<<”\nSalary : “<<getSal();
Cout<<”\nSkill : “<<skill;
};
Private:
Char type[10];
Public:
Page
34
{
putData(sal,name);
strepy(type,typ);
Void display(void)
Cout<<”\nName :”<<getName();
Cout<<”\nsalary :”<<getSal();
Cout<<”\nType :”<<type;
};
Private:
Char pName[10];
Public:
putData(sal,name);
strepy(pName,pNam);
Void display(void)
Clrscr();
Page
35
Analyst al(“ameeca”,8600,”post”);
Programmer p1(“ABC”,12000,”C++”);
Pr1.display();
A1.display();
P1.display();
Getch();
OUTPUT :
Project Leader :
Name : Akshey
Salary : 10000
Analyst :
Name : Ameeca
Salary : 8600
Type : Post
Programmer :
Name : ABC
Salary : 12000
Skill : C++
Page
36
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
Char empname[10];
Int empid;
Public:
Void getemp()
Gets(empname);
Cin>>empid;
Void display()
Cout<<endl<<”Name :”<<empname;
Cout<<endl<<”Id :”<<empid;
};
Page
37
Class qualification
Int exp;
Public:
Void getqual()
Cin>>exp;
Void dispqual()
Cout<<endl<<”Experiece=”<<exp<<”years”;
};
Int projid;
Public:
Void getproject()
Cin>>projid;
Void dispproj()
Cout<<endl<<”PROJECT ID : “<<projid;
Page
38
}
};
Int groupid;
Public:
Void getgroup()
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();
Page
39
s.display();
s.dispqual();
s.dispproj();
cout<<endl<<endl<<endl<<”FOR MANAGER::::”<<endl;
m.getemp();
m.getqual();
m.getgroup();
m.display();
m.dispqual();
m.dispgroup();
getch();
Page
40
OUTPUT :
FOR SCIENTIST :-
Name : Akash
Id: 13012
Experience=6 years
PROJECT ID : 443
FOR MAMAGER::::
Name : Sameera
Id : 1445
Experience= 16 years
Group Id : 5002
Page
41
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;
Cin>>ch;
Switch(ch)
Case 1:
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;
Page
42
Cout<<”\nFile Read : \n”<<string;
Fin.close();
Break;
Default:
Cout<<”INVALID CHOICE”;
OUTPUT :
MENU
1)Write To File
Enter Choice: 1
MEMU
1)Write To File
Enter Choice: 2
Page
43