Showing posts with label Solved Questions. Show all posts
Showing posts with label Solved Questions. Show all posts

+30 C++ Loop Exercise

Congratulations on completing the C++ Repetition Statements section, famous loops, one of the most important topics in any programming language.

Now is the time to put your knowledge into practice, this is where you will evolve, become a real programmer.

Solve the exercises, try, try, try again and again, before you see the solution, combined?
Just reading or watching videos will never make you a programmer, even the worser.

It is in the race, trying, struggling, breaking one's head, sweeping nights, and crying in a fetal position that one forges a true C++ Progressive programmer.

Left? Say goodbye to friends, girlfriend or boyfriend, family, social networks ... time to hide from society and exercise!

Ah ... and post there in the comments your solutions!

WHILE, DO WHILE, and FOR exercises in C++

0. Make a C++ program that asks the user for an integer, and displays their multiplication table.
Multiplication table with FOR, WHILE and DO WHILE

1. Make a program that takes two integers and generates the integers that fall within their range.

2. Make a program that asks for a grade between zero and ten. Show a message if the value is invalid and keep asking until the user enters a valid value.

3. Make a program that prints numbers 1 to 20 on the screen, one below the other. Then modify the program so that it shows the numbers next to each other.

4. Write programs that display the following patterns on the screen according to the number you provide, which will always be the number of lines:
(Solutions in e-book)
4.1

** 
*** 
**** 
*****

4.2
1
12 
123 
1234 
12345

4.3
1                                                                                                             
22                                                                                                      
333                                                                                                         
4444                                                                                                         
55555 


4.4
1                                                                                  
2 3                                                                                                         
4 5 6                                                                                                       
7 8 9 10 


4.5
       1                                                           
      2 3                                                           
     4 5 6                                                         
    7 8 9 10

4.6
        *                                                           
       * *                                                         
      * * *                                                         
     * * * *                                                       
    * * * * *

4.7
1                                                                      
01                                                                     
101                                                                    
0101                                                                   
10101

4.8
    *                                                                  
   ***                                                                 
  *****                                                                
 *******                                                               
*********                                                              
 *******                                                               
  *****                                                                
   ***                                                                 
    *


4.9
          1                                                            
        1   1                                                          
      1   2   1                                                        
    1   3   3   1                                                      
  1   4   6   4   1 

4.10                                                                      
1   1                                                                  
1   2   1                                                              
1   3   3   1                                                          
1   4   6   4   1                                                      
1   5   10   10   5   1                                                
1   6   15   20   15   6   1

4.11
12345                                                                                                         
2345                                                                                                          
345                                                                                                           
45                                                                                                            
5

4.12
12345                                                                                                         
 1234                                                                                                         
  123                                                                                                         
   12                                                                                                         
    1

4.13
5 4 3 2 1                                                              
4 3 2 1                                                                
3 2 1                                                                  
2 1                                                                    
1

4.14
    1                                                                  
   21                                                                  
  321                                                                  
 4321                                                                  
54321

4.15
     1234567654321                                                                                            
      12345654321                                                                                             
       123454321                                                                                              
        1234321                                                                                               
         12321                                                                                                
          121                                                                                                 
           1

5. Make a program that reads 5 numbers and reports the largest number.

6. Make a program that reads 5 numbers and gives the sum and average of the numbers.

7. Make a program that calculates the show the arithmetic average of N grades.

8. Make a program that prints only odd numbers between 1 and 50 on the screen. At the end, also show the sum of the numbers.

9. Create a program that asks the user for a number and calculates the summation to that value.

10. Create a program that asks the user for a number, and calculates its factorial. Ex .: 5! = 5.4.3.2.1 = 120
Summation and Factorial

11. The Fibonacci series is formed by the sequence 0,1,1,2,3,5,8,13,21,34,55, ... where the next term is always the sum of the previous two. Make a program capable of generating the series to the nth term that the user must supply.
Fibonacci in C++ with loopings

12. Make a program that asks for two numbers, base and exponent, calculate and display the first number raised to the second number. Do not use the power function of language.
Exponentiation with loopings in C++

13. Make a program that asks for an integer and determines whether or not it is a prime number. A prime number is one that is divisible only by itself and by 1.

14. Make a program that shows all primes between 1 and N where N is a user-supplied integer.
Prime numbers with C++

15. Make a program that displays all possible Powerball game combinations.
Powerball lottery in C++

