You are on page 1of 25

NORTH EASTERN MINDANAO STATE UNIVERSITY

BS CIVIL ENGINEERING
ES55 – NUMERICAL SOLUTION TO CE PROBLEMS
Solved Computer Problems

1. The relationship between the sides (a, b) of a right triangle and the
hypotenuse (h) is given by the Pythagorean formula a2 + b2 = h2. Write a
program that reads in the lengths of the two sides of a right triangle and
computes the hypotenuse of the triangle.

Source code:
#include<iostream>
#include<math.h>
#include<cmath>
using namespace std;
int main()
{
cout << "PHYTAGOREAN THEOREM ";

float a, b, c;

cout << "\n\n\nEnter Value of a: ";


cin >> a;

cout << "\nEnter Value of b: ";


cin >> b;

int a2 = pow(a, 2);


int b2 = pow(b, 2);
c = sqrt(a2 + b2);

cout << "\n\nHypothenuse: " << c;

cout << "\n\n\n";


cout << " ------------------------- \n";
cout << "*****JESIMIE A. ORIAS*****\n";
cout << " ------------------------- \n";
cout << " CP01 ";

return 0;
}
Print Screen:

2. The area of a triangle whose sides are a, b, and c can be computed by the
formula A = s(s − a) (s −b) (s − c) where s = (a + b + c)/2. Write a program
that reads in the lengths of the three sides of a triangle and outputs the area
of the triangle.

Source code:
#include<iostream>
#include<math.h>
#include<cmath>
using namespace std;
int main()
{
cout << "AREA OF A TRIANGLE ";

float a, b, c, s, area;

cout << "\n\n\nEnter Value of A: ";


cin >> a;

cout << "\nEnter Value of B: ";


cin >> b;

cout << "\nEnter Value of C: ";


cin >> c;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));

if (a + b > c || b + a > c)
{
if (a + c > b || c + a > b)
{
if (c + b > a || b + c > a)
{
cout << "\n\nArea = " << area;
}
}
}
if (a + b <= c)
{
cout << "Invalid Inputs! ...";
}
if (a + c <= b)
{
cout << "Invalid Inputs! ...";
}
if (c + b <= a)
{
cout << "Invalid Inputs! ...";
}

cout << "\n\n\n";


cout << " ------------------------- \n";
cout << "*****JESIMIE A. ORIAS*****\n";
cout << " ------------------------- \n";
cout << " CP02 ";

return 0;
}
Print Screen:

3. Write a program that gives the user the choice of computing the area of any of
the following: a circle, a square, a rectangle, or a triangle. The program should
include a loop to allow the user to perform as many calculations as desired.
Use a procedure for each of the different kinds of calculations.

