Hi im a beginner at this and i need help to write a program that reads in the length of a square from the user, and computes the area, perimeter, and diagonal of the square. Thanks
What have you done so far? Us doing your homework for you wont help you, it will just prevent you from understanding what you are doing. Just work out your algorithms for each of those, and then put them into your program. For example, your algorithms might be this:
Perimeter = 4 * side
Area = side * side
Diagonal = square_root_of_2 * side
OK, for your reference, look up Pythagoras' theorem. I just wrote a simplified version there, as it prevents work that would need to be done by the computer. In its main form, Pythagoras' theorem is that if you have a right angled triangle, the square of the hypotenuse (the longest side, opposite the right angle) is equal to the square of the other two sides. Say you had a right angled triangle h, a, b, it could be shown like this:
h2 = a2 + b2
I just simplified it by square rooting both sides and simplifying based on the lengths of a side in a square being equal.
Youre probably going to want to use the <math.h> library as well as it contains pow(to find an exponent e.g 2^2 and sqrt (to find the square root of a number)
Example code
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <math.h> // pow and sqrt
usingnamespace std;
int main()
{
cout << pow(7, 3); // Same as 7^3
cout << sqrt(100); // square root of 100
}