write an algorithm which asks for length of the side of a square validate it to see that it is a value between 1 and 100 and then calculate and display its perimeter otherwise give ERROR message
Answers
Answer:
- start
- declar value length
- take user input
- put user input in length value
- check if length is between 1 and 100
- display error if statement is false
- stop if statement is false
- continue if statement is true
- display 4 x length as parameter
- stop
Explanation:
The algorithm can be written in different programming languages.
python3 algorithm for finding perimeter of square-
# taking user input
length=float(input("Enter the length of the side of your square: "))
# checking number
if length>=1 and length<=100:
# printing output
print("Perimeter of your square is "+str(4*length))
else:
print("ERROR: please enter valid value between 1 and 100")
c++ algorithm for finding perimeter of square-
#include <iostream>
using namespace std;
int main(){
// defining veriable length
double lenght;
// asking user for input
cout<<"Enter the length of the side of your square: ";
// taking user input
cin>>lenght;
// checking number
if (lenght>=1 and lenght<=100)
{
// giving output
cout<<"Perimeter of your square is "<<4*lenght<<endl;
}else{
cout<<"ERROR: please enter valid value between 1 and 100"<<endl;
};
}
program output:
$python3 algorithm.py
Enter the length of the side of your square: 76.35
Perimeter of your square is 305.4
$g++ algorithm.c++ -o exe && ./exe
Enter the length of the side of your square: 46.47
Perimeter of your square is 185.88
$python3 algorithm.py
Enter the length of the side of your square: 200
ERROR: please enter valid value between 1 and 100
$./exe
Enter the length of the side of your square: 345
ERROR: please enter valid value between 1 and 100