0% found this document useful (0 votes)
60 views55 pages

6th Phy-311 PP Solved All L+S (2013-2019) by Mha Physics

The document provides C++ code solutions to computational physics problems involving: 1) Calculating equivalent resistance in series using a for loop. 2) Printing values of x and the function f(x)=3x^3 - 4x^2 + 2x + 4 using arrays. 3) Calculating the factorial of a user-input number using a for loop. 4) Converting temperature from Celsius to Kelvin using a function. 5) Evaluating integrals using Simpson's and Weddle's rules with input for limits and intervals. 6) Adding and subtracting 3x3 matrices by accessing and manipulating 2D arrays. 7) Modeling simple harmonic motion using Euler's method with

Uploaded by

Mansrajian Bs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views55 pages

6th Phy-311 PP Solved All L+S (2013-2019) by Mha Physics

The document provides C++ code solutions to computational physics problems involving: 1) Calculating equivalent resistance in series using a for loop. 2) Printing values of x and the function f(x)=3x^3 - 4x^2 + 2x + 4 using arrays. 3) Calculating the factorial of a user-input number using a for loop. 4) Converting temperature from Celsius to Kelvin using a function. 5) Evaluating integrals using Simpson's and Weddle's rules with input for limits and intervals. 6) Adding and subtracting 3x3 matrices by accessing and manipulating 2D arrays. 7) Modeling simple harmonic motion using Euler's method with

Uploaded by

Mansrajian Bs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Bs Physics Semester 6th

By: Mha Physics Code: Phy-311 Subject: Computational Physics-I

Past Papers (Short + Long) Solved All Program (2013-2019)

GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA

Short Question 2013


Write C++ program Code Segment for the following:
(a) To calculate equivalent of resistance in series?
Program:
#include<iostream>
using namespace std;
main()
{
float R,n,Req=0;
cout<<"Enter the Number of Resistance In Series :\n";
cin>>n;
cout<<"Enter the value of Resistance:\n";
cin>>R;
for (int i=0;i<n;i++)
{
Req=Req+R;
}
cout<<"The equivalent resistance in series combination of same resistance =:\n"<<Req;
return 0;
}
(b) To print “x” aginst “f(X)” for the equation

f(x)=3𝒙𝟑 -4𝒙𝟐 + 𝟐𝒙 + 𝟒
Program: (Simple)
#include<iostream>

#include<math.h>

using namespace std;

main()

float x;

float fx;

cout<<"enter the value of x:\n";

cin>>x;

fx=3*pow(x,3)-4*pow(x,2)+2*x+4;

cout<<"Value of x="<<x<<" & corresponding function="<<fx;

return 0;

Program: (using array)


#include<iostream>
P a g e -1 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
#include<math.h>
using namespace std;
main()
{
int i,j,s;
cout<<"enter the size of array :\n";
cin>>s;
float x[s],y[s];
for (i=0;i<s;i++)
{
cout<<"Enter The values of X"<<i<<endl;
cin>>x[i];
}
cout<<"Values of X is="<<"\t\t"<<"and f(x)="<<endl;
for (j=0;j<s;j++)
{
y[j]= 3*(pow(x[j],3))-4*(pow(x[j],2))+2*x[j]+4;
cout<<"\t"<<x[j]<<"\t\t\t"<<y[j]<<endl;
}
return 0;
}
(c) To calculate the factorial of a number read from the user ?
Program:
#include<iostream>
using namespace std;
main()
{
unsigned long fact=1;
int i,n;
cout<<"Enter the number:\n";
cin>>n; //any number whose factorial you want to find
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"factorial of number is="<<fact;
return 0;
}
(d) To convert temperature from Celsius to kelvin scale using a function ?
Program:
#include<iostream>

using namespace std;

float kelvin (int c)

float k;

k=c+273.16;

return k;

main()

float c;

cout<<"enter the celcius temperature:\n";

cin>>c;

P a g e -2 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


cout<<"Temperaturein Kelvin:="<<kelvin(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));

P a g e -4 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


}
}
I=(3*h*sum)/10;
cout<<"Definite Integration:\t"<<I<<endl;
break;
default:
{
cout<<" error :Invalid option"<<endl;
break;
}
}
return 0;
}

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];
}
}

P a g e -5 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


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++)
{
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++)
{
C[i][j]=A[i][j]+B[i][j];
D[i][j]=B[j][i];
E[i][j]=5*A[i][j]-D[i][j];

P a g e -6 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


}
}
cout<<" Sum of A+B is:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<C[i][j]<<"\t";
}
cout<<endl;
}
cout<<" Sum of 5A-Bt is:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<E[i][j]<<"\t";
}
cout<<endl;
}
return 0;
}

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;

