w3resource

C++ Exercises: Compute the sum of the three integers. If one of the values is 13 then do not count it and its right towards the sum

C++ Basic Algorithm: Exercise-53 with Solution

Sum Ignoring 13 and Right Values

Write a C++ program to compute the sum of the three integers. If one of the values is 13 then do not count it and its right towards the sum.

Sample Solution:

C++ Code :

#include <iostream>
using namespace std;

// Function 'test' takes three integers (x, y, z) as parameters
int test(int x, int y, int z)
{
    // Check if x is 13
    if (x == 13)
        return 0; // Return 0 if x is 13

    // Check if y is 13
    if (y == 13)
        return x; // Return x if y is 13

    // Check if z is 13
    if (z == 13)
        return x + y; // Return sum of x and y if z is 13

    // If none of the above conditions are met, return the sum of x, y, and z
    return x + y + z;
}

int main() 
{
    // Testing the 'test' function with different sets of numbers
    cout << test(4, 5, 7) << endl;    // Output: 16 (x + y + z = 4 + 5 + 7 = 16)
    cout << test(7, 4, 12) << endl;   // Output: 23 (x + y + z = 7 + 4 + 12 = 23)
    cout << test(10, 13, 12) << endl; // Output: 10 (y is 13, so returns x = 10)
    cout << test(13, 12, 18) << endl; // Output: 0 (x is 13, so returns 0)

    return 0;    
}

Sample Output:

16
23
10
0

Visual Presentation:

C++ Basic Algorithm Exercises: Compute the sum of the three integers. If one of the values is 13 then do not count it and its right towards the sum.

Flowchart:

Flowchart: Compute the sum of the three integers. If one of the values is 13 then do not count it and its right towards the sum.

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C++ program to compute the sum of three given integers. If the two values are same return the third value.
Next: Write a C++ program to compute the sum of the three given integers. However, if any of the values is in the range 10..20 inclusive then that value counts as 0, except 13 and 17.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/cpp-exercises/basic-algorithm/cpp-basic-algorithm-exercise-53.php