16. Program software that receives a user number and tells you if it is a perfect number or not. Search Google for a perfect number.

17. Program software that receives two integers from the user, and tell them the greatest common divisor of these numbers.

18. Program a software that receives a number less than 1000, and say the value of the unit, the dozen, and the hundred digits.

19. Program software that calculates the sum of the digits of a number.

20. Make a program that shows the following Series terms:

  S = 1/1 + 2/3 + 3/5 + 4/7 + 5/9 + ... + n / m.

Print at the end the sum of the series.


21. Let the harmonic series H = 1 + 1/2 + 1/3 + 1/4 + ... + 1 / N, Make a program that calculates the value of H with N terms, where N is input by the user.

22. Make a program that shows the following terms of the Series:

  S = 1/1 + 2/3 + 3/5 + 4/7 + 5/9 + ... + n / m.

Print at the end the sum of the series.


23. The value of PI can be approximated by the following infinite sequence:
Infinite series to calculate pi
Create a program that calculates the value of this series with 10 terms, then 100 terms, and finally using a thousand terms. What values did you get?

Equilateral Triangle, Isosceles or Scalene? C++ Exercise

In this C++ tutorial, we will solve the following question from our list of exercises:

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

C++ Triangle Types

There are three types of triangle:

  1. Equilateral: all sides are equal
  2. Isosceles: Only Two Sides Are Equal
  3. Scalene: All Sides Are Different


So all we have to do is ask for three sides of a triangle, and test whether the sides are all the same, or have two equal or if it's all different.

One way to do this is by testing if it's all the same in an IF, to know if it's equilateral:

  • if ((a == b) && (b == c))

Then if they are all different, to know if it is scalene:

  • if ((a! = b) && (a! = c) && (b! = c))


If it is none of the above, it is because it is isosceles.

Our code looks like this:
#include <iostream>
using namespace std;

int main()
{
    int a, b, c;

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

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

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

    if( (a==b) && (b==c) )
        cout<<"Equilateral\n";
    else if( (a!=b) && (a!=c) && (b!=c))
        cout<<"Scalene\n";
    else
        cout<<"Isosceles\n";

}

Equilateral, Isosceles or Scalene?

An interesting thing about programming is that code is a kind of fingerprint.
Each one does their own thing, each one has their own methods, lines of thought and creativity.

Sometimes it is common to make big code, ugly and confusing.
Then we see someone using half the lines we use, doing something far more beautiful and comprehensive.

That's why it's important to study through other people's books, websites, tutorials, and codes to 'pick up' on others' reasoning. Never miss this custom, okay?

Let's go for one more solution.
First, let's test if there are two equal sides:

  • (a == b) || (a == c) || (b == c)


If you have, either one: either equilateral or just isosceles.
So we test if the three sides are equal:

  • (a == b) && (b == c)

If so, we say it is equilateral. Otherwise it falls into the nested ELSE and we say it is isosceles.
If it does not have at least two equal sides, it is scalene.

Look:

#include <iostream>
using namespace std;

int main()
{
    int a, b, c;

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

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

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

    if( (a==b) || (a==c) || (b==c))
        if( (a==b)&&(b==c) )
            cout<<"Equilateral\n";
        else
            cout<<"Isosceles\n";
    else
        cout<<"Scalene\n";

}

C++ hacker test


A hacker is the one who finds loopholes, errors, code problems.
There is a problem with the code above: the conditions of existence of a triangle.

It's not just entering three values and we have a triangle, no.

Research the conditions of existence of a triangle, and before deciding whether it is equilateral, isosceles or scalene, see if the triangle can even exist.

Post your code in the comments.

How many days does the month have - C++ Exercise

In this tutorial, we will solve the following exercise from our list:
  • 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 has 28 days (not leap year).

How many days are there in the month

Come on.
Months with 28 days: 2Months with 30 days: 4, 6, 9 and 11Months with 31 days: 1, 3, 5, 7, 8, 10 and 12

Let's do accumulating cases so you can better fix this important switch technique.
The days variable starts with 0.

The logic is as follows ... every month has 28 days or more. So we want case 2 to always run so it will be down there.

There we will add the value 28 to the days variable.
Okay, any month you enter will be at 28 days, because we will not use break in cases, to accumulate.
  • days = days + 28;

The middle cases are the months 4, 6, 9 and 11 have 30 days.
As we will added 28 days, we only have to add 2 more days to stay 30, in these cases:
  • days = days + 2;