P a g e -7 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


main()
{
float x=0,t=0,a,tmax=15,v=15,k=-1,m=1,h=0.1;
a=(-1)*(k*x)/m; //(F=ma=-kx so we get formula a=-kx/m)
int p=0;
float X[1000],T[1000],V[1000],A[1000];
while (t<=tmax)
{
T[p]=t;
V[p]=v;
X[p]=x;
A[p]=a;
x=x+(x*h); //xf=xi+vt bcz s=x=vt
v=v+(a*h); //Vf=vi+at
t=t+h;
p++; //increment operator
}
cout<<setw(16)<<"Time"<<setw(16)<<"Position"<<setw(16)<<"Velocity"<<setw(16)<<"A
cceleration"<<endl;
for(int i=0;i<p;i++)
{
cout<<setw(16)<<T[i]<<setw(16)<<X[i]<<setw(16)<<V[i]<<setw(16)<<A[i]<<endl;
}
return 0;
}

Short Question 2014


Write C++ program Code Segment for the following:
(a) To calculate equivalent capacitance of two capacitor connected in series
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;

P a g e -8 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


Ceq=(C1*C2)/(C1+C2);
cout<<"The equivalent capacitance in series combination==:"<<Ceq;
return 0;
}

(b) To print “x” aginst “f(X)” for the equation

f(x)=25𝒙𝟑 -44𝒙𝟐 − 𝟐𝟏𝒙 + 𝟒𝟕


Program: (simple)
#include<iostream>

#include<math.h>

using namespace std;

main()

float x;

float fx;

cout<<"enter the value of x:\n";

cin>>x;

fx=25*pow(x,3)-44*pow(x,2)-21*x+47;

cout<<"Value of x="<<x<<" & corresponding function="<<fx;

return 0;

Program: (using Array)


#include<iostream>

#include<math.h>

using namespace std;

main()

int i,j,s;

cout<<"enter the size of array :\n";

cin>>s;

float x[s],y[s];

for (i=0;i<s;i++)

cout<<"Enter The values of X"<<i<<endl;

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;

cout<<"Values of X is="<<x[j]<<"\t and f(x)="<<y[j]<<endl;

P a g e -9 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


return 0;

(c) To calculate the factorial of a number (Repeat 2013)


Program:
#include<iostream>
using namespace std;
main()
{
float R,n,Req=0;
cout<<"Enter the Number of Resistance In Series :\n";
cin>>n;
cout<<"Enter the value of Resistance:\n";
cin>>R;
for (int i=0;i<n;i++)
{
Req=Req+R;
}
cout<<"The equivalent resistance in series combination of same resistance =:\n"<<Req;
return 0;
}
(d) To convert from Kilogram to gram using a function ?
Program:
#include<iostream>
using namespace std;
float gram(float kg)
{
float g;
g=kg/1000;
return g;
}
main()
{
float kilo;
cout<<"enter the value in kilogram:\n";
cin>>kilo;
cout<<"print value in gram:="<<gram(kilo);
return 0;
}

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));

P a g e -10 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


}
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";
h=(b-a)/n;
sum=f(b)+f(a);
for (int i=0;i<n;i++)
{

P a g e -11 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


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));
}
}
I=(3*h*sum)/10;
cout<<"Definite Integration:\t"<<I<<endl;
break;
default:
{
cout<<" error :Invalid option"<<endl;
break;
}
}
return 0;
}

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()

P a g e -12 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


{
float A[3][3],B[3][3],C[3][3],D[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];
}
}
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++)
{
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++)
{

P a g e -13 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


cout<<B[i][j]<<"\t";
}
cout<<endl;
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
D[i][j]=A[i][j]-B[i][j];
C[i][j]=B[j][i];
}
}
cout<<" Result of A-B is:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<D[i][j]<<"\t";
}
cout<<endl;
}
cout<<" Sum of C=Bt is:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<C[i][j]<<"\t";
}
cout<<endl;
}
return 0;
}

P a g e -14 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


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:
#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;
}

P a g e -15 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


if(x<E)
{
flag=0;
}
else
{
cout<<setw(8)<<i<<setw(16)<<X1<<setw(16)<<X0<<setw(16)<<x<<endl;
X0=X1;
}
}
if(flag==1)
{
cout<<" solution does not exist ";
}
else
{
cout<<" after "<<i-1<<" iteration root= "<<X1<<endl;
}
return 0;
}

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;
}

