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

Muhammad Bilal

The document contains 30 programs written in C++ to solve computational physics problems. The programs cover topics like calculating the circumference of a circle, simple interest, mathematical operations, compound assignment operations, checking even/odd numbers, positive/negative numbers, grading based on test scores, calculating total mass of a system, and other physics related calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Muhammad Bilal

The document contains 30 programs written in C++ to solve computational physics problems. The programs cover topics like calculating the circumference of a circle, simple interest, mathematical operations, compound assignment operations, checking even/odd numbers, positive/negative numbers, grading based on test scores, calculating total mass of a system, and other physics related calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Lab Report of Computational Physics

Submitted to: Professor Dr. Iqbal Hussain

Submitted by: Muhammad Bilal

Roll Number: Bsf2004891

Department: BS Physics

Subject: Nuclear Physics

University of Education Lahore


Multan Campus
Table of Contents
Program 1: Write a C++ program that inputs the radius of a circle and displays circumference.
Using formula 2πr. Store the value of π by using define directory.................................................................3
Program 2: Write a program to find simple interest by using formula: Simple interest = principal
amount × rate of interest × no. of years /100............................................................................................................4
Program 3: Write a program that performs all mathematical operations on two variables..............5
Program 4: Write a program that performs all compound assignment operations. Methodology. 6
Program 5: Write a program to check whether a number is even or odd? Using if else statements.
........................................................................................................................................................................................................ 7
Program 6: Write a program to check whether a number is positive or negative? Using if else
statements................................................................................................................................................................................. 8
Program 7: Write a program that enters the test score of a student and displays his grade
according to the following criteria test score grade...............................................................................................9
Program 8: Write a program to calculate the total mass of the system containing four masses.
Tm=m1+m2+m3+m4........................................................................................................................................................ 10
Program 9: Write a program that gets two numbers from the user and displays the result of the
first number and raises the power of the 2nd number using a do-while loop.........................................11
Program 10: Write a program in C++ to display the cube of the number up to a given integer...12
Program 11: Write a program that inputs a character by the user and check whether it is vowel
or consonant.......................................................................................................................................................................... 13
Program 12: Write a program to sum the odd and even number from 1 to upper bound...............14
Program 13: Write a program to calculate the horizontal and vertical displacement covered by
projectiles............................................................................................................................................................................... 15
Program 14: Write a C++ program to check a prime number......................................................................17
Program 15: Write a program to solve the expression a * b / (-c * 31 % 13) * d..................................18
Program 16: Write a program to input any year print out whether it is leap year or not a year,
occurring once every four years, which has 366 days including 29 February as an intercalary day.
..................................................................................................................................................................................................... 19
Program 17: Write a program to solve the 3rd equation of motion using supposed values?..........20
Program 18: Write a C++ program to take inputs from a user and store them into an array?.....21
Program 19: Write a program to create a dynamic array?............................................................................22
Program 20: Write a program to find the largest number..............................................................................23
Program 21: Write a c++ program to check whether the character is an uppercase or lowercase
alphabet. Using the if-else statement.........................................................................................................................24
Program 22: Write a c++ program that displays first three numbers and their sum using while
loop............................................................................................................................................................................................ 25
Program 23: Write a c++ program to check whether the user defined time is AM or PM..............26
Program 24: Write a c++ program to find the cube root of a defined number.....................................27
Program 25: Write a c++ program to input two numbers, print out their contents before and
after exchange without using a third variable.......................................................................................................28
Program 26:Write the c++ program to swap the user define any number..............................................29
Program 27: Write the c++ program to find the factorial of a number....................................................30
Program 28: Write the c++ program to calculate the radius of a circle. R2=X2+Y2..........................31
Program 29: Write a c++ program to calculate the value of Force by applying coulomb’s law....32
Program 30: Write a c++ program to calculate the value of Force by applying gravitational law.
..................................................................................................................................................................................................... 33
Program 31: Write a c++ program to calculate the value of current by applying I=(Io) /(r*r).....34
Program 32: Write a c++ program to calculate cube root of a defined number...................................35
Program 1: Write a C++ program that inputs the radius of a circle
and displays circumference. Using formula 2πr. Store the value of π
by using define directory.

Input