Source Code:
#include<iostream>
#include<math.h>
#include<cmath>
using namespace std;
int main()
{
float a, b, c, radius, s, length, area;

int ch;

char x = 'y';
if (x == 'y');

cout << "\t\tFIND THE AREA";


cout << "\n\t1. Circle";
cout << "\n\t2. Square";
cout << "\n\t3. Rectangle";
cout << "\n\t4. Triangle\n";
cout << "\n Enter Your Choice: ";
cin >> ch;

switch (ch)
{
case 1:
{
cout << "\n\tEnter Radius of the Circle: ";
cin >> radius;

area = 3.14*radius*radius;

cout << "\n\n\t\tArea of the Circle = " << area;


break;
}
case 2:
{
cout << "\n\tEnter Length of the Side of the Square: ";
cin >> length;

area = length*length;

cout << "\n\n\t\tArea of the Square = " << area;


break;
}
case 3:
{
cout << "\n\tEnter Length of the Rectangle: ";
cin >> a;
cout << "\n\tEnter Breadth of the Rectangle: ";
cin >> b;

area = a*b;

cout << "\n\n\t\tArea of the Rectangle = " << area;


break;
}
case 4:
{
cout << "\n\tEnter First Side of the Triangle: ";
cin >> a;
cout << "\n\tEnter Second Side of the Triangle: ";
cin >> b;
cout << "\n\tEnter Third Side of the Triangle: ";
cin >> c;

s = (a+b+c)/2;
area = sqrt (s*(s-a)*(s-b)*(s-c));
cout << "\n\n\t\tArea of the Triangle = " << area;
break;
}
default:
cout << "\n Invalid Choice, Try Again...";
break;

cout << "\n\nWould you like to try again? ";


cin >> x;
if (x == 'n');
system ("\npause");

cout << "\n\n\n";


cout << " ------------------------- \n";
cout << "*****JESIMIE A. ORIAS*****\n";
cout << " ------------------------- \n";
cout << " CP03 ";

return 0;
}

Print Screen:
4. Write a program to calculate the sum: 1+ 4 + 7 +…+100.

Source Code:
#include<iostream>
#include<math.h>
#include<cmath>
using namespace std;
int main()
{
int difference = 3;
int sum = 0;

cout<<"\nCALCULATE THE SUM: 1+ 4 + 7 + ? + 100. ";


cout<<"\n\n";

for(int i=1; i<=100; i = i+ difference)


{
sum=sum+i;

if (i != 100)
cout << i << " + ";

else
cout << i << " = " << sum;
}

cout << "\n\nThe Sum is: " << sum;

cout << "\n\n\n";


cout << " ------------------------- \n";
cout << "*****JESIMIE A. ORIAS*****\n";
cout << " ------------------------- \n";
cout << " CP04 ";

return 0;
}
Print Screen:

5. Write a program to list the numbers from 0 to 25, their squares, square roots,
fourth power, and fourth root. The output should be in a neat five column
format.

Source Code:
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
int i;

cout << "\n\t\t\t\t\t\t\tNUMBERS\n\n\n";

cout << setw (20) << "Squares";


cout << setw (28) << "Square Roots";
cout << setw (30) << "4th Power";
cout << setw (30) << "4th Root\n";

for (int i = 0; i <= 25; i++ )


{
cout << setw (2) << i;
int p = i*i;

cout << setw (23) << p;


float sq = sqrt (i);

cout << setw (28) << setprecision (4) << sq;


int f = pow (i, 4);

cout << setw (30) << f;


float ss = sqrt (sqrt (i));

cout << setw (30) << setprecision (4) << ss;


cout << endl;
}

cout << "\n\n\n";


cout << " ------------------------- \n";
cout << "*****JESIMIE A. ORIAS*****\n";
cout << " ------------------------- \n";
cout << " CP05 ";

return 0;
}
Print Screen:

6. Write a program that reads in a number N and then outputs the sum of the
squares of the numbers from 1 to N. If the input is 3, for example, the output
should be 14, because 12 + 22 + 32 = 1 + 4 + 9 = 14. The program should allow
the user to repeat this calculation as often as desired.

Source Code:
#include <iostream>
#include <math.h>
#include <cmath>
using namespace std;
int main()
{
float s;
float i;
float n;

char a = 'y';
if (a == 'y');

cout << "\nNUMBERS: SQUARE and its SUM";

cout << "\n\nEnter a Number: ";


cin >> n;

cout << "\nNumber of Terms: ";


for (float i = 0; i <= n; i++)
{
cout << i << " , ";
}

cout << "\n\nSquare of Terms: ";


for (float i = 0; i <= n; i++)

{
cout << i*i << " + ";
s += (float)i * (float)i;
}

cout << "\n\nThe Sum of " << n << " Terms = " << s;

cout << "\n\n\nWould you like to try again? ";


cin >> a;
if (a == 'n');
system ("\npause");

cout << "\n\n\n";


cout << " ------------------------- \n";
cout << "*****JESIMIE A. ORIAS*****\n";
cout << " ------------------------- \n";
cout << " CP06 ";

return 0;
}
Print Screen:

7. Write a program to compute the factorial of a number (N!). For example, 3! = 1


x 2 x 3 = 6. By convention, 0! is set equal to 1.

Source Code:
#include <iostream>
#include <math.h>
#include <cmath>
using namespace std;
int main()
{
int n;
long double factorial = 1.0;

cout << "FACTORIALS!";

cout << "\n\nEnter a Positive Integer: ";


cin >> n;
cout << "\nFinding " << n << "! ";

cout << "\n\nTerms: ";


for (int i =1; i <= n; i++)
{
cout << i << " * ";
factorial *= i;
}
cout << "\n\n";
cout << n << "! = " << factorial;

if (n < 0)
cout << "ERROR! Factorial of a Negative Number Does Not Exist.";

cout << "\n\n\n";


cout << " ------------------------- \n";
cout << "*****JESIMIE A. ORIAS*****\n";
cout << " ------------------------- \n";
cout << " CP07 ";

return 0;
}