Short Question 2015


Write C++ program Code Segment for the following:
(a) To calculate equivalent capacitance of two capacitor connected in parallel
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;
}
(b) To print “x & y” aginst “f(X,Y)” for the equation

f(x)= 12𝒙𝟐 − 𝟒𝒚𝟐


Program: (simple)
#include<iostream>

#include<math.h>

using namespace std;

main()

float x,y;

float fx;

cout<<"enter the value of x:\n";

cin>>x;

cout<<"enter the value of y:\n";

cin>>y;

fx=12*pow(x,2)-4*pow(y,2);

cout<<"Value of (x,y):("<<x<<","<<y<<") & corresponding function="<<fx;

return 0;

Program: (using array)


P a g e -17 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
#include<iostream>
#include<math.h>
using namespace std;
main()
{
int i,j,k,s;
cout<<"enter the size of array :\n";
cin>>s;
float x[s],y[s],z[s];
for (i=0;i<s;i++)
{
cout<<"Enter The values of X"<<i<<endl;
cin>>x[i];
}
for (j=0;j<s;j++)
{
cout<<"Enter The values of y"<<j<<endl;
cin>>y[j];
}
for (k=0;k<s;k++)
{
z[k]=12*(pow(x[k],2))-4*(pow(y[k],2));
cout<<" values of (x,y):("<<x[k]<<","<<y[k]<<")"<<" and f(x,y)="<<z[k]<<endl;
}
return 0;
}

(c) To calculate the table of number 5


Program:
#include<iostream>
using namespace std;
main()
{
int i,n,a;
n=5;
cout<<"Table of Number 5 is:"<<endl;
for (int i=1;i<=10;i++)
{
a=n*i;
cout<<n<<"x"<<i<<"="<<a<<endl;
}

P a g e -18 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


return 0;
}
(d) To convert temperature from Celsius to fahrenheit scale using a function ?
Program:
#include<iostream>

using namespace std;

float fah (float c)

float f;

f=(9*c)/5+32;

return f;

main()

float c;

cout<<"enter the celcius temperature:\n";

cin>>c;

cout<<"Temperaturein fahrenheit:="<<fah(c);

return 0;

Long Question 2015


𝟔
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 (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;

P a g e -20 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


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));
}
}
I=(3*h*sum)/10;
cout<<"Definite Integration:\t"<<I<<endl;
break;
default:
{
cout<<" error :Invalid option"<<endl;
break;
}
}
return 0;
}
Q#4

P a g e -21 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


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)K=7A-5B and (ii) C=𝑨𝒙𝑩𝒕

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++)

P a g e -23 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


{
cout<<K[i][j]<<"\t";
}
cout<<endl;
}
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
sum=0;
for(l=0;l<3;l++)
sum=sum+A[i][l]*E[l][j];
C[i][j]=sum;
}
}
cout<<"Result of C=A*B':\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<C[i][j]<<"\t";
}
cout<<endl;
}
return 0;
}
Q#5 (a)
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:
P a g e -24 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
(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:

REPEAT 2013 Question #5 Long


Q#5(b)
Write C++ Program which reads in a number and display its factorial.
Implement your Program for 10 iterations?

Program: (same Number 10 Times)


#include<iostream>
#include<math.h>
using namespace std;
unsigned long fact(int n)
{
int fact=1;
for(int i=1;i<=n;i++)
fact=fact*i;
return fact;
}
main()
{
int c,n;
cout<<"enter positive number :\n";
cin>>n;
for(c=1;c<=10;c++)
cout<<"factorial of"<<c<<" iteration: "<<fact(n)<<endl;
return 0;
}

P a g e -25 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


Program: (Different 10 Number) { 1st is corrct }
#include<iostream>
#include<math.h>
using namespace std;
unsigned long fact(int n)
{
int fact=1;
for(int i=1;i<=n;i++)
fact=fact*i;
return fact;
}
main()
{
int n;
for(n=1;n<=10;n++)
cout<<"factorial of"<<n<<"="<<fact(n)<<endl;
return 0;
}

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;
}

P a g e -26 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


(b) To print “x” and “f(X)” for the equation
(𝒙𝟐 −𝟐𝒙+𝟒)
f(x)= 𝟏𝟓

Program: (simple)
#include<iostream>

#include<math.h>

using namespace std;

main()

float x;

long double fx;

