Computer Practical File C PDF Free
Computer Practical File C PDF Free
PRACTICAL FILE
SUSHANT MONGIA
A2305111007
2-ECE-01-(X)
Program List Date:
1) WAP in C++ to find whether the number entered by the 14/12/2011
user is even or odd.
#include<iostream.h>
#include<conio.h>
void main ()
int i;
cin >> i;
if ( i % 2== 0 )
else
getch();
Output:
Enter an integer
2 is even
2. WAP in C++ to print the table of a number entered by the user.
#include<iostream.h>
#include<conio.h>
void main ()
int a ;
cin >> a ;
cout << a << " x " << i << " = " << i*a << "\n" ;
getch();
Output:
Enter an integer
5 x 1 =5
5 x 2 =10
5 x 3 =15
5 x 4 =20
5 x 5 =25
5 x 6 =30
5 x 7 =35
5 x 8 =40
5 x 9 =45
5 x 10 =50
3. WAP in C++ to find the factorial of a number entered by the user.
#include<iostream.h>
#include<conio.h>
void main ()
int a ;
cin >> a ;
a=a*i;
getch();
Output:
Enter an integer
Factorial = 24
4. WAP in C++ to swap two numbers using call by value.
#include<iostream.h>
#include<conio.h>
int temp;
temp = x;
x = y;
y = temp;
void main ()
int a , b ;
getch(); a = 65
} b = 20
5. WAP in C++ to implement inline function.
#include<iostream.h>
#include<conio.h>
return ( x * y ) ;
return ( p / q ) ;
void main ()
getch();
Output:
121.227898
1.257128
6. WAP in C++ to implement friend function with functions definitions inside the
class.
#include<iostream.h>
#include<conio.h>
class sample
int a;
int b;
public:
};
} Output:
sample x;
x.setvalue();
getch();
}
7. WAP in C++ using friend function to add two complex numbers with functions
definitions outside the class.
#include<iostream.h>
#include<conio.h>
int r , i ;
class complex
int real;
int imag;
public:
void get() ;
} c1 , c2 ;
void add()
r = c1.real + c2.real ;
i = c1.imag + c2.imag ;
void main ()
c1.get();
c2.get();
add();
cout << " \n complex no is " << r << " + i " << i ;
getch();
Output:
complex no is 15 + i 28
8. WAP in C++ using static data member as a counter.
#include<iostream.h>
#include<conio.h>
class item
int number;
public:
number = a ;
count ++ ;
};
; void main ()
item a , b , c ;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
a.getcount();
b.getcount();
c.getcount();
getch();
Output:
count : 0
count : 0
count : 0
count : 3
count : 3
count : 3
9. WAP in C++ to implement array of objects, creating a class employee and
accepting and displaying multiple datasets accepted by the user using array of
objects.
#include<iostream.h>
#include<conio.h>
class employee
char name[30];
float age;
public:
};
void main ()
employee manager[size];
+)
manager[i].getdata();
for(i=0;i<size;i++)
manager[i].putdata();
getch();
Output:
Details of manager1
Enter age : 25
Details of manager2
Details of manager3
Enter age : 32
Manager1
Name : ABC
Age : 25
Manager2
Name : DEF
Age :28
Manager3
Name : GHI
Age : 32
#include<conio.h>
class defa
int a ;
int b ;
public:
defa ( )
};
void main ()
defa D ;
getch();
Output:
#include<conio.h>
class para
int a ;
int b ;
public:
a=x;
b=y;
void disp()
};
void main ()
P.disp(); a = 100
getch(); b = 200
#include<conio.h>
class code
int id ;
public:
code ( )
{}
code ( int a
)
{
id = a ;
id = x.id ;
void
display(void)
{
cout << id ;
};
void main ()
{
code A(100);
code B(A);
code C = A;
code D;
D = A;
getch();
Output:
id of A: 100
id of B: 100
id of C: 100
id of D: 100
#include<conio.h>
class B
int a ;
public:
int b ;
void set_ab();
int get_a();
void show_a();
};
class D : public B
int c ;
public:
void display();
};
void B :: set_ab()
a=5;
b = 10 ;
int B :: get_a()
{
return a ;
void B :: show_a()
void D :: mul ()
c = b * get_a() ;
void D :: display()
void main ()
Dd;
d.set_ab();
d.mul();
d.show_a();
d.display();
d.b = 20 ;
d.mul ();
d.display();
getch();
Output:
a=5
a=5
b = 10
c = 50
a=5
b = 20
c = 100
#include<conio.h>
class student
protected:
int roll_number;
public:
void get_number(int);
void put_number(void);
};
roll_number = a ;
protected:
float sub1;
float sub2;
public:
void get_marks(float,float);
void put_marks(void);
};
sub1 = x;
sub2 = y;
float total ;
public:
void display(void);
};
put_number();
put_marks();
cout << "Total = " << total << "\n" ;
void main ()
result student1;
student1.get_number(111);
student1.get_marks(75.0,59.5);
student1.display();
getch();
Output:
Marks in sub1 = 75
Total = 134.5
#include<conio.h>
class M
protected:
int m ;
public:
void get_m(int);
};
class N
protected:
int n;
public:
void get_n(int);
};
public:
void display(void);
};
void M :: get_m(int x)
m=x;
}
void N :: get_n(int y)
n=y;
void P :: display(void)
void main ()
P p;
p.get_m(10);
p.get_n(20);
p.display();
getch();
Output:
m = 12
n=3
m*n =36
#include<conio.h>
class A
int a,b;
public :
void getdata()
void putdata()
cout << "\n The value of a is : " << a << " and b is " << b;
};
class B : public A
int c , d;
public :
void indata()
void outdata()
cout << "\n The value of c " << c << " and d is " << d;
};
class C: public A
int e,f;
public :
void input()
void output()
cout << " \nthe value of e is " << e << " and f is " << f;
};
void main()
B obj1;
C obj2;
obj1.getdata();
obj1.indata();
obj2.getdata();
obj2.input();
obj1.putdata();
obj1.outdata();
obj2.output();
getch();
Output:
#include<conio.h>
class student
protected:
int roll_number ;
public:
void get_number(int a)
roll_number = a ;
void put_number(void)
};
protected:
public:
part1 = x ; part2 = y ;
}
void put_marks(void)
};
class sports
protected:
float score ;
public:
void get_score(float s)
score = s ;
void put_score(void)
};
{
float total ;
public :
void display(void);
};
put_number();
put_marks();
put_score();
void main()
result student_1;
student_1.get_number(1234);
student_1.get_marks(27.5,33.0);
student_1.get_score(6.0);
student_1.display();
getch();
Output:
Roll no. : 1234
Marks obtained :
part1 = 27.5
part2 = 33
Sports wt: 6
#include<iostream.h>
#include<conio.h>
class student
protected:
int roll_number;
public:
void get_number(int a)
roll_number = a;
void put_number(void)
};
protected:
public:
{
part1 = x; part2 = y;
void put_marks(void)
};
protected:
float score ;
public:
void get_score(float s)
score = s;
void put_score(void)
};
float total;
public:
void display(void)
put_number();
put_marks();
put_score();
};
void main()
result student_1;
student_1.get_number(678);
student_1.get_marks(30.5, 25.5);
student_1.get_score(7.0);
student_1.display();
getch();
Output:
Roll no. : 678
Marks obtained :
part1 = 38.5
part2 = 25.5
Sports wt: 7
Total score: 63
19. Write a program to illustrate application of exception handling.
#include<iostream.h>
#include<conio.h>
void main()
int a,b;
cin >> a;
cin >> b;
int x = a-b;
try
if(x != 0)
getch();
}
20. Write a program to copy the contents of one file to other.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<process.h>
void main()
char ch;
clrscr();
file1.open("file1.txt",ios :: out);
cin.get(ch);
while(ch != '\n')
file1.put(ch);
cin.get(ch);
file1.close();
file1.open("file1.txt",ios :: in);
if(!file1)
{
cout<< "\n File1 not found !! \n";
getch();
exit(0); // aborting…
while(file1)
file1.get(ch);
cout.put(ch);
file2.open("file2.txt",ios :: out );
while(file1)
file1.get(ch);
file2.put(ch);
file1.seekg(0);
file2.close();
file2.open("file1.txt",ios :: in);
if(!file1)
exit(0); // aborting…
while(file2)
file2.get(ch);
cout.put(ch);
file2.close();
getch();
#include<conio.h>
#include<stdlib.h>
void main( )
clrscr();
char line[SIZE];
ofstream fout1,fout2;
fout1.open("country");
fout1<<"India\n";
fout1<<"Japan\n";
fout1<<"Sri Lanka\n";
fout1.close();
fout2.open("capital");
fout2<<"New Delhi\n"<<"Tokyo\n"<<"Colombo\n";
fout2.close();
fin1.open("country");
fin2.open("capital");
fin1.getline(line,80);
cout<<line<<"\n";
fin2.getline(line,80);
cout<<line<<"\n";
fin1.getline(line,80);
cout<<line<<"\n";
fin2.getline(line,80);
cout<<line<<"\n";
fin1.getline(line,80);
cout<<line<<"\n";
fin2.getline(line,80);
cout<<line<<"\n";
fin1.close();
fin2.close();
getch();
Output:
India
New Delhi
Japan
Tokyo
Sri Lanka
Colombo