#include <iostream>
#define pi 3.14
using namespace std;
int main()
{
int R, Circum;
cout << "Enter the value of radius=";
cin >> R;
Circum = 2 * pi * R;
cout << "Circumference of the circle=" << Circum << "cm";
return 0;
}

Output

Enter the value of radius=5


Circumference of the circle=31cm
Program 2: Write a program to find simple interest by using
formula: Simple interest = principal amount × rate of interest × no.
of years /100.

Input

#include <iostream>
using namespace std;
int main()
{
float pa, r, n, SI;
cout << "Enter the value Principal amount=";
cin >> pa;
cout << "Rate of interest=";
cin >> r;
cout << "No. of years=";
cin >> n;
SI = pa * r * n / 100;
cout << "Simple interest=" << SI << " rupees";
return 0;
}

Output

Enter the value Principle amount=3000


Rate of interest=5
No. of years=5 Simple interest=750 rupees
Program 3: Write a program that performs all mathematical
operations on two variables.

Input

#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Enter the value of a =";
cin >> a;
cout << "Enter the value of b =";
cin >> b;
cout << "a+b=" << a + b << endl;
cout << "a-b=" << a - b << endl;
cout << "a*b=" << a * b << endl;
cout << "a/b=" << a / b << endl;
cout << "a%b=" << a % b << endl;
return 0;
}

Output

Enter the value of a =6


Enter the value of b =7
a+b=13
a-b=-1
a*b=42
a/b=0
a%b=6
Program 4: Write a program that performs all compound
assignment operations. Methodology

Input

#include <iostream>
#define PI 3.14
using namespace std;
int main()
{
int a;
cout << "Enter the value of an integer=";
cin >> a;
cout << "Value of a after a+ =" << a << endl;
a -= 5;
cout << "Value of a after a- =" << a << endl;
a *= 5;
cout << "Value of a after a* =" << a << endl;
a /= 5;
cout << "Value of a after a/ =" << a << endl;
return 0;
}

Output

Enter the value of an integer=10


Value of a after a+ =10
Value of a after a- =5
Value of a after a* =25
Value of a after a/ =5
Program 5: Write a program to check whether a number is even or
odd? Using if else statements.

Input

#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter the value of n=";
cin >> n;
if (n % 2 == 0)
cout << n << " is an even number";
else
cout << n << " is an odd number";
return 0;
}

Output

Enter the value of n=56


56 is an even number
Program 6: Write a program to check whether a number is positive
or negative? Using if else statements.

Input

#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter any integer=";
cin >> n;
if (n > 0)
cout << n << " is positive number";
else
cout << n << " User defined number is negative ";
return 0;
}

Output

Enter any integer=54


54 is positive number
Program 7: Write a program that enters the test score of a student
and displays his grade according to the following criteria test score
grade.

Input

#include <iostream>
using namespace std;
int main()
{
int score;
cout << "Enter your test score=";
cin >> score;
if (score >= 90)
cout << " Your grade is A ";
else if (score >= 80)
cout << " Your grade is B ";
else if (score >= 70)
cout << " Your grade is C ";
else if (score > 60)
cout << " Your grade is D ";
else
cout << " Sorry! You are fail ";
return 0;
}

Output

Enter the your test score=67


Your grade is D
Program 8: Write a program to calculate the total mass of the
system containing four masses. Tm=m1+m2+m3+m4.

Input

#include <iostream>
using namespace std;
int main()
{
int Tm, m1, m2, m3, m4;
cout << "Enter the value of m1=";
cin >> m1;
cout << "Enter the value of m2=";
cin >> m2;
cout << "Enter the value of m3=";
cin >> m3;
cout << "Enter the value of m4=";
cin >> m4;
Tm = m1 + m2 + m3 + m4;
cout << " Total mass =" << Tm;
return 0;
}

Output

Enter the value of m1=57


Enter the value of m2=47
Enter the value of m3=43
Enter the value of m4=89
Total mass =236
Program 9: Write a program that gets two numbers from the user
and displays the result of the first number and raises the power of
the 2nd number using a do-while loop.

Input

#include <iostream>
using namespace std;
int main()
{
int a, b, c, r;
cout << "Enter first number=";
cin >> a;
cout << "Enter second number=";
cin >> b;
c = 1;
r = 1;
do
{
r = r * a;
c = c + 1;
} while (c <= b);
cout << "Result=" << r;
return 0;
}

