0% found this document useful (0 votes)
33 views

mpcs ii sem c++lab MANUAL

Uploaded by

sridhar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

mpcs ii sem c++lab MANUAL

Uploaded by

sridhar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

1.

Write a program to
a. print the sum of digits in C++ language,
#include<iostream.h>

int main()
{
int x, s = 0;
cout << "Enter the number : ";
cin >> x;
while (x != 0)
{
s = s + x % 10;
x = x / 10;
}
cout << "\nThe sum of the digits : "<< s;
}

Output
Enter the number : 236214828
The sum of the digits : 36

b. Check the given number is Armstrong or not

#include <iostream.h>

int main()
{
int num, originalNum, remainder, result = 0;
cout << "Enter a number: ";
cin >> num;
originalNum = num;

while (originalNum != 0)
{
// remainder contains the last digit
remainder = originalNum % 10;

result += remainder * remainder * remainder;

// removing last digit from the orignal number


originalNum /= 10;
}
if (result == num)
cout << num << " is an Armstrong number.";
else
cout << num << " is not an Armstrong number.";

return 0;
}
Output

Enter a positive integer: 371


371 is an Armstrong number.

c.Print the prime number from 2 to n is natural


number given
#include <iostream>
using namespace std;

int main() {

int low, high, i;


bool is_prime = true;

cout << "Enter two numbers (intervals): ";


cin >> low >> high;

cout << "\nPrime numbers between " << low << " and " << high <<
" are: " << endl;

while (low < high) {


is_prime = true;

// 0 and 1 are not prime numbers


if (low == 0 || low == 1) {
is_prime = false;
}

for (i = 2; i <= low/2; ++i) {


if (low % i == 0) {
is_prime = false;
break;
}
}

if (is_prime)
cout << low << ", ";

++low;
}

return 0;
}

Output
Enter two numbers (intervals): 0
20
Prime numbers between 0 and 20 are:
2, 3, 5, 7, 11, 13, 17, 19,
2. Write a program to find largest and smallest elements in a given
list of numbers and sort the given list.

#include<iostream>
using namespace std;
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;
}
Output

Enter the size of the array : 5


Enter the elements of the array : 1 2 3 4 5
Largest element : 5
Smallest element : 1

3. write a program to read student name,roll no,marks and display the


same using class and object
#include <iostream.h>
class student
{
private:
char name[30];
int rollNo;
int total;

public:
//member function to get student's details
void getDetails(void);
//member function to print student's details
void putDetails(void);
};

//member function definition, outside of the class


void student::getDetails(void)
{
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter total marks outof 500: ";
cin >> total;
}
/member function definition, outside of the class
void student::putDetails(void)
{
cout << "Student details:\n";
cout<<"Name:"<<name<<",Roll Number:"<<rollNo<<",Total:"<<total;
}
int main()
{
student std[MAX]; //array of objects creation
int n,loop;

cout << "Enter total number of students: ";


cin >> n;
for(loop=0;loop< n; loop++)
{
cout << "Enter details of student " << loop+1 << ":\n";
std[loop].getDetails();
}
cout << endl;
for(loop=0;loop< n; loop++)
{
cout << "Details of student " << (loop+1) << ":\n";
std[loop].putDetails();
}
return 0;
}

output
Enter total number of students: 2

Enter details of student 1:


Enter name: Mike
Enter roll number: 101
Enter total marks outof 500: 456

Enter details of student 2:


Enter name: Mock
Enter roll number: 102
Enter total marks outof 500: 398

Details of student 1:
Student details:
Name:Mike,Roll Number:101,Total:456

Details of student 2:
Student details:
Name:Mock,Roll Number:102,Total:398
4.write a program to find area of rectangle,circle , and square using constructors

