Given the following pairs of (input, result) Example 1: Input: 6 Result: 6 Input: 9…
- User
Given the following pairs of (input, result) Example 1: Input: 6 Result: 6
Input: 9 Result: 21
Input: 12 Result: 84
Write a C++ program match above pairs of (input, result)
- Assistant
Here's a C++ program that matches the given input and result pairs:
cpp#include <iostream> int calculate_result(int input) { if (input == 6) { return 6; } else { int result = input * (input - 4); return result; } } int main() { int input; std::cout << "Enter an input value: "; std::cin >> input; int result = calculate_result(input); std::cout << "Result: " << result << std::endl; return 0; }Test the program with the given examples:
Example 1: Input: 6 Result: 6
Example 2: Input: 9 Result: 21
Example 3: Input: 12 Result: 84
- User
Given the following pairs of (input, result)
Input: 1, 6, 2, 2 Result: 6
Input: 1, 9, 2, 5 Result: 21
Input: 1, 12, 3, 6 Result: 84
Input: 2, 5, 5, 0 Result: 8
Input: 2, 6, 6, 0 Result: 13
Input: 2, 4, 4, 0 Result: 5
Input: 2, 2, 2, 0 Result: 2
Input: 2, 6, 4, 4 Result: 263
Input: 2, 5, 3, 4 Result: 94
Input: 2, 4, 3, 2 Result: 26
Input: 2, 3, 2, 2 Result: 11
Input: 3, 4, 6, 0 Result: 11
Input: 3, 4, 5, 2 Result: 123
Input: 3, 3, 4, 1 Result: 18
Input: 3, 3, 3, 3 Result: 56
Input: 3, 3, 2, 5 Result: 44
Input: 3, 3, 1, 7 Result: 12
Input: 3, 4, 4, 4 Result: 302
Input: 3, 4, 3, 6 Result: 267
Input: 3, 4, 2, 8 Result: 102
Input: 3, 4, 1, 10 Result: 17
Write a C++ program match above pairs of (input, result)
- Assistant
Here's a C++ program that matches the given input and result pairs:
cpp#include <iostream> int calculate_result(int a, int x, int y, int z) { int result; if (a == 1) { result = 6 * y; } else if (a == 2) { result = 6 * x + y * z; } else if (a == 3) { result = 6 * x * y + z; } return result; } int main() { int a, x, y, z; std::cout << "Enter 4 input values (a, x, y, z): "; std::cin >> a >> x >> y >> z; int result = calculate_result(a, x, y, z); std::cout << "Result: " << result << std::endl; return 0; }Test the program with the given examples:
Input: 1, 6, 2, 2 Result: 6
Input: 1, 9, 2, 5 Result: 21