Output

Enter first number=5


Enter second number=2
Result=25
Program 10: Write a program in C++ to display the cube of the
number up to a given integer.

Input

#include <iostream>
using namespace std;
int main()
{
int i, term, cube;
cout << "Input the number of terms : ";
cin >> term;
for (i = 1; i <= term; i++)
{
cube = i * i * i;
cout << " The cube of " << i << " is = " << cube << endl;
}
return 0;
}

Output

Input the number of terms : 3


The cube of 1 is = 1
The cube of 2 is = 8
The cube of 3 is = 27
Program 11: Write a program that inputs a character by the user
and check whether it is vowel or consonant.

Input

#include <iostream>
using namespace std;
int main() {
char c;
cout << "Enter a character: ";
cin >> c;
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
cout << c << " is a vowel.";
}
else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
cout << c << " is a consonant.";
}
else {
cout << "Invalid input: Not an alphabet.";
}
return 0;
}

Output

Enter an character: s
s is a consonant
Program 12: Write a program to sum the odd and even number
from 1 to upper bound.

Input

#include <iostream>
using namespace std;
int main()
{
int sumOdd = 0;
int sumEven = 0;
int Upperbound;
cout << "Enter the value of Upper Bound=";
cin >> Upper Bound;
int number = 1;
while (number <= Upper Bound)
{
if (number % 2 == 0)
{
sumEven = sumEven + number;
}
else
{
sumOdd = sumOdd + number;
}
++number;
}
cout << "The sum of the even number=" << sumEven << endl;
cout << "The sum of the odd number=" << sumOdd << endl;
cout << "Difference between even and odd number=" << sumEven - sumOdd;
return 0;
}

Output

Enter the value of Upper Bound=4


The sum of the even number=6
The sum of the odd number=4
Difference between even and odd number=2
Program 13: Write a program to calculate the horizontal and
vertical displacement covered by projectiles.

Input

#include <iostream>
#include <cmath>
using namespace std;

const double PI = 3.141592654;


const double g = -10;

int main() {
double launch_angle, Vo, Vx, Vy, time, x, y, y_max;

do {
cout << "Enter initial velocity: ";
cin >> Vo;
} while (Vo < 0 || Vo > 100);

do {
cout << "Enter launch angle (1 to 89 degrees): ";
cin >> launch_angle;
} while (launch_angle < 1 || launch_angle > 89);

Vx = Vo * cos(launch_angle * PI / 180);
Vy = Vo * sin(launch_angle * PI / 180);

cout << "Vx = " << Vx << endl;


cout << "Vy = " << Vy << endl;

do {
cout << "Enter the time: ";
cin >> time;
} while (time < 0);

y_max = (Vo * sin(launch_angle * PI / 180) * Vo * sin(launch_angle * PI / 180)) / (2 * g);


cout << "Y max = " << y_max << endl;
x = Vo * cos(launch_angle * PI / 180) * time;
y = (1 / 2) * g * time * time + Vo * sin(launch_angle * PI / 180) * time;

cout << "x = " << x << endl;


cout << "y = " << y << endl;

return 0;
}

Output

Enter initial velocity =5


Enter Launch Angle =60
Vx =2.5
Vy =4.33013
Enter the time=8
Y max = -93.75
x =20
y =34.641
Program 14: Write a C++ program to check a prime number.

Input

#include <iostream>
using namespace std;

int main() {
int num, i, j = 0;
cout << "Enter number: ";
cin >> num;

for (i = 1; i <= num; i++) {


if ((num % i) == 0) {
j++;
}
}

if (j == 2)
cout << num << " is a prime number.\n";
else
cout << num << " is not a prime number.\n";

return 0;
}

Output

Enter number: 56
56 is not a prime number.
Program 15: Write a program to solve the expression a * b / (-c * 31
% 13) * d.

Input

#include <iostream>
using namespace std;

int main() {
int a, b, c, d, r;
cout << "Enter the value of a: ";
cin >> a;
cout << "Enter the value of b: ";
cin >> b;
cout << "Enter the value of c: ";
cin >> c;
cout << "Enter the value of d: ";
cin >> d;
r = a * b / (-c * 31 % 13) * d;
cout << "Result of the expression is: " << r << endl;
return 0;
}

