Showing posts with label IF and ELSE. Show all posts
Showing posts with label IF and ELSE. Show all posts

Quadratic equation in C++ - Bhaskara

In this C ++ tutorial, we will learn how to solve a 2nd degree equation using the Bháskara formula in C ++. It's a matter of our list of conditional testing exercises:

  • 11. Make a program that calculates the roots of a quadratic equation in the form ax² + bx + c. The program should request the values of a, b and c and make the consistencies, informing the user in the following situations:
    If the user enters the value of A equal to zero, the equation is not of the second degree and the program should not ask the other values, being closed;
    If the calculated delta is negative, the equation has no real roots. Inform the user and quit the program;
    If the calculated delta equals zero the equation has only one real root; inform it to the user;
    If delta is positive, the equation has two real roots; inform them to the user;

Bhaskara Formula in C ++

First, we ask for the three coefficients a, b and c.
We also declare the delta variable to store delta (really?) and root1 and root2 to store roots.

The first test is to check if a is other than 0, only if we can calculate the roots.
If not, it falls into ELSE and we warn you that the equation does not exist.

Being different from 0, we have to calculate the delta:
From Bhaskara: delta = b² - 4ac

Now, let's test the delta.
If it is negative, we say it has no real roots.
If 0, we calculate the single root: (-b / 2a)
If greater than 0, we calculate the two roots (-b- sqrt(delta))/2a and (-b + sqrt(delta))/2a

See how our code looked:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    float a, b, c, delta,
          root1, root2;

    cout << "Coefficient a: ";
    cin >> a;

    cout << "Coefficient b: ";
    cin >> b;

    cout << "Coefficient c: ";
    cin >> c;

    if(a != 0){
        delta = (b*b) - (4*a*c);

        if(delta<0){
            cout <<"No real roots\n";
        }
        else if (delta==0){
            root1=(-b)/(2*a);
            cout << "One real root: "<<root1<<endl;
        }else{
            root1=(-b - sqrt(delta))/(2*a);
            root2=(-b + sqrt(delta))/(2*a);
            cout << "Root 1: "<<root1<<endl;
            cout << "Root 2: "<<root2<<endl;
        }
    }else{
        cout <<"a=0, this is not a quadratic equation\n";
    }

}

C++ challange

Can you solve this question to give complex roots?


#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    float a, b, c, delta,
          root1, root2;

    cout << "Coefficient a: ";
    cin >> a;

    cout << "Coefficient b: ";
    cin >> b;

    cout << "Coefficient c: ";
    cin >> c;

    if(a != 0){
        delta = (b*b) - (4*a*c);

        if(delta<0){
            delta = -delta;
            cout<<"Root 1: "<<(-b/(2*a))<<"+"<<(sqrt(delta)/(2*a))<<"i\n";
            cout<<"Root 2: "<<(-b/(2*a))<<"-"<<(sqrt(delta)/(2*a))<<"i\n";
        }
        else if (delta==0){
            root1=(-b)/(2*a);
            cout << "One real root: "<<root1<<endl;
        }else{
            root1=(-b - sqrt(delta))/(2*a);
            root2=(-b + sqrt(delta))/(2*a);
            cout << "Root 1: "<<root1<<endl;
            cout << "Root 2: "<<root2<<endl;
        }
    }
    else
        cout <<"a=0, this is not a quadratic equation\n";

    return 0;
}

3 Numbers in ascending and descending order in C++

In this tutorial, we will solve two exercises from our list of conditional statements tests.

  • 03. Make a program that asks for 3 numbers and places them in ascending order.
  • 04. Make a program that asks for 3 numbers and puts them in descending order.

3 numbers in ascending order

We will store the numbers in the num1, num2 and num3 variables, and we will also use a temporary variable, an auxiliary, temp.

The idea is this: we want to put the largest value entered in the num1 variable. The second largest value in variable num2 and the smallest in num3.

For this, let's make three comparisons:

  1. Compare num1 with num2, if num2 is greater, we invert the values. After this first test, we guarantee that the value contained in num1 is greater than num2.
  2. Compare num1 with num3, if num3 is greater, we invert the values. After this second test, we guarantee that the value contained in num1 is greater than num2 and now greater than num3.
  3. Now we have num2 and num3, if num3 is greater, we must invert the values, so we have the value contained in num2 greater than num3.

To do the value inversions, we will use the tutorial:
How to change the value of two variables

Our code is:
#include <iostream>
using namespace std;

