Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Function: Function to calculate the sum of two numbers

C# Sharp Function: Exercise-3 with Solution

Write a program in C# Sharp to create a function for the sum of two numbers.

Pictorial Presentation:

C# Sharp Exercises: Function: Function to calculate the sum of two numbers

Sample Solution:

C# Sharp Code:

using System;
public class funcexer3
{
    public static int Sum(int num1, int num2)
    {
        int total;
        total = num1 + num2;
        return total;
    }
     
    public static void Main()
    {
	  Console.Write("\n\nFunction to calculate the sum of two numbers :\n");
      Console.Write("--------------------------------------------------\n");
	  Console.Write("Enter a number: ");
      int n1= Convert.ToInt32(Console.ReadLine());
      Console.Write("Enter another number: ");
      int n2= Convert.ToInt32(Console.ReadLine());
      Console.WriteLine("\nThe sum of two numbers is : {0} \n", Sum(n1,n2) );
    }
}

Sample Output:

Function to calculate the sum of two numbers :                                                                
--------------------------------------------------                                                            
Enter a number: 25                                                                                            
Enter another number: 50                                                                                      
                                                                                                              
The sum of two numbers is : 75

Flowchart :

Flowchart: C# Sharp Exercises - Function to calculate the sum of two numbers

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a program in C# Sharp to create a user define function with parameters.
Next: Write a program in C# Sharp to create a function to input a string and count number of spaces are in the string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.