Output
Enter the value of a =6
Enter the value of b =7
Enter the value of c =8
Enter the value of d =9
Result of the expression is:-378
Program 16: Write a program to input any year print out whether it
is leap year or not a year, occurring once every four years, which
has 366 days including 29 February as an intercalary day.

Input

#include <iostream>
using namespace std;

int main() {
int year;
cout << "Enter the year: ";
cin >> year;
if (year % 4 == 0)
cout << year << " is a leap year" << endl;
else
cout << year << " is not a leap year" << endl;
return 0;
}

Output

Enter a year = 2014


2014 is not a leap year.
Program 17: Write a program to solve the 3rd equation of motion
using supposed values?

Input

#include <iostream>
using namespace std;

int main() {
int a, s, vf, vi;
cout << "Enter the acceleration (a): ";
cin >> a;
cout << "Enter the final velocity (vf): ";
cin >> vf;
cout << "Enter the initial velocity (vi): ";
cin >> vi;
s = (vf * vf - vi * vi) / (2 * a);
cout << "The distance (s) = " << s << endl;
return 0;
}

Output

Enter the a = 6
Enter the vf = 9
Enter the vi = 6
The distance S = 132
Program 18: Write a C++ program to take inputs from a user and
store them into an array?

Input

#include <iostream>
using namespace std;

int main() {
int numbers[5];
cout << "Enter 5 numbers: ";
for (int i = 0; i < 5; ++i) {
cin >> numbers[i];
}
cout << "The numbers are: ";
for (int i = 0; i < 5; ++i) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}

Output

67
87
87
09
98
The number are = 67 87 87 9 98
Program 19: Write a program to create a dynamic array?

Input

#include<iostream>
using namespace std;
int main()
{
int n , x ;
cout<<"Enter the no. of items = ";
cin >>n;
int *arr = new int(n);
cout<<"Enter "<<n<< " items "<<endl ;
for (x=0; x<n ; x++)
{
cin >> arr [x];
}
cout <<"You Entered = ";
for (x=0; x<n ; x++)
{
cout << arr [x]<<" ";
}
return 0;
}

Output

Enter the no. of items = 6


Enter 6 items
6,7,8,9,5,8
You Entered = 6 7 8 9 5 8
Program 20: Write a program to find the largest number

Input

#include <iostream>
using namespace std;
int main()
{
float n1, n2, n3;
cout<<"Enter three numbers"<<endl;
cin>>n1>>n2>>n3;
if (n1>=n2 && n1>=n3)
cout<<"Largest number="<<n1;
if (n2>=n1 && n2>=n3)
cout<<"Largest number="<<n2;
if (n3>=n1 && n3>=n2)
cout<<"Largest number="<<n3;
return 0;
}

Output

Enter three numbers


65
87
98
Largest number=98
Program 21: Write a c++ program to check whether the character is
an uppercase or lowercase alphabet. Using the if-else statement.

Input

#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter any character = ";
cin>>ch;
if (ch>='a' && ch<='z')
{
cout<<ch<<" character is in lowercase ";
}
else if (ch>='A' && ch<='Z')
{
cout<<ch<<" character is in uppercase ";
}
else
{
cout<<"Invalid entry";
}
return 0;
}

Output

Enter any character = R


R character is in uppercase
Program 22: Write a c++ program that displays first three numbers
and their sum using while loop

Input

#include <iostream>
using namespace std;
int main()
{
int i=1, s=0;
while( i<=3 )
{
cout <<i<<endl;
s=s+i;
i++;
}
cout << "Sum of numbers is = " << s << endl;
return 0;
}

Output

1
2
3
Sum of numbers is = 6
Program 23: Write a c++ program to check whether the user
defined time is AM or PM.

Input

#include <iostream>
using namespace std;
int main()
{
int hour;
cout<<"Enter time to check if PM or AM? ="<<endl;
cin>>hour;
if (hour>= 12 && hour < 24)
cout<<"Time is not AM, its PM";
else
{
cout<<"It is AM";
}
return 0;
}

Output

Enter time to check if PM or AM ?= 7


It is AM
Program 24: Write a c++ program to find the cube root of a defined
number.

