w3resource

C#: Compute the sum of two given integers, if two values are equal then return the triple of their sum


Sum or Triple Sum of Integers

Write a C# program to compute the sum of two given integers. If two values are the same, return the triple of their sum.

C# Sharp Exercises: Compute the sum of two given integers, if two values are equal then return the triple of their sum

Sample Solution:

C# Sharp Code:

using System;
using System.Collections.Generic;

// This is the beginning of the Exercise19 class
public class Exercise19 {
    // This is the main method where the program execution starts
    static void Main(string[] args)
    {
        // Displaying the result of the SumTriple method with different integer arguments
        Console.WriteLine(SumTriple(2, 2));   // Test case 1: Equal integers
        Console.WriteLine(SumTriple(12, 10)); // Test case 2: Different integers
        Console.WriteLine(SumTriple(-5, 2));  // Test case 3: Different integers with negative value       
    }

    // Method to calculate the sum of two integers; if they are equal, the sum is tripled
    public static int SumTriple(int a, int b)
    {
        // Using a ternary conditional operator to check if integers 'a' and 'b' are equal
        return a == b ? (a + b) * 3 : a + b; // If 'a' equals 'b', return the triple sum of 'a' and 'b', otherwise return their sum
    }
}

Sample Output:

12                                                                     
22                                                                     
-3

Flowchart:

Flowchart: C# Sharp Exercises - Compute the sum of two given integers, if two values are equal then return the triple of their sum

For more Practice: Solve these Related Problems:

  • Write a C# program that returns double the sum if the absolute difference of two numbers is less than 5.
  • Write a C# program to compute the sum of two numbers, and triple the sum if either is divisible by the other.
  • Write a C# program that returns the square of the sum if the inputs are equal.
  • Write a C# program that returns triple the sum of two numbers only if both numbers are odd.

Go to:


PREV : Check Positive and Negative Pair.
NEXT : Absolute Difference or Double It.

C# Sharp Code Editor:



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.