Practical 1: Aim: A) Write A Program To Print "Hello World". Code
Practical 1: Aim: A) Write A Program To Print "Hello World". Code
Code:
#include <stdio.h>
int main() {
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
n /= 10;
return 0;
Output:
Aim: g) Write a program to print a pattern.
Code:
#include<stdio.h>
int main()
{
int a, b, c, i =1;
c = 5;
for(a=1;a<=c;a++)
{
for (b=1;b<=a;b++)
printf("%3d" ,i++);
printf("\n");
}
return 0;
}
Output:
Practical 2
Aim: a) Using class.
Code:
#include<iostream>
#include<cstring>
using namespace std;
class car
{
string Brake[20];
int speed;
public:
void slowdown( string Brake)
{
cout << " Apply Brake!" << endl;
}
void slowdown( int speed)
{
cout << "Slowdown" << endl;
}
};
int main(){
car obj;
obj.slowdown(24);
obj.slowdown(23);
return 0;
}
Output:
Aim: b) Function Overload.
Code:
#include<iostream>
using namespace std;
class sum
{
public:
int add(int num1,int num2)
{
return num1+num2;
}
int add(int num1, int num2, int num3){
return num1+num2+num3;
}
};
int main(void)
{
sum obj;
cout << obj.add(10,20,30) << endl;
cout << obj.add(25,25) << endl;
return 0;
}
Output:
Aim: c) Perimeter using Function Overload.
Code:
#include<iostream>
#include<cmath>
using namespace std;
class sum{
public:
int perimeter(int length){
return 4*length;;
}
int perimeter(int length , int breadth ){
return 2*length+2*breadth;
}
int perimeter(double radius){
return 2*M_PI*radius;
}
};
int main(void){
sum obj;
cout << obj.perimeter(23) << endl;
cout << obj.perimeter(20,40) << endl;
cout << obj.perimeter(4.5) << endl;
return 0;
}
Output:
Aim: d) Static Function.
Code:
#include<iostream>
using namespace std;
class Box
{
public:
static int objectCount;
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout << "Constrcutor called:" << endl;
length = l;
breadth = b;
height = h;
objectCount ++;
}
double volume ()
{
return length*breadth*height;
}
static int getCount()
{
return objectCount;
}
private:
double length;
double breadth;
double height;
};
int Box::objectCount =0;
int main(void)
{
cout << "Initial stage count:" << Box:: getCount() << endl;
Box Box1(3.2,3.4,4.5); // box1
Box Box2(2.5,3.5,4.5); //box2
cout << " Final stage count:" << Box:: getCount() << endl;
return 0;
}
Output:
Practical 3
Aim: a) Pass value by reference.
Code:
#include<iostream>
#include<conio.h>
using namespace std;
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
cout << " After swapping numbers \n";
cout << "a=" << a << endl;
cout << " b= " << b;
}
int main()
{
int x, y;
cout << " Enter two numbers" << endl;
cin >> x >> y;
cout << "Before Swapping number:" << endl;
cout << "a =" << x << endl;
cout << " b=" << y << endl;
swap(x,y);
getch();
return 0;
}
Output:
Aim: b) Single Inheritance.
Code:
#include<iostream>
#include<conio.h>
using namespace std;
class Employee
{
public:
int cid;
char name[20];
int age;
public:
void inputdata(int e, int a)
{
cid = e;
cout << "Enter name:" << endl;
cin >> name ;
age = a;
}
void display()
{
cout << "ID:" << cid <<endl;
cout << "Name:" << name << endl;
cout << "Age:" << age << endl;
}
};
class Fulltimestaff : public Employee
{
public:
int da;
char hra;
int salary;
public:
void enterdata(int d, char h, int sal)
{
da = d;
hra = h;
salary = sal;
}
void show()
{
cout << "Daily Allowance=" << da << endl;
cout << "HRA=" << hra << endl;
cout << "Salary=" << salary << endl;
}
};
int main()
{
Fulltimestaff f1;
f1.inputdata(30,23);
f1.display();
f1.enterdata(2500,'CE', 103000);
f1.show();
getch();
}
Output:
Aim: c) Multiple Heritance.
Code:
#include<iostream>
using namespace std;
class A
{
public:
int a=5;
A(){
cout << "Constuctor for class A"<< endl;
}
};
class B{
public:
int b=10;
B(){
cout << "Constructor for class B:" << endl;
}
};
class C: public A, public B{
public:
int c =20;
C()
{
cout << "Constructor of class C:" << endl;
cout << "Class C Inherits from class A and class B: " << endl;
}
};
int main(){
C obj;
cout << "a=" << obj.a << endl;
cout << "b=" << obj.b << endl;
cout << "c=" << obj.c << endl;
return 0;
}
Output:
binary::binary(int x)
{
a=x;
}
void binary::show()
{
cout<<a<<"\t";
}
binary binary::operator+(binary s)
{
s.a=a+s.a;
return s;
}
binary binary::operator-(binary s)
{
s.a=a-s.a;
return s;
}
binary binary::operator*(binary s)
{
s.a=a*s.a;
return s;
}
binary binary::operator/(binary s)
{
s.a=a/s.a;
return s;
}
int main()
{
binary ob1(100),ob2(20),ob3,ob4,ob5,ob6;
cout<<"\nThe first variable is:";
ob1.show();
cout<<"\nThe second variable is:";
ob2.show();
cout<<"\n\nOperations:";
cout<<"\n\nAddition:";
ob3=ob1+ob2;
ob3.show();
cout<<"\nSubtraction:";
ob4=ob1-ob2;
ob4.show();
cout<<"\nMultiplication:";
ob5=ob1*ob2;
ob5.show();
cout<< "\nDivision:";
ob6=ob1/ob2;
ob6.show();
return 0;
}
Output: