w3resource

C++ Exercises: Compute the specified expressions and print the output


Compute Specified Expression

Write a program in C++ to compute the specified expressions and print the output.

Sample Solution:

C++ Code:

#include <iostream> // Including the input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function
{
    cout << "\n\n Compute the specified expressions and print the output:\n"; // Outputting a message indicating the purpose of the program
    cout << "------------------------------------------------------------\n"; // Outputting a separator line

    // Calculating and outputting the result of the given expression
    cout << " Result of the expression "
<< "(25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5) is : " // Outputting the expression
<< (25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5) << "\n"; // Calculating and outputting the result of the expression

    return 0; // Returning 0 to indicate successful program execution
}

Sample Output:

 Compute the specified expressions and print the output:              
------------------------------------------------------------          
 Result of the expression (25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5) is : 2.13889 

Flowchart:

Flowchart: Compute the specified expressions and print the output

For more Practice: Solve these Related Problems:

  • Write a C++ program to compute a given complex arithmetic expression and display each intermediate step.
  • Write a C++ program that evaluates multiple expressions and prints their results in a side-by-side comparison.
  • Write a C++ program to compute a compound expression with both integer and floating-point operations, ensuring proper type casting.
  • Write a C++ program that accepts values from the user to substitute into an expression and then computes and displays the result.

Go to:


PREV : Current Date and Time Display.
NEXT : Test Type Casting in C++.

C++ Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.