int main()
{
    int num1, num2, num3, temp;

    cout << "Number 1: ";
    cin >> num1;

    cout << "Number 2: ";
    cin >> num2;

    cout << "Number 3: ";
    cin >> num3;

    if(num2 > num1){
        temp = num1;
        num1 = num2;
        num2 = temp;
    }

    if(num3 > num1){
        temp = num1;
        num1 = num3;
        num3 = temp;
    }

    if(num3 > num2){
        temp = num2;
        num2 = num3;
        num3 = temp;
    }

    cout <<num1<<" >= "<<num2<<" >= "<<num3<<endl;
}

3 numbers in descending order

The logic is the same as above, we just want num1 to have the smallest value, num2 the second smallest value and num3 the greater value.

Our code is:




#include <iostream>
using namespace std;

int main()
{
    int num1, num2, num3, temp;

    cout << "Number 1: ";
    cin >> num1;

    cout << "Number 2: ";
    cin >> num2;

    cout << "Number 3: ";
    cin >> num3;

    if(num2 < num1){
        temp = num1;
        num1 = num2;
        num2 = temp;
    }

    if(num3 < num1){
        temp = num1;
        num1 = num3;
        num3 = temp;
    }

    if(num3 < num2){
        temp = num2;
        num2 = num3;
        num3 = temp;
    }

    cout <<num1<<" <= "<<num2<<" <= "<<num3<<endl;
}

Comparing Two Numbers in C++: Larger, Smaller, or Equal

Resolved, coded-out question from the C++ Conditional statements Exercises tutorial:

01. Make a program that asks for two numbers, and say which one is greater, witch is smaller or if they are equal.

How to compare two numbers in C++

Let's store the numbers in variables num1 and num2.
First, let's test if the first one is larger than the second, if it go in the first IF and says it in cout.

If not, fall into the ELSE.
In this ELSE, the second number is greater than the first, or can be equal.

We tested this with a new nested IF, to see if num2 is greater than num1, if so, we warn you.
If num1 is not greater than num2, or num2 is not greater than num1, then it falls into the nested ELSE and we necessarily have the numbers to be equal.

See how our code looked:




#include <iostream>
using namespace std;

int main()
{
    int num1, num2;

    cout << "Number 1: ";
    cin >> num1;
    cout << "Number 2: ";
    cin >> num2;

    if(num1>num2)
        cout <<num1<<" is greater than "<<num2<<endl;
    else
        if(num2>num1)
            cout <<num2<<" is greater than "<<num1<<endl;
        else
            cout << "Equals"<<endl;
    return 0;
}

C++ Conditional Statements Exercises

Guys, let's wrap up a section of our C ++ course once again.
Congratulations if you arrived here. Now let's practice our knowledge by doing some exercises.

And remember, try hard, but really, really hard.
Not of? Think some more. Then more, take a walk, cool your head, and try again.

Problem solving is synonymous with being a programmer, so try to solve the exercises below using your basic knowledge of C ++ and conditional testing.

C++ conditional testing problems

01. Make a program that asks for two numbers, and say which one is larger or if they are equal.
How to compare two numbers in C++

02. Write a program that tells you whether a received number is positive or negative.

03. Make a program that asks for two integers and store in two variables. Then change the value of the variables and display on screen
Swapping two numbers

04. Make a program that asks for 3 numbers and places them in ascending order.

05. Make a program that asks for 3 numbers and puts them in descending order.
3 numbers in ascending and descending order

06. Program software that receives the user's X and Y coordinates, and tell which quadrant the point is in.

07. Make a simple calculator program that asks the user for two numbers, then displays a menu where he will choose which operation to perform. The operation and output must be in a switch case.

08. Using switch case concepts, make a program that asks the user the month (number 1 through 12), and tells you how many days that month has. February is 28 days old (not leap).

09. Using switch case concepts, make a program that asks the user the month (number 1 through 12), and tells you how many days that month has, including whether it is a leap year or not.
How many days a month has

10. Make a program that takes all three sides of a triangle and tells you if it is equilateral, isosceles, or scalene.
Types of triangle

11. Make a program that calculates the roots of a quadratic equation in the form ax² + bx + c. The program should request the values ​​of a, b and c and make the calculations, informing the user in the following situations:

    If the user enters the value of A equal to zero, the equation is not of the second degree and the program should not ask the other values, being closed;
    If the calculated delta is negative, the equation has no real roots. Inform the user and quit the program;
    If the calculated delta equals zero the equation has only one real root; inform it to the user;
    If delta is positive, the equation has two real roots; inform them to the user;

PS: Use cmath library (#include <cmath>) and sqrt(x) function to calculate the square root of x.

How to solve quadratic equation in C++