Print Screen:

8. A perfect number is a positive integer that is equal to the sum of all those
positive integers (excluding itself) that divide it evenly. The first perfect
number is 6, because its divisors (excluding itself) are 1, 2, 3, and because
6 = 1 + 2 + 3. Write a program to find the first three perfect numbers (include
6 as one of the three).

Source Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
int i = 1.0;
int j = 1.0;
int sum = 0.0;

cout << "PERFECT NUMBERS";


cout << "\n\nThe First Three Perfect Numbers:";
cout << "\n";

while (i <= 500)


{
while (j <= 500)
{
if (j < i)
{
if (i % j == 0)
sum = sum + j;
}
j++;
}
if (sum == i)
{
cout << setw (9) << i << " ";
}
i++;
j = 1;
sum = 0;
}

cout << "\n\n\n";


system ("pause");

cout << "\n\n\n";


cout << " ------------------------- \n";
cout << "*****JESIMIE A. ORIAS*****\n";
cout << " ------------------------- \n";
cout << " CP08 ";

return 0;
}
Print Screen:

9. Write a program to find all integer solutions to the equation 4x + 3y – 9z = 5


for values of x, y, and z between 0 and 100.

Source Code:
#include <iostream>
#include <iomanip>
#include <windows.h>
using namespace std;
int main()
{
int x;
int y;
int z;
int i = 0.0;

cout << "\n\t\t\t\t\tALL INTEGER SOLUTION FOR: 4x + 3y - 9z = 5\n\n";

for (x = 0; x <= 100; x++)


for (y = 0; y <= 100; y++)
for (z = 0; z <= 100; z++)
if (4 * x + 3 * y - 9 * z == 5)
{
i++;
cout << "(" << x << ", " << y << ", " << z << ")";
cout << (i % 11? " " : "\n");
}
cout << "\n\n\n\t\tNumber of Solutions: " << i;

cout << "\n\n\n";


system ("pause");

cout << "\n\n\n";


cout << " ------------------------- \n";
cout << "*****JESIMIE A. ORIAS*****\n";
cout << " ------------------------- \n";
cout << " CP09 ";

return 0;
}

Print Screen:
10. The value e^e can be approximated by the sum: 1 + x +x^2/2! + x^3/3! + … +
x^n/n!.

Source Code:
#include <iostream>
#include <math.h>
#include <cmath>
using namespace std;
int main()
{
int x;
int y;
long double j;
long double s = 1.0;
long double r = 1.0;

char a = 'y';
if (a == 'y');

cout << "\nTHE VALUE OF e^x\n\n\n";

cout << "Enter the value of x: ";


cin >> x;
cout << "\nEnter the value of y: ";
cin >> y;

j = x;
cout << "\n\nYour Terms:";
for (int i = 1; i <= y; i++)
{
if (i == 1)
{
cout << i << " + ";
}
if (i < y)
{
cout << j << "/" << s << " + ";
r += j/s;
j *= x;
s *= (i +1);
}
if (i == y)
{
cout << j << " + " << s;
r += j/s;
j *= x;
s *= (i + 1);
}
}
cout << "\n\nApproximated Value of e^x = " << r;

cout << "\n\n\nWould you like to try again? ";


cin >> a;
if (a == 'n');
system ("\npause");

cout << "\n\n\n";


cout << " ------------------------- \n";
cout << "*****JESIMIE A. ORIAS*****\n";
cout << " ------------------------- \n";
cout << " CP10 ";

return 0;
}
Print Screen:

11. Write a program that reads in a real number (representing that many inches)
and then outputs the area of a square with sides of that length and the area
of a circle with a diameter of that length. Use two procedures to compute the
two areas.

Source Code:
#include <iostream>
#include <math.h>
#include <cmath>
using namespace std;
int main()
{
float radius, length, diameter, area;

cout << "\nTHE AREA of a SQUARE and a CIRCLE";

cout << "\n\n\nAREA OF A SQUARE\n";


cout << "\nEnter Side of the Square: ";
cin >> length;

area = length * length;

cout << "Area of the Square = " << area;

cout << "\n\n\nAREA OF A CIRCLE\n";


cout << "\nEnter Diameter of the Circle: ";
cin >> diameter;

radius = diameter/2;
cout << "Radius = " << radius;

area = 3.14 * radius * radius;


cout << "\nArea of the Circle = " << area;

cout << "\n\n\n";


cout << " ------------------------- \n";
cout << "*****JESIMIE A. ORIAS*****\n";
cout << " ------------------------- \n";
cout << " CP11 ";

return 0;
}