#include<iostream>
#include<math.h>
class area
{
float ar;
public:
area(float r)
{
ar=3.14*r*r;
}
area(float l, float b)
{
ar=l*b;
}
area(float a)
{
ar=a*a;
}
void display()
{
cout<<"\n Area : "<<ar;
}
};
int main()
{
int ch;
float x, y, z;
do
{
<<"\n\n 1. Area of Circle";
cout<<"\n 2. Area of Rectangle";
cout<<"\n 3. Area of square";
cout<<"\n 4. Exit";
cout<<"\n\n Enter Your Choice : ";
cin>>ch;

switch(ch)
{
case 1 :
{
cout<<"\n Enter Radius of the Circle : ";
cin>>x;
area a1(x); //Class area, object is created : a1
a1.display();
}
break;

case 2 :
{
cout<<"\n Enter Length and Breadth of the Rectangle : ";
cin>>x>>y;
area a2(x,y);
a2.display();
}
break;

case 3 :
{
cout<<"\n Enter square area : ";
cin>>x;
area a3(x);
a3.display();
}
break;

case 4 :
exit(0);

default :
cout<<"\n\n Invalid Choice ...";
}
} while(ch!=4);
return 0;
}

Output:
1.Area of circle
2. Area of rectangle
3. Area of square
4.exit
Enter your choice
2
Enter sides of the rectangle
5 5
Area of rectangle is : 25
5. Program to demonstrate on Copy constructor.

#include<iostream.h>
#include<conio.h>
class copy
{
int var,fact;
public:
copy(int temp)
{
var=temp;
}
double calculate()
{
fact=1;
for(int i=1;i<=var;i++)
{
fact=fact*i;;
}
return fact;
}
};
void main()
{
int n;
clrscr();
cout<<"\n\t Enter the number";
cin>>n;
copy obj(n);
copy cpy=obj;
cout<<"\n \t"<<n<<"\t factorial is"<<obj.calculate();
cout<<"\n \t"<<n<<"\t factorial is"<<cpy.calculate();
getch();
}

Output

Enter the number : 5


5 factorial is : 120
5 factorial is : 120
6. Program to demonstrate Friend function.
Program:
#include<iostream.h>
#include<conio.h>
class base
{
int var1,var2;
public:
void get()
{
cout<<"enter two values";
cin>>var1>>var2;
}
friend float mean(base ob);
};
float mean(base ob)
{
return float(ob.var1+ob.var2)/2;
}
void main()
{
base obj;
clrscr();
obj.get();
cout<<"\n\t Mean value is"<<mean(obj);
getch();
}

Output

Enter two values: 10 20


Mean value is : 15

7. Program for Unary operator overloading.


Program:
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b,c;
public:
complex()
{
}
void getvalue()
{
cout<<"enter two values";
cin>>a>>b;
}
void operator++()
{
a=++a;
b=++b;
}
void operator--()
{
a=--a;
b=--b;
}
void display()
{
cout<<a<<"+\t"<<b<<"i"<<endl;
}
};
void main()
{
complex obj;
clrscr();
obj.getvalue();
obj++;
cout<<"Increament complex number \n";
obj.display();
obj--;
cout<<"decreament complex number \n";
obj.display();
getch();
}

Output

Enter the two numbers: 3 6


Increament complex number:
4+ 7i
Decrement complex number:
3+ 6i

8. Program for Binary operator overloading.

Program:
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
void getvalue()
{
cout<<"enter the value of complex Numbers a,b";
cin>>a>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
}
void display()
{
cout<<a<<"+\t"<<b<<"i"<<endl;
}
};
void main()
{
complex obj1,obj2,result,result1;
clrscr();
obj1.getvalue();
obj2.getvalue();
result=obj1+obj2;
result1=obj1-obj2;
cout<<"input values:\n";
obj1.display();
obj2.display();
cout<<"Result:\n";
result.display();
result1.display();
getch();
}

Output

Enter the value of complex numbers a ,b 4 5


Enter the value of complex numbers a, b 2 2
Input values:
4 + 5i
2 + 2i
Result:
6 + 7i
2 + 3i

9. Program for Single and Multiple Inheritance.

// Using Single Inheritance


