LAB 4
If statement
Exercise 1



• Given a positive number, print its square and
                  square root.
Exercise 2
• Write a C++ program to take a depth (in
 kilometers) inside the earth as input data;
 compute and display the temperature at that
 depth in degrees Celsius and Fahrenheit. The
 relevant formulas are:
           Celsius = 10 x (depth) + 20
        Farhrenheit = 1.8 x (Celsius) + 32
Exercise 3

• The arithmetic mean of two numbers is the
 result of dividing their some by 2. The geometric
 mean of two numbers is the square root of their
 product. The harmonic mean of two numbers is
 the arithmetic mean of their reciprocals. Write a
 program that takes two floating point numbers
 as inputs and displays these three means.
If statement

1. if (condition)         3. if (condition)
      statement;                statement;

2. if (condition)           else if (condition)
      statement;                statement;

   else                     else
      statement;                statement;
Example



• Write a C++ program to find the roots of the
 quadratic equations given its coefficients.
Pseudocode
1. Read a,b,c.
2. d=b*b - 4*a*c
3. if(d <0)
      Print "No real roots“
    else if(d=0)
      r = -b/2*a
      Print r
    else
      r1 = (-b+sqrt(d))/2*a
      r2 = (-b-sqrt(d))/2*a
      Print r1 , r2

C++ lab -4

  • 1.
  • 2.
    Exercise 1 • Givena positive number, print its square and square root.
  • 3.
    Exercise 2 • Writea C++ program to take a depth (in kilometers) inside the earth as input data; compute and display the temperature at that depth in degrees Celsius and Fahrenheit. The relevant formulas are: Celsius = 10 x (depth) + 20 Farhrenheit = 1.8 x (Celsius) + 32
  • 4.
    Exercise 3 • Thearithmetic mean of two numbers is the result of dividing their some by 2. The geometric mean of two numbers is the square root of their product. The harmonic mean of two numbers is the arithmetic mean of their reciprocals. Write a program that takes two floating point numbers as inputs and displays these three means.
  • 5.
    If statement 1. if(condition) 3. if (condition) statement; statement; 2. if (condition) else if (condition) statement; statement; else else statement; statement;
  • 6.
    Example • Write aC++ program to find the roots of the quadratic equations given its coefficients.
  • 7.
    Pseudocode 1. Read a,b,c. 2.d=b*b - 4*a*c 3. if(d <0) Print "No real roots“ else if(d=0) r = -b/2*a Print r else r1 = (-b+sqrt(d))/2*a r2 = (-b-sqrt(d))/2*a Print r1 , r2