cout<<"enter the value of x:\n";

cin>>x;

fx=(pow(x,2)-2*x+4)/15;

cout<<"Value of x:"<<x<<" & corresponding function="<<fx;

return 0;

Program: (using array)


#include<iostream>

#include<math.h>

#include<stdio.h>

using namespace std;

main()

int i,j,s;

cout<<"enter the size of array :\n";

cin>>s;

float x[s],y[s];

for (i=0;i<s;i++)

cout<<"Enter The values of X"<<i<<endl;

cin>>x[i];

for (j=0;j<s;j++)

y[j]=(pow(x[j],2)-2*x[j]+4)/15;

cout<<" values of x="<<x[j]<<" and f(x)="<<y[j]<<endl;

P a g e -27 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


return 0;

(c) To calculate the factorial of a number read from the user ?


Program: (Repeat 2013)
#include<iostream>
using namespace std;
main()
{
unsigned long fact=1;
int i,n;
cout<<"Enter the number:\n";
cin>>n; //any number whose factorial you want to find
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"factorial of number is="<<fact;
return 0;
}
(d) To convert temperature from Celsius to fahrenheit scale using a function ?
Program: (Repeat 2015)
#include<iostream>

using namespace std;

float fah (float c)

float f;

f=(9*c)/5+32;

return f;

main()

float c;

cout<<"enter the celcius temperature:\n";

cin>>c;

cout<<"Temperaturein fahrenheit:="<<fah(c);

return 0;

Long Question 2016


𝟔
Q#3 Write C++ program to evaluate the ∫
𝟏 √
𝒙 𝒅𝒙 by Trapezoidal or Simpson’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>
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:

P a g e -29 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


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));
}
}
I=(h/3)*sum;
cout<<"Definite Integration:\t"<<I<<endl;
break;
default:
{
cout<<" error :Invalid option"<<endl;
break;
}
}
return 0;
}

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];

P a g e -30 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


int i,j,k,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++)
{
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";
}

P a g e -31 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


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;
}
}
}
cout<<"result AxB is:\n";
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++)
{
D[i][j]=B[j][i];
E[i][j]=4*A[i][j]+2*D[i][j];
}
}
cout<<"Result of 4A+2B: \n";
for(i=0;i<3;i++)

P a g e -32 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


{
for(j=0;j<3;j++)
{
cout<<E[i][j]<<"\t";
}
cout<<endl;
}
return 0;
}

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?

P a g e -33 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


Program:
Array:
An array contains Multiple objects of identical types stored sequentially in memory. The Individual
objects in an array, referred to as “Array Elements”, can be addressed using a number ,called “Index or
Subscript .”

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;
}

Short Question 2017:


Write C++ program Code Segment for the following:
(a) To print table of number enter by user
Program:
#include<iostream>
using namespace std;
main()
{

P a g e -34 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


int n,i;
cout<<"Enter the number:\n";
cin>>n;
cout<<"table of number '"<<n<<" 'is:\n";
for (i=1;i<=10;i++)
{
cout<<n<<"x"<<i<<"="<<n*i<<endl;
}
return 0;
}
(b) To print “x” aginst “f(X)” for the equation

f(x)=7√(𝟑𝒙𝟐 − 𝟐𝒙 − 𝟐𝟐)
and calculate the maxmimun and minimum of f(x)
Program:
#include<iostream>

#include<math.h>

using namespace std;

main()

int i,j,k,l,s;

cout<<"enter the size of array :\n";

cin>>s;

float x[s],y[s];

float max,min;

for (i=0;i<s;i++)

cout<<"Enter The values of X"<<i<<endl;

cin>>x[i];

for (j=0;j<s;j++)

y[j]= 7*sqrt(3*(pow(x[j],2))-2*x[j]-22);

cout<<"Values of X is="<<x[j]<<"\t and f(x)="<<y[j]<<endl;

max=y[0];

for(k=0;k<s;k++)

if (max<y[k])

max=y[k];

cout<<"Maximum value of f(x) is="<<max<<endl;

P a g e -35 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


min=y[0];

for(l=0;l<s;l++)

if (min>y[l])

min=y[l];

cout<<"Minimum value of f(x) is="<<min<<endl;

return 0;

(Above Programme New Method)


#include<iostream>

#include<math.h>

using namespace std;

main()

int i,j,k,max,min,s;

cout<<"enter the size of array :\n";

cin>>s;

float x[s],y[s],fx[s];

for (i=0;i<s;i++)

cout<<"Enter The values of X"<<i<<endl;

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]);