You see: if you type 2, it falls only in case 2, which puts 28 in the days variable. If you type 4, 6, 9 or 11, it falls into those cases that add up to 2 and then falls into the case that adds up to 28 days, totaling 30 days.

So if you type 1, 3, 5, 7, 8, 10, or 12, we should just add +1:
  • days = days + 1;
See how our code looked:

#include <iostream>
using namespace std;

int main()
{
    int days=0, month;

    cout << "Month number: ";
    cin >> month;

    switch(month)
    {
        case 1: case 3: case 5: case 7:
        case 8: case 10: case 12:
            days = days + 1;

        case 4: case 6:
        case 9: case 11:
            days = days + 2;

        case 2:
            days=days+28;
            break;
        default:
            cout <<"Invalid month"<<endl;
    }

    cout <<"Month "<<month<<" has "<<days<<" days.\n";
}

Can you understand? It is a very charming solution.

Leap Year solution



  • 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.



Here, in addition to what is in the previous code, we have to ask the year to the user. If it is leap year, we should add 29 in case 2 to the days variable, if not, we keep adding only 28.


We do this with an IF ELSE statement internal to the switch case.
Here's how the code looks:
#include <iostream>
using namespace std;

int main()
{
    int days=0, month, year;

    cout << "Month number: ";
    cin >> month;

    cout << "Year: ";
    cin >> year;

    switch(month)
    {
        case 1: case 3: case 5: case 7:
        case 8: case 10: case 12:
            days = days + 1;

        case 4: case 6:
        case 9: case 11:
            days = days + 2;

        case 2:
            if( (year % 400 == 0) || ( (year % 4 == 0) && (year % 100 != 0) ) )
                days = days+29;
            else
                days = days+28;
            break;
        default:
            cout <<"Invalid month"<<endl;
    }

    cout <<"The month "<<month<<" has "<<days<<" days.\n";
}

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++

C++ Basic Exercises

Congratulations, you have completed the basic part of our C++ Course.
Now let's practice.

Try with all your heart, with all your calm, effort and concentration, to do the exercises below.
Leave a comment with your solutions, ok?

C++ questions

01. Write a program that asks for the radius of a circle, and then displays the circle's perimeter and area.

02. Make a program that takes the radius of a sphere and calculates its volume.

03. Make a program that receives an integer, represented a value in years. Show how many days this time interval has, assuming a year has 365 days.

04. Make a program that receives two variables: the value of hours and minutes. Then convert everything to minutes and also to seconds.

05. Create a program that receives the temperature in degrees Celsius and converts it to Farenheit.

06. Do the opposite of the previous exercises.
Conversion between Celsius and Fahrenheit

07. Make a program that asks for the current year and your age, then displays your year of birth.

Percentage Exercises in C++

08. Make a program that calculates 12% of 2112

09. Make a program that receives a user value and calculates 12% of that total.

10. Make a program that receives a percentage value from the user, and calculates how much it represents from a second value that he typed.

11. Program software that receives two numbers, where the first must be less than the second.
It then calculates the percentage that the first represents from the second.
For example, if you entered 12 and 21, that means 12 represents 57.14% of 21

12. You have become a C ++ programmer, and now you are earning super good. But you will still have to pay taxes.
Create software that receives your salary and calculates the 7% income tax.

The output of your program should be gross salary (no rebate), the amount of tax you will pay and your net salary (after deducting IR).

13. Due to inflation, every year your salary should be adjusted based on it.
For example, if inflation was 2.5% then your salary should grow by the same amount so as not to lose value.
Create a C ++ program that asks for a person's salary, last year's inflation, and apply that inflation. Show previous salary, inflation increase, and new salary.

14. In the city of C ++ lland there is a tolerance of 15% of the speed limit so as not to be fined. Make a program that asks the user for the maximum speed of a road and calculates how fast the car can travel without being fined. Your code will be embedded in the car's GPS system to warn the speed limit the car must travel.
Solutions: Percentage in C++

Average exercises in C++

15. Create a program that asks the user for two grades, and returns their average.

16. Do the same as the previous exercise, but for 3 grades.

17. The MIT entrance exam weights are 3 for Mathematics, 2.5 for Physics, 2.5 for Chemistry, 1.0 for Foreign language and 1.0 for English. Create a system that asks for user grades and returns their average.