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

Practical File

Uploaded by

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

Practical File

Uploaded by

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

Q1.Write a c++ program to display the sum of two numbers.

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 :

Q3.Write a program that reads three numbers a ,u and t and then


calculates s=ut+1/2att.
Program :
#include <iostream>
using namespace std;
int main() {
int u,a,t;
float s;
cout<<"Enter the value of U : ";
cin>>u;
cout<<"\nEnter the value of T : ";
cin>>t;
cout<<"\nEnter the value of A : ";
cin>>a;
s=u*t+1/2*a*t*t;
cout<<"\nThe value of s = "<< s;
return 0; }

Output :

Q4.Write a program to find a maximum of two numbers using a ternary


operator.
Program :
#include <iostream>
using namespace std;
int main() {
int n1,n2,max;
cout<<"Enter the value of N1 : ";
cin>>n1;
cout<<"\nEnter the value of N2 : ";
cin>>n2;
max = (n1>n2) ? n1 : n2;
cout<<"The largest number between "<< n1<< " and "<< n2
<< " is "<< max;
return 0;
}
Output :

Q5.Write a program to print whether a number is even or odd.


Program :
#include <iostream>
using namespace std;
int main() {
int num;
cout<<"Enter the number : ";
cin>>num;
if ( num % 2 == 0)
cout<<"The number "<< num<< " is even ";
else
cout<<"The number "<< num << " is odd";
return 0; }

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 :

Q9.WAP to show the pattern.


Program :
#include <iostream>
using namespace std;
int main()
{ int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i)
{ for(int j = 1; j <= i; ++j)
{ cout << j << " "; }
cout << "\n"; }
cout<<endl<<"-----------------TUSHAR-----------------------"<<endl;
cout<<endl<<"-----------------07417702021-------------"<<endl;
return 0;
}
Output :

Q10.WAP to find the difference of two complex numbers using oops.


Program :
#include <iostream>
using namespace std;
class complex
{ int re,im;
public:
void get();
void disp();
void diff(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::diff(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<<"\nThe 1st complex no. is :: ";
c1.disp();
cout<<"\nThe 2nd complex no. is :: ";
c2.disp();
c3.diff(c1,c2);
cout<<"\nThe Diffrence of two complex no.s are :: ";
c3.disp();
return 0; }

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 :

Q12.WAP to find the difference of two complex numbers using oops.


Program :
#include <iostream>
using namespace std;
class Counter
{ int n;
static int Count;
public: void getdata (int number)
{ n = number;
Count++; }
void showcount ()
{ cout << "Counts :" <<Count << endl; } };
int Counter:: Count;
int main()
{ Counter C1, C2, C3, C4;
C1. showcount ();
C2. showcount ();
C3. showcount ();
C4. showcount ();
C1. getdata (10);
C2. getdata (20);
C3. getdata (30);
C4. getdata (40);
cout<<"Value of count after Calling : ";
cout<<"\ngetdata functions"<<endl;
C1. showcount();
C2. showcount();
C2. showcount();
C4. showcount();
return 0; }

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 :

Q14.WAP to illustrate use of objects as function arguments in pass by


value mechanism by performing addition of feet and inches format.
Program :
#include <iostream>
using namespace std;
class sum {
public: int feet;
float inches;
void setdata(int f,float i) { if(i>=12){
int val=i/12;
feet=f+val;
inches=i-12*val;
return; }
feet=f;
inches=i; }
void getdata() {
cout<<"Feet:"<<feet<<endl<<"Inches:"<<inches<<endl; }
void setsum(sum a,sum b) { feet=a.feet+b.feet;
inches=a.inches+b.inches; } };
int main(){
int feet;
float inches;
sum a,b;
cout<<"Enter data for first object:"<<endl;
cout<<"Enter Feet:";
cin>>feet;
cout<<"Enter inches:";
cin>>inches;
a.setdata(feet,inches);
cout<<"Enter data for second object:"<<endl;
cout<<"Enter Feet:";
cin>>feet;
cout<<"Enter inches:";
cin>>inches;
b.setdata(feet,inches);
sum c;
c.setsum(a,b);
c.getdata(); }

Output :

You might also like