cout<<"Values of X is="<<x[j]<<"\t and f(x)="<<fx[j]<<endl;

max=fx[s];

for( j=0; j<s; j++ )

if(max<fx[j])

max=fx[j];

cout<<" the max of F(x) is "<<max<<endl;

min=fx[0];

for( j=0; j<s; j++ )

P a g e -36 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


{

if(min>fx[j])

min=fx[j];

cout<<" the min of F(x) is "<<min<<endl;

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;
}

Long Question 2017::


𝟔 𝒙𝟐 +𝟒
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>
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";

P a g e -38 | (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+f(a+(i*h));
}
else if(i%3==0)
{
sum=sum+6*f(a+(i*h));
}
else
{
sum=sum+5*f(a+(i*h));
}
}
I=(3*h*sum)/10;
cout<<"Definite Integration:\t"<<I<<endl;
break;
default:
{
cout<<" error :Invalid option"<<endl;
break;
}
}
return 0;
}

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>

P a g e -39 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


#include<math.h>

using namespace std;

main()

int A[3][3],B[3][3],C[3][3],D[3][3],E[3][3];

int i,j,k,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++)

for(j=0;j<3;j++)

cout<<A[i][j]<<"\t";

cout<<endl;

cout<<"entered values in matrix B:\n";


P a g e -40 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
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++)

sum=0;

for(k=0;k<3;k++)

sum=sum+A[i][k]*B[k][j];

C[i][j]=sum;

cout<<"result C=AxB is:\n";

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];

cout<<"Result of 2A'+3B: \n";

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:

Repeat 2014 Q#5 (a)


Q#5 (b)
Write C++ Program for Conversion of
(i) Temperature From “F” to “C”?
(ii) Length from “m” to “km”?

Program: (Temperature From “F” to “C”)


SIMPLE:
#include<iostream>
using namespace std;
main()
{
float f,c;
P a g e -42 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
cout<<"enter the temperatue in fahrenheit:\t";
cin>>f;
c=5*(f-32)/9;
cout<<"Teperature in celcuis:\t"<<c<<endl;
return 0;
}

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;
}

Program: (Length from “m” to “km”)


SIMPLE:
#include<iostream>
using namespace std;
main()
{
float m,km;
cout<<"enter the distance in metre:\t";
cin>>m;
km=(m/1000);
cout<<"distance in kilometer:\t"<<km<<endl;
P a g e -43 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
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;
}

Short Question 2018


Write C++ program Code Segment for the following:
(a) To calculate and print equivalent capacitance of two capacitor connected in series read capacitance
from user? (Repeat-2014 a )
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)/(C1+C2);
cout<<"The equivalent capacitance in series combination==:"<<Ceq;
return 0;
}
(b) To print “x & y” against “f(X,Y)” for the equation

f(x)= 𝒙𝟐 + 𝟕𝟕𝐱𝐲 − 𝒚𝟐
Program: (simple)
#include<iostream>

#include<math.h>

using namespace std;


P a g e -44 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA
main()

float x,y;

float fx;

cout<<"enter the value of x:\n";

cin>>x;

cout<<"enter the value of y:\n";

cin>>y;

fx=pow(x,2)+77*x*y-pow(y,2);

cout<<"Value of (x,y):("<<x<<","<<y<<") & corresponding function="<<fx;

return 0;

Program: (using array)


#include<iostream>
#include<math.h>
using namespace std;
main()
{
int i,j,k,s;
cout<<"enter the size of array :\n";
cin>>s;
float x[s],y[s],z[s];
for (i=0;i<s;i++)
{
cout<<"Enter The values of X"<<i<<endl;
cin>>x[i];
}
for (j=0;j<s;j++)
{
cout<<"Enter The values of y"<<j<<endl;
cin>>y[j];
}
for (k=0;k<s;k++)
{
z[k]=(pow(x[k],2))+77*x[k]*y[k]-(pow(y[k],2));

P a g e -45 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


cout<<" values of (x,y):("<<x[k]<<","<<y[k]<<")"<<" and f(x,y)="<<z[k]<<endl;
}
return 0;
}

(c) For Minimum Value of F(x) see Below Program