Print Screen:
12. Write a program that reads 10 integers into an array, computes the average,
largest, and smallest numbers in the array.

Source Code:
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
int main()
{
int a = 10;
int arr[a];
int sum = 0;

int maximum = INT_MIN;


int minimum = INT_MAX;

cout << "\nARRAY OF 10 INTEGERS";


cout << "\n\nEnter 10 Integers: ";
cout << "\n";
for (int i = 1; i <= 10; i++)
{
cout << setw(8) << i << ". ";
cin >> arr[i];

if (maximum < arr[i])


maximum = arr[i];

if (minimum > arr[i])


minimum = arr[i];

sum += arr[i];
}

cout << "\n\nLargest Number: " << maximum;


cout << "\nSmallest Number: " << minimum;
cout << "\nAverage: " << (sum/a);

cout << "\n\n\n";


cout << " ------------------------- \n";
cout << "*****JESIMIE A. ORIAS*****\n";
cout << " ------------------------- \n";
cout << " CP12 ";

return 0;
}
Print Screen:

13. Write a program that allows the user to type in up to 10 positive numbers and
then echoes back the numbers typed in but in reverse order.

Source Code:
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
int main()
{
int a = 10;
int arr[a];
int sum = 0;

int maximum = INT_MIN;


int minimum = INT_MAX;

cout << "\nARRAY OF 10 INTEGERS";


cout << "\n\nEnter 10 Integers:";
cout << "\n";

for (int i = 1; i <= 10; i++)


{
cout << setw(8) << i << ". ";
cin >> arr[i];
}
cout << "\n\nArray in Reverse Order: ";
for (int i = a; i >= 0; i--)
{
cout << arr[i] << " ";
}

cout << "\n\n\n";


cout << " ------------------------- \n";
cout << "*****JESIMIE A. ORIAS*****\n";
cout << " ------------------------- \n";
cout << " CP13 ";

return 0;
}

Print Screen:

14. Write a program that reads in an m by n matrix (m row by n column), then


reads in an n by p matrix (n row by p column), and then computes the product
matrix and displays the two matrices as well as their product matrix on the
screen.
Source Code:
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;

int main()
{
int x;
int y;
int z;

int a[10][10];

cout<<"\nMATRIX!\n\n";
cout<<"Enter the Number of Rows: ";
cin>>x;
cout<<"\nEnter the Number of Columns in MATRIX A: ";
cin>>y;
cout<<"\nEnter the Number of Columns in MATRIX B: ";
cin>>z;

cout<<setw(1)<<"\n\nMATRIX A\n";
for (int i = 1; i <= x; i++)
{
for (int e = 1; e <= y; e++)
{
cout<<"Row "<<i<<setw(3)<<" "<<"Column "<<e<<" : ";
cin>>a[i][e];
}
}
cout<<"\n\nMATRIX B\n";
int b[10][10];
for (int i = 1; i <= y; i++)
{
for (int e = 1; e <= z; e++)
{
cout<<"Row "<<i<<setw(3)<<" "<<"Column "<<e<<" : ";
cin>>b[i][e];
}
}
int c[10][10];
cout<<"\n\nMULTIPLICATION OF MATRIX A & MATRIX B : ";
for (int i = 1; i <= x; i++)
{
for (int e = 1; e <= z; e++)
{
c[i][e] = 0;
for (int j = 1; j <= y; j++)
c[i][e] += a[i][j] * b[j][e];
}
}
cout << "\n" << "Output Matrix: " <<"\n";
for(int i = 1; i <= x; i++)
for(int e = 1; e <= z; e++)
{
cout << " " << c[i][e];
if(e == z)
cout << "\n";
}

cout << "\n\n\n";


cout << " ------------------------- \n";
cout << "*****JESIMIE A. ORIAS*****\n";
cout << " ------------------------- \n";
cout << " CP14 ";

return 0;
}

Print Screen:

You might also like