w3resource

C#: Compute the sum of the two given integers


Sum is 18 if One Value in 10-20 Range

Write a C# Sharp program to compute the sum of the two given integers. Return 18 if one of the given integer values is in the range of 10..20 inclusive.

visual Presentation:

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

Sample Solution:-

C# Sharp Code:

using System;
using System.Linq;

namespace exercises
{
    // Class declaration
    class Program
    {
        // Main method - entry point of the program
        static void Main(string[] args)
        {
            // Calling the 'test' method with different integers and printing the results
            Console.WriteLine(test(3, 7));     // Output: 18
            Console.WriteLine(test(10, 11));   // Output: 18
            Console.WriteLine(test(10, 20));   // Output: 18
            Console.WriteLine(test(21, 220));  // Output: 241
            Console.ReadLine(); // Keeping the console window open
        }

        // Method to determine the result based on the conditions
        public static int test(int x, int y)
        {
            // If x is between 10 and 20 OR y is between 10 and 20, return 18; otherwise, return the sum of x and y
            return (x >= 10 && x <= 20) || (y >= 10 && y <= 20) ? 18 : x + y;
        }
    }
}

Sample Output:

10
18
18
241

Flowchart:

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

For more Practice: Solve these Related Problems:

  • Write a C# program that returns 25 if either of the two integers is in the 30–40 range.
  • Write a C# program to return 0 if both integers are in the 10–20 range; otherwise return their sum.
  • Write a C# program to return a constant value if any of the two integers fall in overlapping forbidden ranges.
  • Write a C# program to return 18 if the absolute difference of two numbers falls in the 10–20 range.

Go to:


PREV : Within 2 of Multiple of 10.
NEXT : Fizz, Buzz, FizzBuzz.

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.