Practical File
Practical File
Program :
#include <iostream>
using namespace std;
int main() {
int a,b,c;
a=10;
b=25;
c=a+b;
cout<< "Sum is : "<< c;
return 0;
}
Output:
Q2.Write a program that reads a floating point number and then prints
integer part and fractional part.
Program :
#include <iostream>
using namespace std;
int main() {
float x;
int a[5],b[5];
cout<<"Enter the number : ";
cin>>x;
int y=x;
float z=(x-y);
cout<<"The integer part is : "<< y ;
cout<<" \nThe fractional part is : "<< z;
return 0;
}
Output :
Output :
Output :
Q6.Write a program to make a class and enter data by the user.
Program :
#include <iostream>
using namespace std;
int n=1e5;
class Employee{
private:
string name;
int age;
int phone_number;
int id;
public:
void getdata(){
cout<<"Enter name:";
cin>>name;
cout<<"Enter age:";
cin>>age;
cout<<"Enter phone number:";
cin>>phone_number;
cout<<"Enter ID:";
cin>>id;
}
void showdata(){
cout<<"Name:"<<name<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Phone Number:"<<phone_number<<endl;
cout<<"ID:"<<id<<endl; } };
int main() {
//1.Reference variable
cout<<endl<<"-----------------TUSHAR-----------------------"<<endl;
cout<<"Program for reference variable"<<endl;
int x=23,z=34;
int &y=x;
cout<<"x:"<<x<<",y:"<<y<<",z:"<<z<<endl;
y=z;
cout<<"x:"<<x<<",y:"<<y<<",z:"<<z<<endl;
//Program for global variables and local variables
cout<<endl<<"-----------------TUSHAR-----------------------"<<endl;
int n=52;
cout<<"Local variable:"<<n<<",Global variable:"<<::n<<endl;
//Program for showing use of pre/post increment adn decrement operators;
cout<<endl<<"-----------------TUSHAR-----------------------"<<endl;
cout<<"Program for showing use of pre/post increment adn decrement operators"<<endl;
int i=5;
//Post increment;
cout<<"Post-increment of i:"<<i++<<endl;
//Pre increment;
cout<<"Pre-increment of i:"<<++i<<endl;
//Post decrement;
cout<<"Post-decrement of i:"<<i--<<endl;
//Pre decrement;
cout<<"Pre-decrement of i:"<<--i<<endl;
//class
cout<<endl<<"-----------------TUSHAR-----------------------"<<endl;
cout<<"Program for making a class emp wiht age,name,id,phone number as data members
and showdata() and getdata() as member functions"<<endl;
Employee emp1,emp2;
emp1.getdata();
emp1.showdata();
emp2.getdata();
emp2.showdata(); }
Output :
Q7.WAP which perform addition of two matrices using oops.
Program :
#include <iostream>
using namespace std;
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout << "Enter number of rows : ";
cin >> r;
cout << "Enter number of columns : ";
cin >> c;
cout << endl << "Enter elements of 1st matrix: " << endl;
// Storing elements of first matrix entered by user.
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{ cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j]; }
// Storing elements of second matrix entered by user.
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{ cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j]; }
// Adding Two matrices
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
// Displaying the resultant sum matrix.
cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{ cout << sum[i][j] << " ";
if(j == c - 1)
cout << endl; }
cout<<endl<<"-----------------TUSHAR-----------------------"<<endl;
cout<<endl<<"-----------------07417702021-------------"<<endl;
return 0; }
Output :
Q8.WAP to demonstrate member function inside the class and outside
the class.
Program :
#include <iostream>
using namespace std;
class car
{ private:
int car_number;
char car_model[10];
public:
void getdata(); //function declaration
void showdata(); };
// function definition
void car::getdata()
{ cout<<"Enter car number: "; cin>>car_number;
cout<<"\n Enter car model: "; cin>>car_model; }
void car::showdata()
{ cout<<"Car number is "<<car_number;
cout<<"\n Car model is "<<car_model; }
int main()
{
car c1;
c1.getdata();
c1.showdata();
cout<<endl<<endl<<"-----------------TUSHAR-----------------------"<<endl;
cout<<endl<<"-----------------07417702021-------------"<<endl;
return 0; }
Output :
Output :
Q11.WAP to find the multiplication of two complex numbers using
oops.
Program :
#include <iostream>
using namespace std;
class complex
{ int re,im;
public:
void get();
void disp();
void mult(complex,complex); };
void complex :: get()
{ cout<<"\nEnter Real Part :: ";
cin>>re;
cout<<"\nEnter Imag. Part :: ";
cin>>im; }
void complex :: disp()
{ cout<<re<<"+"<<im<<"i"<<"\n"; }
void complex::mult(complex c1,complex c2)
{ re=c1.re*c2.re;
im=c1.im*c2.im; }
int main()
{ complex c1,c2,c3;
cout<<"Enter 1st complex no.: \n";
c1.get();
cout<<"\nEnter 2nd complex no.: \n";
c2.get();
cout<<"\n\nThe 1st complex no. is :: ";
c1.disp();
cout<<"\nThe 2nd complex no. is :: ";
c2.disp();
c3.mult(c1,c2);
cout<<"\nThe Multiplication of two complex no.s are :: ";
c3.disp();
return 0; }
Output :
Output :
Q13.Given the account numbers and balance of two accounts wap to
transfer the specified sum from one of these accounts to the other
and then update the balance in both accounts using pass objects by
reference.
Program :
#include <iostream>
using namespace std;
class Account{
public:
int balance;
void getBalance(){
cout<<"Balance:"<<balance<<endl; }
void transfer(Account &a1,Account &a2,int amount){
if(a2.balance<amount){
cout<<"Insufficient Balance:"<<endl;
return; }
cout<<"Amount successfully transferred from account 2 to account 1"<<endl;
a1.balance+=amount;
a2.balance-=amount; } };
int main(){
Account a1,a2;
int amount;
cout<<"Enter Balance for Account1:";
cin>>a1.balance;
cout<<"Enter Balance for Account2:";
cin>>a2.balance;
cout<<"Enter Amount to be transferred:";
cin>>amount;
a1.getBalance();
a2.getBalance();
a1.transfer(a1,a2,amount);
a1.getBalance();
a2.getBalance(); }
Output :
Output :