Program:
#include<iostream>
#include<math.h>
using namespace std;
main()
{
int i,j,k,s;
cout<<"enter the size of array :\n";
cin>>s;
float x[s],y[s],z[s],min ;
for (i=0;i<s;i++)
{
cout<<"Enter The values of X"<<i<<endl;
cin>>x[i];
}
for (j=0;j<s;j++)
{
cout<<"Enter The values of y"<<j<<endl;
cin>>y[j];
}
for (k=0;k<s;k++)
{
z[k]=(pow(x[k],2))+77*x[k]*y[k]-(pow(y[k],2));
cout<<" values of (x,y):("<<x[k]<<","<<y[k]<<")"<<" and f(x,y)="<<z[k]<<endl;
}
min=z[0];
for(k=0;k<s;k++)
{
if (min>z[k])
min=z[k];
}
cout<<"minimum Value of f(x,y)= "<<min<<endl;
return 0;
}

Short Question 2019


Short:
Write C++ program Code Segment for the following:
(a) To calculate equivalent of two resistance connected in series?
Program:
#include<iostream>
using namespace std;
main()
{
float R1,R2,Req;
cout<<"Enter the value of Resistance 1:\n";
cin>>R1;
cout<<"Enter the value of Resistance 2:\n";
cin>>R2;
Req=R1+R2;
cout<<"The equivalent resistance in series combination of same resistance =:\n"<<Req;
return 0;
}

P a g e -46 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


(b) To print “x” aginst “f(X)” for the equation
𝟓
f(x)=
√(𝟐𝒙𝟑 −𝟒𝒙𝟐 −𝟐𝒙+𝟒)

Program: (simple)
#include<iostream>

#include<math.h>

using namespace std;

main()

float x,y;

float fx;

cout<<"enter the value of x:\n";

cin>>x;

y=2*pow(x,3)-4*pow(x,2)-2*x+4;

fx=5/sqrt(y);

cout<<"Value of x:"<<x<<" & corresponding function="<<fx;

return 0;

Program: (using array)


#include<iostream>

#include<math.h>

using namespace std;

main()

int i,j,k,s;

cout<<"enter the size of array :\n";

cin>>s;

float x[s],y[s],z[s];

for (i=0;i<s;i++)

cout<<"Enter The values of X"<<i<<endl;

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]);

cout<<"Values of X is="<<x[j]<<"\t and f(x)="<<z[j]<<endl;

P a g e -47 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


return 0;

(c) To Calculate and display the table of number ?


Program: (Repeat 2017 a)
#include<iostream>
using namespace std;
main()
{
int n,i;
cout<<"Enter the number:\n";
cin>>n;
cout<<"table of number '"<<n<<" 'is:\n";
for (i=1;i<=10;i++)
{
cout<<n<<"x"<<i<<"="<<n*i<<endl;
}
return 0;
}
(d) To convert from gram to kilogram using a function ?
Program:
#include<iostream>

using namespace std;

float kg(float gram)

return (gram/1000);

main()

float g;

cout<<"enter the value in gram:\n";

cin>>g;

cout<<"print value in kilogram:="<<kg(g);

return 0;

Long Question 2019


𝟔 𝟏
Q#3 Write C++ program to evaluate the ∫𝟏 𝒙𝟑 −𝟓
𝒅𝒙 by Simpson’s (1/3) Rule or by Trapezoidal 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;
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));
}

P a g e -49 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


}
I=(h/3)*sum;
cout<<"Definite Integration:\t"<<I<<endl;
break;
case 2:
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;
default:
{
cout<<" error :Invalid option"<<endl;
break;
}
}
return 0;
}

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;

P a g e -50 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


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++)
{
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;

P a g e -51 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
D[i][j]=7*A[i][j]-3*B[i][j];
C[i][j]=A[j][i];
}
}
cout<<" Result of 7A-3B is:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<D[i][j]<<"\t";
}
cout<<endl;
}
cout<<" Sum of C=2At is:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<2*C[i][j]<<"\t";
}
cout<<endl;
}
return 0;
}

Q#5 (a)

P a g e -52 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA


Write C++ Program for the forced Harmonic Motion (F.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, damping coefficient=0.5 N/ms, w=0.01s-1 and fo=1.5N.).
Calculate and Print values for time , Position and velocity and aceleration. How u can change the same
program for Simple harmonic motion,

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)

Typed By: Mha Physics


Institution: Govt. Post Graduate College Sheikhupura.
E-Mail: [email protected]
Blogger: http://mhaphysics.blogspot.com
You Tube Channel Name: Mha Physics
Facebook page & group: Mha Physics
Published Date: 15/October/2020
Remember In Your Prayers.

P a g e -55 | (Mha Physics) GOVT. POST GRADUATE COLLEGE, SHEIKHUPURA

You might also like