Professional Documents
Culture Documents
Solved Computer Problems
Solved Computer Problems
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;
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;
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! ...";
}
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');
switch (ch)
{
case 1:
{
cout << "\n\tEnter Radius of the Circle: ";
cin >> radius;
area = 3.14*radius*radius;
area = length*length;
area = a*b;
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;
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;
if (i != 100)
cout << i << " + ";
else
cout << i << " = " << sum;
}
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;
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 << i*i << " + ";
s += (float)i * (float)i;
}
cout << "\n\nThe Sum of " << n << " Terms = " << s;
return 0;
}
Print Screen:
Source Code:
#include <iostream>
#include <math.h>
#include <cmath>
using namespace std;
int main()
{
int n;
long double factorial = 1.0;
if (n < 0)
cout << "ERROR! Factorial of a Negative Number Does Not Exist.";
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;
return 0;
}
Print Screen:
Source Code:
#include <iostream>
#include <iomanip>
#include <windows.h>
using namespace std;
int main()
{
int x;
int y;
int z;
int i = 0.0;
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');
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;
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;
radius = diameter/2;
cout << "Radius = " << radius;
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;
sum += arr[i];
}
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;
return 0;
}
Print Screen:
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";
}
return 0;
}
Print Screen: