6th Phy-311 PP Solved All L+S (2013-2019) by Mha Physics
6th Phy-311 PP Solved All L+S (2013-2019) by Mha Physics
f(x)=3𝒙𝟑 -4𝒙𝟐 + 𝟐𝒙 + 𝟒
Program: (Simple)
#include<iostream>
#include<math.h>
main()
float x;
float fx;
cin>>x;
fx=3*pow(x,3)-4*pow(x,2)+2*x+4;
return 0;
float k;
k=c+273.16;
return k;
main()
float c;
cin>>c;
return 0;
Long Question2013
Q#3
𝟔
Write C++ program to evaluate the ∫𝟏 (𝟑𝒙𝟐 + 𝟏) by Simpson's (1/3) or Weddle’s Rule due to options 1or 2
respectively (use n=6). Report the error message for any other option pressed by the user.
Program:
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
double long f(double long x)
{
return (3*x*x+1);
}
main()
{
int option;
float a,b,h,n,I;
float sum=0;
cout<<"Press 1 for Simpson's rule and Press 2 for wedddle's rule:\t";
cin>>option;
cout<<"enter lower limit:\n";
cin>>a; //1
cout<<"enter upper limit:\n";
cin>>b; //6
cout<<"enter number of intervals:\n";
cin>>n; //n=6 given
switch(option)
{
case 1:
cout<<"integration by Simpson's Rule:\n";
P a g e -3 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
h=(b-a)/n;
sum=f(b)+f(a);
for (int i=0;i<n;i++)
{
if (i%2==0)
{
sum=sum+2*f(a+(i*h));
}
else
{
sum=sum+4*f(a+(i*h));
}
}
I=(h/3)*sum;
cout<<"Definite Integration:\t"<<I<<endl;
break;
case 2:
cout<<"integration by Weddle's Rule:\n";
h=(b-a)/n;
sum=f(b)+f(a);
for (int i=0;i<n;i++)
{
if (i%2==0)
{
sum=sum+f(a+(i*h));
}
else if(i%3==0)
{
sum=sum+6*f(a+(i*h));
}
else
{
sum=sum+5*f(a+(i*h));
Q#4
Suppose A & B be 3x3 matrices. Write C++ program which reads in entries of A & B and Prints out the entries
of matrix which is
(i) A+B and (ii) 5A-𝑩𝒕
Program:
#include<iostream>
#include<math.h>
using namespace std;
main()
{
float A[3][3],B[3][3],C[3][3],D[3][3],E[3][3];
int i,j;
cout<<"enter the values in matrix A:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>A[i][j];
}
}
Q#5
Write C++ Program for the simple Harmonic Motion (S.H.M) of a mass attached with a spring using Euler’s
method under the following conditions:
(g=9.8 ms-2, initial position=zero ,and velocity=15 ms-1 time step 0.1 sec and maximum time 15 sec. k=1 N/m,
m=1Kg).
Calculate and Print values for time , Position and velocity. How u can change the same program for forced
harmonic motion, Damped Harmonic Motion.(give short comments only).
Program:
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
#include<math.h>
main()
float x;
float fx;
cin>>x;
fx=25*pow(x,3)-44*pow(x,2)-21*x+47;
return 0;
#include<math.h>
main()
int i,j,s;
cin>>s;
float x[s],y[s];
for (i=0;i<s;i++)
cin>>x[i];
for (j=0;j<s;j++)
y[j]= 25*(pow(x[j],3))-44*(pow(x[j],2))-21*x[j]+47;
Long Question2014
𝟔 𝒅𝒙
Q#3 Write C++ program to evaluate the ∫ by Trapezoidal or Weddle’s Rule due to options 1or 2
𝟏 𝒙𝟐 −𝟏
respectively (use n=6). Report the error message for any other option pressed by the user.
Program:
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
double long f(double long x)
{
return (1/(x*x-1));
Q#4
Suppose A & B be 3x3 matrices. Write C++ program which reads in entries of A & B and Prints out the entries
of matrix which is
(i) A-B and (ii) C=𝑩𝒕
Program:
#include<iostream>
#include<math.h>
using namespace std;
main()
Find the roots of the equation “ 3x-cosx-5” using simple iterative method using Xo=1.5. Write
C++ Program Program to implement the method correct to 2 decimal places?
Program:
#include<iostream>
#include<conio.h>
#include<math.h>
#include<iomanip>
using namespace std;
float f(float x)
{
return (0.333*(cos(x)+5));
}
main()
{
float E,X0,X1,X2,x;
int n,i,flag=1;
cout<<" enter an educatd guess ";
cin>>n;
cout<<" enter value for X0 ";
cin>>X0;
cout<<" enter accuracy ";
cin>>E;
cout<<" iteration \t flag \t root ";
cout<<setw(8)<<"iteration"<<setw(16)<<"x(n+1)"<<setw(16)<<"x(n)"<<setw(16)<<"x(n+1)-x(n)"<<endl;
for( i=1; i<n&&flag; i++ )
{
X1=f(X0);
x=X1-X0;
if(x<0)
{
x=-x;
}
Q#5(b) Write C++ program which reads in a number and display its table. Implement your program
for 10 iterations?
Program:
#include<iostream>
#include<math.h>
using namespace std;
main()
{
int n,i;
cout<<"enter the number:\t";
cin>>n;
for(i=1;i<=10;i++)
P a g e -16 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
{
cout<<n<<"X"<<i<<"="<<n*i<<endl;
}
return 0;
}
#include<math.h>
main()
float x,y;
float fx;
cin>>x;
cin>>y;
fx=12*pow(x,2)-4*pow(y,2);
return 0;
float f;
f=(9*c)/5+32;
return f;
main()
float c;
cin>>c;
cout<<"Temperaturein fahrenheit:="<<fah(c);
return 0;
Program:
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
double long f(double long x)
{
return (2*x*x-1);
}
main()
{
P a g e -19 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
int option;
float a,b,h,n,I;
float sum=0;
cout<<"Press 1 for Trapezoidal and Press 2 for
weddle's rule:\t";
cin>>option;
cout<<"enter lower limit:\n";
cin>>a; //1
cout<<"enter upper limit:\n";
cin>>b; //6
cout<<"enter number of intervals:\n";
cin>>n; //n=6 given
switch(option)
{
case 1:
cout<<"integration by Trapezoidal Rule:\n";
h=(b-a)/n;
sum=f(b)+f(a);
for (int i=1;i<n;i++)
{
sum=sum+2*f(a+(i*h));
}
I=(h/2)*sum;
cout<<"Definite Integration:\t"<<I<<endl;
break;
case 2:
cout<<"integration by Weddle's Rule:\n";
h=(b-a)/n;
Program:
#include<iostream>
#include<math.h>
using namespace std;
main()
{
float A[3][3],B[3][3],C[3][3],K[3][3],E[3][3];
int i,j,l,sum;
cout<<"enter the values in matrix A:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>A[i][j];
}
}
cout<<"enter the values in matrix B:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>B[i][j];
}
}
cout<<"entered values in matrix A:\n";
for(i=0;i<3;i++)
P a g e -22 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
{
for(j=0;j<3;j++)
{
cout<<A[i][j]<<"\t";
}
cout<<endl;
}
cout<<"entered values in matrix B:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<B[i][j]<<"\t";
}
cout<<endl;
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
K[i][j]=7*A[i][j]-5*B[i][j];
E[i][j]=B[j][i];
}
}
cout<<" Sum of k=7A-5B is:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
Short Question2016
Write C++ program Code Segment for the following:
(a) To calculate equivalent capacitance of two capacitor connected in parallel
( Repeat 2015)
Program:
#include<iostream>
using namespace std;
main()
{
float C,C1,C2,Ceq=0;
cout<<"Enter the value of capacitor 1:\n";
cin>>C1;
cout<<"Enter the value of Capacitor 2:\n";
cin>>C2;
Ceq=C1+C2;
cout<<"The equivalent capacitance in parallel combination==:"<<Ceq;
return 0;
}
Program: (simple)
#include<iostream>
#include<math.h>
main()
float x;
cin>>x;
fx=(pow(x,2)-2*x+4)/15;
return 0;
#include<math.h>
#include<stdio.h>
main()
int i,j,s;
cin>>s;
float x[s],y[s];
for (i=0;i<s;i++)
cin>>x[i];
for (j=0;j<s;j++)
y[j]=(pow(x[j],2)-2*x[j]+4)/15;
float f;
f=(9*c)/5+32;
return f;
main()
float c;
cin>>c;
cout<<"Temperaturein fahrenheit:="<<fah(c);
return 0;
Program:
#include<iostream>
#include<conio.h>
P a g e -28 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
#include<math.h>
using namespace std;
double long f(double long x)
{
return (sqrt(x));
}
main()
{
int option;
float a,b,h,n,I;
float sum=0;
cout<<"Press 1 for Trapezoidal and Press 2 for Simpson's Rule:\t";
cin>>option;
cout<<"enter lower limit:\n";
cin>>a; //1
cout<<"enter upper limit:\n";
cin>>b; //6
cout<<"enter number of intervals:\n";
cin>>n; //n=6 given
h=(b-a)/n;
sum=f(b)+f(a);
switch(option)
{
case 1:
cout<<"integration by Trapezoidal Rule:\n";
for (int i=1;i<n;i++)
{
sum=sum+2*f(a+(i*h));
}
I=(h/2)*sum;
cout<<"Definite Integration:\t"<<I<<endl;
break;
case 2:
Q#4 Suppose A & B be 3x3 matrices. Write C++ program which reads in entries of A & B and Prints out the
entries of matrix which is
(i) AxB and (ii) 4𝑨 + 𝟐𝑩𝒕
Program:
#include<iostream>
#include<math.h>
using namespace std;
main()
{
int A[3][3],B[3][3],C[3][3],D[3][3],E[3][3];
Q#5(a)
Write C++ Program to determine equivalent resistance of n resistances Connected in Series (READ
RESISTANCE VALUE FROM USER).
Program:
#include<iostream>
using namespace std;
main()
{
float R,Req;
int n;
cout<<"Enter the value of resistance of R:=\t";
cin>>R;
cout<<"Enter Total resistacnce connected in series:=\t";
cin>>n;
Req=n*R;
cout<<"Equivalent resistance of"<<""<<n<<""<<"Resistance is=\t";
cout<<Req;
return 0;
}
Q#5(b)
What is an Array? Write down the general syntax for the usage of an array, if “a” ,”b” and “c” are the three
linear arrays,
Write C++ Program to calculate the 𝒄 = 𝒂𝟐 − 𝒃𝟐 , Initialize “a” by ten odd numbers and “b” by ten
even numbers. Implement your program using arrays?
Syntax:
The general syntax of One Dimensional Linear Array is below:
Type Name [Count]
Program:
#include<iostream>
#include<iomanip>
using namespace std;
main()
{
int a[10]={1,3,5,7,9,11,13,15,17,19};
int b[10]={2,4,6,8,10,12,14,16,18,20};
int c[9],i;
for(i=0;i<10;i++)
{
c[i]=a[i]*a[i]-b[i]*b[i];
}
for(i=0;i<10;i++)
cout<<setw(10)<<a[i]<<setw(10)<<b[i]<<setw(20)<<c[i]<<endl;
return 0;
}
f(x)=7√(𝟑𝒙𝟐 − 𝟐𝒙 − 𝟐𝟐)
and calculate the maxmimun and minimum of f(x)
Program:
#include<iostream>
#include<math.h>
main()
int i,j,k,l,s;
cin>>s;
float x[s],y[s];
float max,min;
for (i=0;i<s;i++)
cin>>x[i];
for (j=0;j<s;j++)
y[j]= 7*sqrt(3*(pow(x[j],2))-2*x[j]-22);
max=y[0];
for(k=0;k<s;k++)
if (max<y[k])
max=y[k];
for(l=0;l<s;l++)
if (min>y[l])
min=y[l];
return 0;
#include<math.h>
main()
int i,j,k,max,min,s;
cin>>s;
float x[s],y[s],fx[s];
for (i=0;i<s;i++)
cin>>x[i];
for (j=0;j<s;j++)
y[j]=(3*x[j]*x[j]-2*x[j]-22);
fx[j]=7*sqrt(y[j]);
max=fx[s];
if(max<fx[j])
max=fx[j];
min=fx[0];
if(min>fx[j])
min=fx[j];
return 0;
(c) To read the (x,y) values from user and calculate polar coordinates (r,𝜽)?
Program: (Simple)
#include<iostream>
#include<math.h>
using namespace std;
main()
{
int x,y;
float t,r,theeta;
cout<<"enter the value of X & y:\n";
cin>>x>>y;
r=sqrt(x*x+y*y);
t=y/x;
theeta=atan(t);
cout<<"value of (x,y)=("<<x<<","<<y<<") and (r,theeta)=("<<r<<","<<theeta<<")"<<endl;
return 0;
}
Program: (using function)
#include<iostream>
#include<math.h>
using namespace std;
float r(float x,float y)
{
return (sqrt(pow(x,2)+pow(y,2)));
}
float theta(float x,float y)
{
return (atan(y/x));
}
main()
{
float x,y;
cout<<"enter the x coordinates:\n";
cin>>x;
cout<<"enter the y coordinates:\n";
cin>>y;
cout<<"values of (x,y)="<<"("<<x<<","<<y<<")"<<"and (r,0)=("<<r(x,y)<<","<<theta(x,y)<<")"<<endl;
return 0;
}
Program:
#include<iostream>
#include<conio.h>
#include<math.h>
P a g e -37 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
using namespace std;
double long f(double long x)
{
return ((x*x+4)/5);
}
main()
{
int option;
float a,b,h,n,I;
float sum=0;
cout<<"Press 1 for Trapezoidal and Press 2 for wedddle's rule:\t";
cin>>option;
cout<<"enter lower limit:\n";
cin>>a; //1
cout<<"enter upper limit:\n";
cin>>b; //6
cout<<"enter number of intervals:\n";
cin>>n; //n=6 given
switch(option)
{
case 1:
cout<<"integration by Trapezoidal Rule:\n";
h=(b-a)/n;
sum=f(b)+f(a);
for (int i=1;i<n;i++)
{
sum=sum+2*f(a+(i*h));
}
I=(h/2)*sum;
cout<<"Definite Integration:\t"<<I<<endl;
break;
case 2:
cout<<"integration by Weddle's Rule:\n";
Q#4 Suppose A & B be 3x3 matrices. Write C++ program which reads in entries of A & B and Prints out the
entries of matrix which is
(i) C=AxB and (ii) 𝟐𝑨𝒕 + 𝟑𝑩
Program:
#include<iostream>
main()
int A[3][3],B[3][3],C[3][3],D[3][3],E[3][3];
int i,j,k,sum;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>A[i][j];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>B[i][j];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<A[i][j]<<"\t";
cout<<endl;
for(j=0;j<3;j++)
cout<<B[i][j]<<"\t";
cout<<endl;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
sum=0;
for(k=0;k<3;k++)
sum=sum+A[i][k]*B[k][j];
C[i][j]=sum;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<C[i][j]<<"\t";
cout<<endl;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
P a g e -41 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
{
D[i][j]=A[j][i];
E[i][j]=2*D[i][j]+3*B[i][j];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<E[i][j]<<"\t";
cout<<endl;
return 0;
Q#5 (a)
Find the roots of the equation “ 3x-cosx-5” using simple iterative method using Xo=1.5. Write
C++ Program Program to implement the method correct to 2 decimal places?
Program:
USING FUNCTION:
#include<iostream>
using namespace std;
float c(float f)
{
return (5*(f-32)/9);
}
main()
{
float f;
cout<<"enter the temperatue in fahrenheit:\t";
cin>>f;
cout<<"Teperature in celcuis:\t"<<c(f)<<endl;
return 0;
}
USING FUNCTION:
#include<iostream>
using namespace std;
int km(int meter)
{
return (meter/1000);
}
main()
{
float m;
cout<<"enter the distance in metre:\t";
cin>>m;
cout<<"distance in kilometer:\t"<<km(m)<<endl;
return 0;
}
f(x)= 𝒙𝟐 + 𝟕𝟕𝐱𝐲 − 𝒚𝟐
Program: (simple)
#include<iostream>
#include<math.h>
float x,y;
float fx;
cin>>x;
cin>>y;
fx=pow(x,2)+77*x*y-pow(y,2);
return 0;
Program: (simple)
#include<iostream>
#include<math.h>
main()
float x,y;
float fx;
cin>>x;
y=2*pow(x,3)-4*pow(x,2)-2*x+4;
fx=5/sqrt(y);
return 0;
#include<math.h>
main()
int i,j,k,s;
cin>>s;
float x[s],y[s],z[s];
for (i=0;i<s;i++)
cin>>x[i];
for (j=0;j<s;j++)
y[j]= 2*(pow(x[j],3))-4*(pow(x[j],2))-2*x[j]+4;
z[j]=5/sqrt(y[j]);
return (gram/1000);
main()
float g;
cin>>g;
return 0;
Program:
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
P a g e -48 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
double long f(double long x)
{
return (1/(pow(x,3)-5));
}
main()
{
int option;
float a,b,h,n,I;
float sum=0;
cout<<"Press 1 for Simpson's and Press 2 for Trapezoidal rule:\t";
cin>>option;
cout<<"enter lower limit:\n";
cin>>a; //1
cout<<"enter upper limit:\n";
cin>>b; //6
cout<<"enter number of intervals:\n";
cin>>n; //n=6 given
h=(b-a)/n;
sum=f(b)+f(a);
switch(option)
{
case 1:
cout<<"integration by Simpson's Rule:\n";
for (int i=0;i<n;i++)
{
if (i%2==0)
{
sum=sum+2*f(a+(i*h));
}
else
{
sum=sum+4*f(a+(i*h));
}
Q#4
Suppose A & B be 3x3 matrices. Write C++ program which reads in entries of A & B and Prints out the entries
of matrix which is
(i) 7A-3B and (ii) C=𝟐𝑨𝒕
Program:
#include<iostream>
#include<math.h>
using namespace std;
main()
{
float A[3][3],B[3][3],C[3][3],D[3][3];
int i,j;
Q#5 (a)
Program:
#include<iostream>
#include<conio.h>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
float x=0,t=0,a,tmax=15,v=15,k=1,m=1,h=0.1,b=0.5,f=1.5,w=0.01;
a=((-k*x)+(-b*v)+(f*cos(w*t)))/m;
int p=0;
float X[1000],T[1000],V[1000],A[1000];
while(t<=tmax)
{
a=((-k*x)+(-b*v)+(f*cos(w*t)))/m;
T[p]=t;
V[p]=v;
X[p]=x;
A[p]=a;
x=x+(v*h);
v=v+(a*h);
t=t+h;
p++;
}
cout<<setw(16)<<" Time "<<setw(16)<<" position "<<setw(16)<<" velocity "<<setw(16)<<" accelaration
"<<endl;
for( int i=0; i<p; i++ )
P a g e -53 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
{
cout<<setw(16)<<T[i]<<setw(16)<<X[i]<<setw(16)<<V[i]<<setw(16)<<A[i]<<endl;
}
return 0;
}
Q#5 (b)
Write C++ Program which reads in 10 numbers into an array and display those numbers in reverse order.
Program:
#include <iostream>
using namespace std;
int main()
{
int arr[10];
int hold;
for(int i=0; i<10; i++)
{
cin>>arr[i];
}
for(int i=0; i<10; i++)
{
for(int j=i+1; j<10; j++)
{
if(arr[j] > arr[i])
{
hold = arr[i];
arr[i] = arr[j];
arr[j] = hold;
}
}
P a g e -54 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
}
cout<<"Elements of array is in reverse order:\n";
for(int i=0; i<10; i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
All Program End (2013-2019)