C-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> 4. Write a C program to find the Square root of a given number #include<stdio.h> 6. Write a C program to find the area and circumference of a circle #include<stdio.h> 7.. Write a C program to demonstrate swapping of two numbers #include<stdio.h> 8. Write Program to demonstrate switch. #include<stdio.h> 9.Write a C program to determine given number is Armstrong number or not. (Sum of
cubes = that number) #include<stdio.h> 10. Program for call by reference #include<stdio.h> void swapnum ( int *var1, int *var2 ) { int tempnum ; tempnum = *var1 ; *var1 = *var2 ; *var2 = tempnum ; } int main( ) { int num1 = 35, num2 = 45 ; printf("Before swapping:"); printf("\nnum1 value is %d", num1); printf("\nnum2 value is %d", num2); /*calling swap function*/ swapnum( &num1, &num2 ); printf("\nAfter swapping:"); printf("\nnum1 value is %d", num1); printf("\nnum2 value is %d", num2); return 0; } 11. Matrix Addition #include<stdio.h> 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 #include _______________________________________________________________________ C++ LAB PROGRAM
|
#include<iostream.h>
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;
}
Type converison
Type converison
#include<iostream.h>
class time
{
private:
int h,m;
public:
time()
{
h=m=0;
}
time(int t)
{
h=t/60;
m=t%60;
}
void get_data()
{
cin>>h>>m;
}
void show_data()
{
cout<<h<<"hrs"<<m<<"mins";
}
operator int()
{
int t=h*60+m;
return t;
}
};
void main()
{
int min;
cout<<"enter the minutes:";
cin>>min;
time t1;
t1.show_data();
cout<<"\n enter the number of hours and minutes:";
t1.get_data();
min=t1;
cout<<"\n total minutes="<<min;
}
class time
{
private:
int h,m;
public:
time()
{
h=m=0;
}
time(int t)
{
h=t/60;
m=t%60;
}
void get_data()
{
cin>>h>>m;
}
void show_data()
{
cout<<h<<"hrs"<<m<<"mins";
}
operator int()
{
int t=h*60+m;
return t;
}
};
void main()
{
int min;
cout<<"enter the minutes:";
cin>>min;
time t1;
t1.show_data();
cout<<"\n enter the number of hours and minutes:";
t1.get_data();
min=t1;
cout<<"\n total minutes="<<min;
}
class to class conversion:
#include<iostream.h>
class rectangle;
class square
private:
int side;
public:
square(int s)
{
side=s;
int get_side()
{
return side;
}
};
class rectangle
{
private:
int length, breadth;
public:
rectangle(int l,int b)
{
length=l;
breadth=b;
}
rectangle(square s)
{
int sqr_side=s.get_side();
length=breadth=sqr_side;
}
void show_data()
{
cout<<"\n length="<<length<<"and breadth="<<breadth;
}
};
void main()
{
rectanlge r(10,20);
r.show_data();
square s(50);
r=s;
r.show_data();
}
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;
}
class rectangle;
class square
private:
int side;
public:
square(int s)
{
side=s;
int get_side()
{
return side;
}
};
class rectangle
{
private:
int length, breadth;
public:
rectangle(int l,int b)
{
length=l;
breadth=b;
}
rectangle(square s)
{
int sqr_side=s.get_side();
length=breadth=sqr_side;
}
void show_data()
{
cout<<"\n length="<<length<<"and breadth="<<breadth;
}
};
void main()
{
rectanlge r(10,20);
r.show_data();
square s(50);
r=s;
r.show_data();
}
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;
}
No comments:
Post a Comment