C-Language Lab programs
1. Program for Arithmetic operator
#include<stdio.h>
2. Program for Armstrong Number #include<stdio.h> 3. Program For Largest and smallest element in an array #include<stdio.h> big=a[0]; for(i=1;i<size;i++) { if(big<a[i]) big=a[i]; } printf("Largest element: %d",big); small=a[0]; for(i=1;i<size;i++) { if(small>a[i]) small=a[i]; } printf("Smallest element: %d",small); return 0; } 4. Write a C program to find the Square root of a given number #include<stdio.h> 5. Program for call by value #include<stdio.h> 6. Write a C program to find the area and circumference of a circle #include<stdio.h> { int m, n, c, d, first[10][10], second[10][10], sum[10][10]; printf("Enter the number of rows and columns of matrix\n"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n"); for (c = 0; c < m; c++) for (d = 0; d < n; d++) scanf("%d", &first[c][d]); printf("Enter the elements of second matrix\n"); for (c = 0; c < m; c++) for (d = 0 ; d < n; d++) scanf("%d", &second[c][d]); printf("Sum of entered matrices:-\n"); for (c = 0; c < m; c++) { for (d = 0 ; d < n; d++) { sum[c][d] = first[c][d] + second[c][d]; printf("%d\t", sum[c][d]); } printf("\n"); } } 12. Call by reference #include<stdio.h> #include<conio.h> C++ LAB PROGRAMS
|
void main()
{
int val, num,
sum = 0;
cout <<
"Enter the number : ";
cin >>
val;
num = val;
while (num !=
0)
{
sum = sum +
num % 10;
num = num /
10;
}
cout <<
"The sum of the digits of " << val << " is "
<< sum;
}
2.
Check whether
the given number is Armstrong or not
|
#include
<iostream>
void
main() {
int num, sum = 0, digit;
cout<<"Enter a positive integer:
";
cin>>num;
for(int temp=num; temp!=0;){
digit = temp % 10;
sum = sum +(digit * digit * digit);
temp = temp/10;
}
if(sum == num)
cout<<num<<" is an
Armstrong number.";
else
cout<<num<<" is not an
Armstrong number.";
}
3.
Write a program
to find largest and smallest elements in a given list of numbers and sort the
|
given list.
|
#include<iostream.h>
int main ()
{
int
arr[10], n, i, max, min;
cout
<< "Enter the size of the array : ";
cin
>> n;
cout
<< "Enter the elements of the array : ";
for (i = 0;
i < n; i++)
cin
>> arr[i];
max =
arr[0];
for (i = 0;
i < n; i++)
{
if (max
< arr[i])
max
= arr[i];
}
min =
arr[0];
for (i = 0;
i < n; i++)
{
if (min
> arr[i])
min
= arr[i];
}
cout
<< "Largest element : " << max;
cout
<< "Smallest element : " << min;
return 0;
}
4..Write a program to read
the student name, roll no, marks and display the same using class
|
and object.
|
#include<iostream.h>
class student
{
private:
char
name[20],regd[10],branch[10];
int
sem;
public:
void
input();
void
display();
};
void student::input()
{
cout<<"Enter
Name:";
cin>>name;
cout<<"Enter
Regdno.:";
cin>>regd;
cout<<"Enter
Branch:";
cin>>branch;
cout<<"Enter
Sem:";
cin>>sem;
}
void student::display()
{
cout<<"\nName:"<<name;
cout<<"\nRegdno.:"<<regd;
cout<<"\nBranch:"<<branch;
cout<<"\nSem:"<<sem;
}
int main()
{
student
s;
s.input();
s.display();
}
5. Write a program to find area
of a rectangle, circle, and square using constructors.
|
#include<iostream.h>
class Area
{
private:
int a,b;
float c;
public:
void triangle()
{
cout <<"area of triangle is" ;
c=0.5*a*b;
cout<<c
<<endl;
}
void
rectangle()
{
cout<<"area of rectangle is "<<a*b<<endl;
}
void circle()
{cout<<"area of circle is
";
c=3.14*a*a;
cout<<c
<<endl;
}
Area(int x,int y)
{cout<<"constructor called "<<endl;
a=x;
b=y;
}
Area(int x)
{
cout<<"constructor called "<<endl;
a=x;
}
};
int main()
{Area
obj(4,6);//1st call implicit call
Area
obj1=Area(2,3);//2nd call Explicit
Area
obj2=8;//if one argument can pass
argument directly
obj.triangle();
obj1.rectangle();
obj2.circle();
return 0;
}
6. Construtor overloading
#include <iostream>
class Area
{
private:
int length;
int breadth;
public:
//
Constructor with no arguments
Area():
length(5), breadth(2) { }
//
Constructor with two arguments
Area(int l,
int b): length(l), breadth(b){ }
void GetLength()
{
cout
<< "Enter length and breadth respectively: ";
cin
>> length >> breadth;
}
int
AreaCalculation() { return length *
breadth; }
void
DisplayArea(int temp)
{
cout
<< "Area: " << temp << endl;
}
};
int main()
{
Area A1, A2(2,
1);
int temp;
cout <<
"Default Area when no argument is passed." << endl;
temp =
A1.AreaCalculation();
A1.DisplayArea(temp);
cout <<
"Area when (2,1) is passed as argument." << endl;
temp =
A2.AreaCalculation();
A2.DisplayArea(temp);
return 0;
}
7. Operator overloading
#include<iostream.h>
class space
{
int x;
int y;
int z;
public:
void getdata(int a, int b, int c);
void display(void);
void operator-();
};
void space::getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void space::display(void)
{
cout<<"="<<x<<"";
cout<<"y="<<y<<"";
cout<<"z="<<z<<"\n";
}
void space::operator-()
{
x=-x;
y=-y;
z=-z;
}
int main()
{
space s;
s.getdata(10,-20,30);
cout<<"s";
s.display();
-s;
cout<<"-s";
s.display();
return 0;
}
8. Overloading binary operator
#include<iostream.h>
class complex
{
float x;
float y;
public:
complex()
{
}
complex(float real,float imag)
{
x=real;
y=imag;
}
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.y;
temp.y=y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<y"+j"<<y<<"\n";
}
int main()
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1"=";
cout<<"c2=";
cout<<"c3=";
return 0;
}
9. Copy Construtor
#include<iostream.h>
class code
{
int id;
public:
code()
{
}
code(int a)
{
id=a;
}
code(code &x)
{
id=x.id;
}
void display(void)
{
cout<<id;
}
};
int main()
{
code A(100);
code B(A);
code C=A;
code D;
D=A;
cout<<"\n id of A:";
A.display();
cout<<"\n id of B:";
B.display();
cout<<"\n id of C:";
C.display();
cout<<"\n id of D:";
D.display();
return 0;
}
13. Single Inheritance:
#include <iostream.h>
Class B
{
int a;
public:
int b;
void get_ab();
int get_a();
void show_a();
};
Class D: public B
{
int c;
public:
void mul();
void display();
};
Void B :: get_ab()
{ a=5;b=10; }
Int B :: get_a()
{ return a;}
Void B :: show_a()
{ count<< “a=”<<a<< “\n" ;
}
Void D :: mul()
{ c=b*get_a();}
Void D :: display()
{
Count<< “a=”<<get_a();
Count<< “b=”<<b;
Count<< “c=”<<c;
}
int main()
{
D d;
d.get_ab();
d.mul();
d.show_a();
d.display();
d.b=20;
d.mul();
d.display();
return 0;
}
14. Multiple inheritance
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm; // sm = Sports mark
public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}
15. Factorial of number using classes
#include<iostream.h>
#include<conio.h>
class factorial{
int f, n;
public:
void fact();
void display();
};
void factorial::fact()
{
f=1;
cout<<"\nEnter a Number:";
cin>>n;
for(int i=1;i<=n;i++)
f=f*i;
}
void factorial::display()
{
cout<<"\nFactorial of "<<n<<" is "<<f;
}
void main()
{
clrscr();
factorial ob;
ob.fact();
ob.display();
getch();
}
16.Addition of two matrices
20. Arrays of objects
#include<iostream.h>
#include<conio.h>
class Employee
{
int Id;
char Name[25];
int Age;
long Salary;
public:
void GetData() //Statement 1 : Defining GetData()
{
cout<<"\n\tEnter Employee Id : ";
cin>>Id;
cout<<"\n\tEnter Employee Name : ";
cin>>Name;
cout<<"\n\tEnter Employee Age : ";
cin>>Age;
cout<<"\n\tEnter Employee Salary : ";
cin>>Salary;
}
void PutData() //Statement 2 : Defining PutData()
{
cout<<"\n"<<Id<<"\t"<<Name<<"\t"<<Age<<"\t"<<Salary;
}
};
void main()
{
int i;
Employee E[3]; //Statement 3 : Creating Array of 3 Employees
for(i=0;i<3;i++)
{
cout<<"\nEnter details of "<<i+1<<" Employee";
E[i].GetData();
}
cout<<"\nDetails of Employees";
for(i=0;i<3;i++)
E[i].PutData();
}
21. Abstract Class using Pointer Variable
#include<iostream.h>
class database
{
public:
virtual void getname()=0;
};
class account:public databse
{
public:
void getname()
{
cout<<"this is account class"<<endl;
}
};
class manager:public database
{
public:
void getname()
{
cout<<"this is manager class"<<endl;'
}
};
class customer:public databse
{
public:
void getname()
{
cout<<"this is customer class"<<endl;
}
};
void main()
{
database *ptr;
manager m;
ptr=&m;
ptr->getname();
account a;
ptr=&a;
ptr->getname();
customer c;
ptr=&c;
ptr->getname();
}
Giving new implementation of derived class method into base class and the calling of this new implemented function with base class's object is done by making base class function as virtual function.
Virtual function is used in situation, when we need to invoke derived class function using base class pointer. We must declare base class function as virtual using virtual keyword preceding its normal declaration. The base class object must be of pointer type so that we can dynamically replace the address of base class function with derived class function. This is how we can achieve "Runtime Polymorphism".
If we doesn't use virtual keyword in base class, base class pointer will always execute function defined in base class.
#include<iostream.h>
class code
{
int id;
public:
code()
{
}
code(int a)
{
id=a;
}
code(code &x)
{
id=x.id;
}
void display(void)
{
cout<<id;
}
};
int main()
{
code A(100);
code B(A);
code C=A;
code D;
D=A;
cout<<"\n id of A:";
A.display();
cout<<"\n id of B:";
B.display();
cout<<"\n id of C:";
C.display();
cout<<"\n id of D:";
D.display();
return 0;
}
10. Constructor & Destrctor
#include<iostream.h>
class addition
{
int a,b;
public:
addition(int,int);
~addition(();
int add()
{
return (a+b);
}
};
addition::addition(int x,int y)
{
a=x;
b=y;
}
addition::~addition()
{
cout<<"memory deallocation\n";
}
int main()
{
addition obj(3,4);
cout<<"sum
is<<obj.add()<<"\n";
return 0;
}
11. Default constructor
#include<iostream.h>
class subtraction
{
public:
int a,b;
int sub(int a,int b)
{
return (a-b)
}
subtraction();
};
subtraction::subtraction()
{
}
int main()
{
int x;
subtraction s;
x=s.sub(7,4);
cout<<"difference
is"<<x<<"\n";
return 0;
}
12. Dynamic Constructor:
#include <conio.h>
class dyncons
{
int * p;
public:
dyncons()
{
p=new int;
*p=10;
}
dyncons(int v)
{
p=new int;
*p=v;
}
int dis()
{ return(*p);
}
};
void main()
{
clrscr();
dyncons o, o1(9);
cout<<"The value of object o's p is:";
cout<<o.dis();
cout<<"\nThe value of object 01's p is:"<<o1.dis();
getch();
}
- Dynamic constructor is used to allocate the memory to the objects at the run time.
- Memory is allocated at run time with the help of 'new' operator.
- By using this constructor, we can dynamically initialize the objects.
#include <conio.h>
class dyncons
{
int * p;
public:
dyncons()
{
p=new int;
*p=10;
}
dyncons(int v)
{
p=new int;
*p=v;
}
int dis()
{ return(*p);
}
};
void main()
{
clrscr();
dyncons o, o1(9);
cout<<"The value of object o's p is:";
cout<<o.dis();
cout<<"\nThe value of object 01's p is:"<<o1.dis();
getch();
}
13. Single Inheritance:
#include <iostream.h>
Class B
{
int a;
public:
int b;
void get_ab();
int get_a();
void show_a();
};
Class D: public B
{
int c;
public:
void mul();
void display();
};
Void B :: get_ab()
{ a=5;b=10; }
Int B :: get_a()
{ return a;}
Void B :: show_a()
{ count<< “a=”<<a<< “\n" ;
}
Void D :: mul()
{ c=b*get_a();}
Void D :: display()
{
Count<< “a=”<<get_a();
Count<< “b=”<<b;
Count<< “c=”<<c;
}
int main()
{
D d;
d.get_ab();
d.mul();
d.show_a();
d.display();
d.b=20;
d.mul();
d.display();
return 0;
}
14. Multiple inheritance
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm; // sm = Sports mark
public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}
15. Factorial of number using classes
#include<iostream.h>
#include<conio.h>
class factorial{
int f, n;
public:
void fact();
void display();
};
void factorial::fact()
{
f=1;
cout<<"\nEnter a Number:";
cin>>n;
for(int i=1;i<=n;i++)
f=f*i;
}
void factorial::display()
{
cout<<"\nFactorial of "<<n<<" is "<<f;
}
void main()
{
clrscr();
factorial ob;
ob.fact();
ob.display();
getch();
}
16.Addition of two matrices
#include<iostream.h>
main()
{
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;
}
return 0;
}
17. Type Conversion :
#include <iostream.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x + y;
// x is implicitly converted to float
float z = x + 1.0;
cout << "x = " << x << endl
<< "y = " << y << endl
<< "z = " << z << endl;
return 0;
}
18.Class and object
#include <iostream>
class CRectangle {
int x, y;
public:
void set_values (int,int);
int area () {return (x*y);}
};
void CRectangle::set_values (int a, int b) {
x = a;
y = b;
}
int main () {
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}
19.Multipath Inheritance:
#include<iostream.h>
#include<conio.h>
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;
};
void main()
{
ClassD obj;
//obj.a = 10; //Statement 1, Error occur
//obj.a = 100; //Statement 2, Error occur
obj.ClassB::a = 10; //Statement 3
obj.ClassC::a = 100; //Statement 4
obj.b = 20;
obj.c = 30;
obj.d = 40;
cout<< "\n A from ClassB : "<< obj.ClassB::a;
cout<< "\n A from ClassC : "<< obj.ClassC::a;
cout<< "\n B : "<< obj.b;
cout<< "\n C : "<< obj.c;
cout<< "\n D : "<< obj.d;
}
main()
{
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;
}
return 0;
}
17. Type Conversion :
#include <iostream.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x + y;
// x is implicitly converted to float
float z = x + 1.0;
cout << "x = " << x << endl
<< "y = " << y << endl
<< "z = " << z << endl;
return 0;
}
18.Class and object
#include <iostream>
class CRectangle {
int x, y;
public:
void set_values (int,int);
int area () {return (x*y);}
};
void CRectangle::set_values (int a, int b) {
x = a;
y = b;
}
int main () {
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}
19.Multipath Inheritance:
#include<iostream.h>
#include<conio.h>
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;
};
void main()
{
ClassD obj;
//obj.a = 10; //Statement 1, Error occur
//obj.a = 100; //Statement 2, Error occur
obj.ClassB::a = 10; //Statement 3
obj.ClassC::a = 100; //Statement 4
obj.b = 20;
obj.c = 30;
obj.d = 40;
cout<< "\n A from ClassB : "<< obj.ClassB::a;
cout<< "\n A from ClassC : "<< obj.ClassC::a;
cout<< "\n B : "<< obj.b;
cout<< "\n C : "<< obj.c;
cout<< "\n D : "<< obj.d;
}
20. Arrays of objects
#include<iostream.h>
#include<conio.h>
class Employee
{
int Id;
char Name[25];
int Age;
long Salary;
public:
void GetData() //Statement 1 : Defining GetData()
{
cout<<"\n\tEnter Employee Id : ";
cin>>Id;
cout<<"\n\tEnter Employee Name : ";
cin>>Name;
cout<<"\n\tEnter Employee Age : ";
cin>>Age;
cout<<"\n\tEnter Employee Salary : ";
cin>>Salary;
}
void PutData() //Statement 2 : Defining PutData()
{
cout<<"\n"<<Id<<"\t"<<Name<<"\t"<<Age<<"\t"<<Salary;
}
};
void main()
{
int i;
Employee E[3]; //Statement 3 : Creating Array of 3 Employees
for(i=0;i<3;i++)
{
cout<<"\nEnter details of "<<i+1<<" Employee";
E[i].GetData();
}
cout<<"\nDetails of Employees";
for(i=0;i<3;i++)
E[i].PutData();
}
21. Abstract Class using Pointer Variable
#include<iostream.h>
class database
{
public:
virtual void getname()=0;
};
class account:public databse
{
public:
void getname()
{
cout<<"this is account class"<<endl;
}
};
class manager:public database
{
public:
void getname()
{
cout<<"this is manager class"<<endl;'
}
};
class customer:public databse
{
public:
void getname()
{
cout<<"this is customer class"<<endl;
}
};
void main()
{
database *ptr;
manager m;
ptr=&m;
ptr->getname();
account a;
ptr=&a;
ptr->getname();
customer c;
ptr=&c;
ptr->getname();
}
C++ Virtual Function
Giving new implementation of base class method into derived class and the calling of this new implemented function with derived class's object is called function overriding.Giving new implementation of derived class method into base class and the calling of this new implemented function with base class's object is done by making base class function as virtual function.
Virtual function is used in situation, when we need to invoke derived class function using base class pointer. We must declare base class function as virtual using virtual keyword preceding its normal declaration. The base class object must be of pointer type so that we can dynamically replace the address of base class function with derived class function. This is how we can achieve "Runtime Polymorphism".
If we doesn't use virtual keyword in base class, base class pointer will always execute function defined in base class.
Example of virtual function
#include<iostream.h>
#include<conio.h>
class BaseClass
{
public:
virtual void Display()
{
cout<<"\n\tThis is Display() method of Base Class";
}
void Show()
{
cout<<"\n\tThis is Show() method of Base Class";
}
};
class DerivedClass : public BaseClass
{
public:
void Display()
{
cout<<"\n\tThis is Display() method of Derived Class";
}
void Show()
{
cout<<"\n\tThis is Show() method of Derived Class";
}
};
void main()
{
DerivedClass D;
BaseClass *B; //Creating Base Class Pointer
B = new BaseClass;
B->Display(); //This will invoke Display() method of Base Class
B->Show(); //This will invoke Show() method of Base Class
B=&D;
B->Display(); //This will invoke Display() method of Derived Class
//bcoz Display() method is virtual in Base Class
B->Show(); //This will invoke Show() method of Base Class
//bcoz Show() method is not virtual in Base Class
}
Call by value:
#include<iostream.h>
#include<conio.h>
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int a = 100, b = 200;
clrscr();
swap(a, b); // passing value to function
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
getch();
return 0;
}
As you can see in the output, although we swapped the values of variable
a
and b
inside the function swap()
still in the main()
method those changes are not
passed on.
#include<iostream.h>
#include<conio.h>
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int a = 100, b = 200;
clrscr();
swap(a, b); // passing value to function
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
getch();
return 0;
}
call by value | call by reference | |
---|---|---|
1 | This method copy original value into function as a arguments. | This method copy address of arguments into function as a arguments. |
2 | Changes made to the parameter inside the function have no effect on the argument. | Changes made to the parameter affect the argument. Because address is used to access the actual argument. |
3 | Actual and formal arguments will be created in different memory location | Actual and formal arguments will be created in same memory location |
No comments:
Post a Comment