Input

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float number, ans;
cout << "Enter the number to find its cube root: ";
cin >> number;
ans = pow(number, 1.0/3.0);
cout << "Cube root of " << number << " is " << ans;
return 0;
}

Output

Enter the number to find its cube root: 7


Cube root of 7 is 1.91293
Program 25: Write a c++ program to input two numbers, print out
their contents before and after exchange without using a third
variable.

Input

#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10, temp;
cout << "Before swapping." <<endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout<<endl;
cout << "After swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}

Output

Before swapping.
a = 5, b = 10
After swapping.
a = 10, b = 5
Program 26:Write the c++ program to swap the user define any
number.

Input

#include <iostream>
using namespace std;
int main()
{
int x , swappedNumber, temp;
cout<<"Enter the value of x = ";
cin>>x;
cout << "Before swapping." << endl;
cout << "x = " << x << endl;
temp = (x % 10) * 100 + ((x / 10) % 10) * 10 + (x / 100);
swappedNumber = temp;
cout << endl;
cout << "After swapping." << endl;
cout << "x = " << swappedNumber << endl;
return 0;
}

Output

Enter the value of x = 7


Before swapping.
x=7
After swapping.
x = 700
Program 27: Write the c++ program to find the factorial of a
number.

Input

#include <iostream>
using namespace std;
int factorial(int n)
{
int res = 1;
for(int i = 2;i<=n;i++)
res = res*i;
return res;
}
int main()
{
int a;
cout<<"enter value of a=";
cin>>a;
cout<<"factorial of "<<a<<" is "<<factorial(a)<<endl;
return 0;
}

Output

enter value of a=7


factorial of 7 is 5040
Program 28: Write the c++ program to calculate the radius of a
circle. R2=X2+Y2

Input

#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int d, x, y, R;
cout<<"Enter the value of x=";
cin>>x;
cout<<"Enter the value of y=";
cin>>y;
d =x*x+y*y;
cout<<"The value of R^2 is "<< d <<endl;
R =sqrt(d);
cout<<"The radius R ="<<R;
return 0;
}

Output

Enter the value of x=6


Enter the value of y=4
The value of R^2 is 52
The radius R =7
Program 29: Write a c++ program to calculate the value of Force by
applying coulomb’s law.

Input

#include <iostream>
#include <cmath>
#define k 8.99e+09
using namespace std;
int main()
{
int q1, q2, r, F;
cout<<"Enter the value of q1=";
cin>>q1;
cout<<"Enter the value of q2=";
cin>>q2;
cout<<"Enter the value of radius= ";
cin>>r;
F=(k*q1*q2) /(r*r) ;
cout<<"The value of F ="<<F<<" Nm²C-² ";
return 0;
}

Output

Enter the value of q1=3


Enter the value of q2=4
Enter the value of radius= 5
The value of F =-2147483648 Nm²C-²
Program 30: Write a c++ program to calculate the value of Force by
applying gravitational law.

Input

#include<iostream>
#include<cmath>
#define G 6.67e-11
using namespace std;
int main()
{
float m1,m2,r,F;
cout << "Enter the value of masses=";
cin>>m1>>m2;
cout << "Enter radius=";
cin >> r;
F=G*((m1*m2)/(r*r));
cout << "Value of Gravitational Force is "<<F<<" Nm²C-² ";
return 0;
}

Output

Enter mass 1=5


Enter mass 2=6
Enter radius=10
Value of Gravitational Force is 2.001e-11 Nm²C-²
Program 31: Write a c++ program to calculate the value of current
by applying I=(Io) /(r*r).

Input

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int Io, r, I;
cout<<"Enter the value of Io=";
cin>>Io;
cout<<"Enter the value of radius=";
cin>>r;
I=(Io) /(r*r) ;
cout<<"The value of I ="<<I;
return 0;
}

Output

Enter the value of Io=7


Enter the value of radius=5
The value of I =0
Program 32: Write a c++ program to calculate cube root of a
defined number.

Input

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float number, ans;
cout << "Enter the number to find its cube root: ";
cin >> number;
ans = pow(number, 1.0/3.0);
cout << "Cube root of " << number << " is " << ans;
return 0;
}

Output

Enter the number to find its cube root: 7


Cube root of 7 is 1.91293

You might also like