Write a program that calculates the mathematical expression

Solution

Oops! This is a really complex expression. Let’s take a look at an approach that even a newbie can follow.

The main idea is to break the complex expression into smaller, simpler expressions and assign each sub-result to temporary variables. In the end, you can build the original expression out of all these temporary variables! This approach is presented here.

PHP

<?php
  echo "Enter value for x: ";
  $x = trim(fgets(STDIN));
  echo "Enter value for w: ";
  $w = trim(fgets(STDIN));
  echo "Enter value for z: ";
  $z = trim(fgets(STDIN));

  $temp1 = 3 * $x * $x + 5 * $x + 2;  
  $temp2 = 7 * $w - 1 / $z;
  $temp3 = (3 + $x) / 7;
  $nominator = 5 * $temp1 / $temp2 - $z;
  $denominator = 4 * $temp3;

  $y = $nominator / $denominator;

  echo "The result is: ", $y;
?>

Java

public static void main(String[] args) throws java.io.IOException {
  java.io.BufferedReader cin = new java.io.
          BufferedReader(new java.io.InputStreamReader(System.in));
  double denominator, nominator, temp1, temp2, temp3, w, x, y, z;

  System.out.print("Enter value for x: ");
  x = Double.parseDouble(cin.readLine());
  System.out.print("Enter value for w: ");
  w = Double.parseDouble(cin.readLine());
  System.out.print("Enter value for z: ");
  z = Double.parseDouble(cin.readLine());

  temp1 = 3 * x * x + 5 * x + 2;  
  temp2 = 7 * w - 1 / z;
  temp3 = (3 + x) / 7;
  nominator = 5 * temp1 / temp2 - z;
  denominator = 4 * temp3;

  y = nominator / denominator;

  System.out.println("The result is: " + y);
}

C++

#include <iostream>
using namespace std;
int main() {
  double denominator, nominator, temp1, temp2, temp3, w, x, y, z;

  cout << "Enter value for x: ";
  cin >> x;
  cout << "Enter value for w: ";
  cin >> w;
  cout << "Enter value for z: ";
  cin >> z;

  temp1 = 3 * x * x + 5 * x + 2;  
  temp2 = 7 * w - 1 / z;
  temp3 = (3 + x) / 7;
  nominator = 5 * temp1 / temp2 - z;
  denominator = 4 * temp3;

  y = nominator / denominator;

  cout << "The result is: " << y;
  return 0;
}

C#

static void Main() {
  double denominator, nominator, temp1, temp2, temp3, w, x, y, z;

  Console.Write("Enter value for x: ");
  x = Double.Parse(Console.ReadLine());
  Console.Write("Enter value for w: ");
  w = Double.Parse(Console.ReadLine());
  Console.Write("Enter value for z: ");
  z = Double.Parse(Console.ReadLine());

  temp1 = 3 * x * x + 5 * x + 2;  
  temp2 = 7 * w - 1 / z;
  temp3 = (3 + x) / 7;
  nominator = 5 * temp1 / temp2 - z;
  denominator = 4 * temp3;

  y = nominator / denominator;

  Console.Write("The result is: " + y);

  Console.ReadKey();
}

Visual Basic

Sub Main()
  Dim denominator, nominator, temp1, temp2, temp3, w, x, y, z As Double

  Console.Write("Enter value for x: ")
  x = Console.ReadLine()
  Console.Write("Enter value for w: ")
  w = Console.ReadLine()
  Console.Write("Enter value for z: ")
  z = Console.ReadLine()

  temp1 = 3 * x ^ 2 + 5 * x + 2
  temp2 = 7 * w - 1 / z
  temp3 = (3 + x) / 7
  nominator = 5 * temp1 / temp2 - z
  denominator = 4 * temp3

  y = nominator / denominator

  Console.Write("The result is: " & y)

  Console.ReadKey()
End Sub

Python

x = float(input("Enter value for x: "))
w = float(input("Enter value for w: "))
z = float(input("Enter value for z: "))

temp1 = 3 * x ** 2 + 5 * x + 2
temp2 = 7 * w - 1 / z
temp3 = (3 + x) / 7
nominator = 5 * temp1 / temp2 - z
denominator = 4 * temp3

y = nominator / denominator

