mpcs ii sem c++lab MANUAL
mpcs ii sem c++lab MANUAL
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
#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;
return 0;
}
Output
int main() {
cout << "\nPrime numbers between " << low << " and " << high <<
" are: " << endl;
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
public:
//member function to get student's details
void getDetails(void);
//member function to print student's details
void putDetails(void);
};
output
Enter total number of students: 2
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
Output
Output
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
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
Output
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
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
Output
Number1: 70
Number2: 50