w3resource

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

C# Sharp Basic: Exercise-19 with Solution

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

C# Sharp Code Editor:

Previous: Write a C# program to check two given integers and return true if one is negative and one is positive.
Next: Write a C# program to get the absolute value of the difference between two given numbers. Return double the absolute value of the difference if the first number is greater than second number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



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://www.w3resource.com/csharp-exercises/basic/csharp-basic-exercise-19.php