In: Computer Science
A program is required to compare three numbers to see if any of them is equal to the product of the other two.
The program will take three integers, between 1 and 100 inclusive.
The program should output one of the following strings:
You are given the following test data and an incomplete and error-filled program.
Test Number |
Inputs |
Expected Output | ||
---|---|---|---|---|
a | b | c | ||
1 | 6 | 2 | 3 | a = b * c |
2 | 3 | 6 | 2 | b = a * c |
3 | 3 | 2 | 6 | c = a * b |
4 | 7 | 8 | 9 | No number is product of the others |
# Problem: Check if any of three numbers is product of others # Input: a as an integer from 1 to 100 # Input: b as an integer from 1 to 100 # Input: c as an integer from 1 to 100 a = 6 b = 2 c = 3 # Output: answer, a string if a == b + c answer = 'a = b * c' if b == a * c answer = 'b = a * c' if c == a + b : answer = 'c = a * b' if (a != b * c) and (b != a * c) and (c != a * b): answer = 'No number is product of others' print (answer)
Code:
#include <iostream>
using namespace std;
int main()
{
int a,b,c; //Variable declaration
cout<<"Enter 1st number a= ";
cin>>a; //take integer input from user as a
cout<<"Enter 2nd number b= ";
cin>>b; //take integer input from user as b
cout<<"Enter 3rd number c= ";
cin>>c; //take integer input from user as c
if((a>=1 && a<=100) && (b>=1 &&
b<=100) && (c>=1 && c<=100)) //check
weather the numbers are between 1 to 100
{
if(a==b*c) //if a is the product of b and c
cout<<"Result: a=b*c"; //print result
else if(b==a*c)
cout<<"Result: b=a*c"; //if b is the product of a and c
else if(c==a*b)
cout<<"Result: c=a*b"; //if c is the product of a and b
else
cout<<"No number is product of others";
}
else
cout<<"Number is not between 1 and 100";
return 0;
}
Output:
code in Python
a = int(input("Enter first number a = "))
b = int(input("Enter first number b = "))
c = int(input("Enter first number c = "))
if a >= 1 and a <= 100 and b >= 1 and b <= 100 and c
>= 1 and c <= 100:
if a == b*c:
print("Result a=b*c")
elif b == a*c:
print("Result b=a*c")
elif c == a*b:
print("Result c=a*b")
else:
print("No number is product of
others")
else:
print("Number is not between 1 and 100")