print("The result is:", y)

You may say, “Okay, but I wasted so many variables and as everybody knows, each variable is a portion of main memory. How can I write the original expression in one single line and waste less memory?

This job may be a piece of cake for an advanced programmer, but what about you? What about a novice programmer?

The next method will help you write even the most complex mathematical expressions without any syntax or logic errors! The rule is very simple. “After breaking the complex expression into smaller, simpler expressions and assigning each sub-result to temporary variables, start backwards and replace each variable with its assigned expression. Be careful though! When you replace a variable with an expression, you must always enclose the expression in parentheses!”

Confused? Don’t be! It’s easier in action. Let’s try to rewrite the previous program. Starting backwards, replace variables nominator and denominator with their assigned expressions. The result is

PHP

php

Java

java

C++

java

C#

java

Visual Basic

java

Python

java

Notice: Please note the extra parentheses added.

Now you must replace variables temp1, temp2, and temp3 with their assigned expressions, and the one-line expression is complete!

PHP

php2

Java

java2

C++

java2

C#

java2

Visual Basic

vb2

Python

python2

It may look scary at the end but it wasn’t that difficult, was it?

The program can now be rewritten

PHP

<?php
  echo "Enter value for x: ";
  $x = trim(fgets(STDIN));
  echo "Enter value for w: ";
  $w = trim(fgets(STDIN));
  echo "Enter value for z: ";
  $z = trim(fgets(STDIN));

  $y = (5 * (3 * $x * $x + 5 * $x + 2)/(7 * $w - 1 / $z) - $z)/(4 * ((3 + $x)/7));

  echo "The result is: ", $y;
?>

Java

public static void main(String[] args) throws java.io.IOException {
  java.io.BufferedReader cin = new java.io.
          BufferedReader(new java.io.InputStreamReader(System.in));
  double w, x, y, z;

  System.out.print("Enter value for x: ");
  x = Double.parseDouble(cin.readLine());
  System.out.print("Enter value for w: ");
  w = Double.parseDouble(cin.readLine());
  System.out.print("Enter value for z: ");
  z = Double.parseDouble(cin.readLine());

  y = (5 * (3 * x * x + 5 * x + 2)/(7 * w - 1 / z) - z)/(4 * ((3 + x) / 7));

  System.out.println("The result is: " + y);
}

C++

#include <iostream>
using namespace std;
int main() {
  double w, x, y, z;

  cout << "Enter value for x: ";
  cin >> x;
  cout << "Enter value for w: ";
  cin >> w;
  cout << "Enter value for z: ";
  cin >> z;

  y = (5 * (3 * x * x + 5 * x + 2)/(7 * w - 1 / z) - z)/(4 * ((3 + x) / 7));

  cout << "The result is: " << y;
  return 0;
}

C#

static void Main() {
  double w, x, y, z;

  Console.Write("Enter value for x: ");
  x = Double.Parse(Console.ReadLine());
  Console.Write("Enter value for w: ");
  w = Double.Parse(Console.ReadLine());
  Console.Write("Enter value for z: ");
  z = Double.Parse(Console.ReadLine());

  y = (5 * (3 * x * x + 5 * x + 2)/(7 * w - 1 / z) - z)/(4 * ((3 + x) / 7));

  Console.Write("The result is: " + y);

  Console.ReadKey();
}

Visual Basic

Sub Main()
  Dim w, x, y, z As Double

  Console.Write("Enter value for x: ")
  x = Console.ReadLine()
  Console.Write("Enter value for w: ")
  w = Console.ReadLine()
  Console.Write("Enter value for z: ")
  z = Console.ReadLine()

  y = (5 * (3 * x ^ 2 + 5 * x + 2)/(7 * w - 1 / z) - z)/(4 * ((3 + x) / 7))

  Console.Write("The result is: " & y)

  Console.ReadKey()
End Sub

Python

x = float(input("Enter value for x: "))
w = float(input("Enter value for w: "))
z = float(input("Enter value for z: "))

y = (5 * (3 * x ** 2 + 5 * x + 2)/(7 * w - 1 / z) - z)/(4 * ((3 + x) / 7))

print("The result is:", y)

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is taken from a book of mine. If you enjoyed it, please visit my author's page inand check out all of my books!