Exercise
Function Returning A Value
Objective
Write a C# program whose Main must be like this:
public static void Main()
{
int x= 3;
int y = 5;
Console.WriteLine( Sum(x,y) );
}
"Sum" is a function that you must define and that will be called from inside Main. As you can see in the example, it must accept two integers as parameters, and it must return an integer number (the sum of those two numbers).
Write Your C# Exercise
C# Exercise Example
// Importing the System namespace to access basic system functions
using System;
class Program
{
// Function to calculate the sum of two integers and return the result
public static int Sum(int a, int b)
{
// Return the sum of a and b
return a + b;
}
// Main method to call the Sum function and display the result
public static void Main()
{
// Declare two integer variables
int x = 3; // First number
int y = 5; // Second number
// Call the Sum function with x and y as arguments, and display the result
Console.WriteLine(Sum(x, y)); // This will print 8 (3 + 5)
}
}