Muhammad Bilal
Muhammad Bilal
Department: BS Physics
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
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
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
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
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
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
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
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
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
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
#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
Input
#include <iostream>
#include <cmath>
using namespace std;
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);
do {
cout << "Enter the time: ";
cin >> time;
} while (time < 0);
return 0;
}
Output
Input
#include <iostream>
using namespace std;
int main() {
int num, i, j = 0;
cout << "Enter number: ";
cin >> num;
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
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
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
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
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
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
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
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
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
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
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
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
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