w3resource

C#: Compute the sum of the three given integers


Sum Ignoring 10-20 Except 13, 17

Write a C# Sharp program to compute the sum of the three given integers. All values in the range 10..20 inclusive count as 0, except 13 and 17.

Visual Presentation:

C# Sharp: Basic Algorithm Exercises - Compute the sum of the three given integers.

Sample Solution:-

C# Sharp Code:

using System; // Import the System namespace which contains fundamental types and base classes
using System.Collections.Generic; // Import the System.Collections.Generic namespace for collections
using System.Linq; // Import the LINQ namespace for LINQ functionality
using System.Text; // Import the System.Text namespace for string manipulation
using System.Threading.Tasks; // Import the System.Threading.Tasks namespace for asynchronous operations

namespace exercises // Define a namespace called 'exercises'
{
    class Program // Define a class named 'Program'
    {
        static void Main(string[] args) // The entry point of the program
        {
            // Output the result of test function with different sets of three integers as arguments
            Console.WriteLine(test(4, 5, 7)); // Test with integers 4, 5, and 7
            Console.WriteLine(test(7, 4, 12)); // Test with integers 7, 4, and 12
            Console.WriteLine(test(10, 13, 12)); // Test with integers 10, 13, and 12
            Console.WriteLine(test(14, 15, 16)); // Test with integers 14, 15, and 16
            Console.ReadLine(); // Wait for user input before closing the console window
        }

        // Function definition of test function that takes three integers 'x', 'y', and 'z' and returns an integer
        public static int test(int x, int y, int z)
        {
            // Calls the fix_num method for each integer and returns the sum of fixed values
            return Program.fix_num(x) + Program.fix_num(y) + Program.fix_num(z);
        }

        // Private method fix_num that takes an integer 'n' and returns a fixed integer or 0
        private static int fix_num(int n)
        {
            // Checks if 'n' falls within certain ranges and returns 0 if it does, otherwise returns 'n'
            return (n > 9 && n < 13) || (n > 13 && n < 17) || (n > 17 && n < 21) ? 0 : n;
        }
    }
}

Sample Output:

16
11
13
0

Flowchart:

C# Sharp: Flowchart: Compute the sum of the three given integers.

For more Practice: Solve these Related Problems:

  • Write a C# program to sum three numbers, excluding all values in the 10–20 range unless they are 11 or 19.
  • Write a C# program to count only odd numbers not in 10–20 range, with exceptions for 13 and 17.
  • Write a C# program to sum three values skipping those in 10-20 unless the value is divisible by 13 or 17.
  • Write a C# program to sum values only if they are not in the banned 10–20 range or are perfect squares.

Go to:


PREV : Sum of Three Integers Ignoring 13 and Right.
NEXT : Nearest to 13 Without Crossing.

C# Sharp Code Editor:



Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.