// Program to find out the payroll system using single inheritance
#include<iostream.h>
#include<conio.h>
class emp
{
public:
char name[30],des[10];
int eno;
void get()
{
cout<<"enter the employee number:";
cin>>eno;
cout<<"enter employee name:";
cin>>name;
cout<<"enter the designation";
cin>>des;
}
};
class salary:public emp
{
float bp,hra,da,pf,np;
public:
void get1()
{
cout<<"enter the basic pay";
cin>>bp;
cout<<"enter the hra";
cin>>hra;
cout<<"enter the da";
cin>>da;
cout<<"enter the pf";
cin>>pf;
}
void calculate()
{
np=bp+hra+da-pf;
}
void display()
{
cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\
t"<<pf<<"\t"<<np;
}
};
void main()
{
int i,n;
char ch;
salary s[10];
clrscr();
cout<<"Enter the number of employee";
cin>>n;
for(i=0;i<n;i++)
{
s[i].get();
s[i].get1();
s[i].calculate();
}
cout<<"\n eno\t ename \t des \t bp \t hra \t da \t pf \t np \n";
for(i=0;i<n;i++)
{
s[i].display();
}
getch();
}

Output
Enter the number of employee : 1
Enter the employee no;150
Enter the employee name ; ram
Enter the designation: Manager
Enter the basic pay: 5000
Enter the hra: 1000
Enter the da: 500
Enter the pf: 300

Eno ename des bp hra da pf np


150 ram Manager 5000 1000 500 300 6200
10. Multiple Inheritance
// To find out the student details using Multiple Inheritance
Program:
#include<iostream.h>
#include<conio.h>
class Student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"enter the Roll number:";
cin>>rno;
cout<<"enter the two subject marks:";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm;
public:
void getsm()
{
cout<<"\n Enter the sports marks";
cin>>sm;
}
};
class statement:public Student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"Rollno:"<<rno<<"\n\tTotal :"<<tot<<"\n\t Average :"<<avg;
}
};
void main()
{
statement obj;
clrscr();
obj.get();
obj.getsm();
obj.display();
getch();
}

Output

Enter the Roll number: 100


Enter two marks: 90
80
Enter the sport marks: 90
Roll no : 100
Total : 260
Average; 86

11. Program to demonstrate pure virtual function implementation.

Program:
#include<iostream.h>
#include<conio.h>
class base
{
private:
int x;
float y;
public:
virtual void getdata();
virtual void display();
};
class der:public base
{
private:
int roll;
char name[20];
public:
void getdata();
void display();
};
void base::getdata()
{
}
void base::display()
{
}
void der::getdata()
{
cout<<"Enter roll of the student";
cin>>roll;
cout<<"Enter name of the student";
cin>>name;
}
void der::display()
{
cout<<"Name is:"<<name;
cout<<"\n Roll no is"<<roll;
}
void main()
{
base *ptr;
der obj;
clrscr();
ptr=&obj;
ptr->getdata();
ptr->display();
getch();
}

Output

Enter the roll no of the student; 111


Enter the name of the student: sachin
Name is; sachin
Roll no is: 111
12. Program to demonstrate call by value, call by address and call by reference.

Program:
// Using call by value.
#include<iostream.h>
#include<conio.h>
void interchange(int number1,int number2);
void main()
{
int num1=50,num2=70;
clrscr();
interchange(num1,num2);
cout<<"\n Number1:"<<num1<<"\n Number2:"<<num2;
getch();
}
void interchange(int number1,int number2)
{
int temp;
temp=number1;
number1=number2;
number2=temp;
}

Output

Number1=50
Number2=70

// Using call by reference


Program:
#include<iostream.h>
#include<conio.h>
void interchange(int *num1,int *num2);
void main()
{
int num1=50,num2=70;
clrscr();
interchange(&num1,&num2);
cout<<"\n Number1:"<<num1<<"\n Number2:"<<num2;
getch();
}
void interchange(int *num1,int *num2)
{
int temp;
temp=*num1;
*num1=*num2;
*num2=temp;
}

Output

Number1: 70
Number2: 50

You might also like