Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Calculate sum of two numbers using function in C#

Calculate sum of two numbers using function in C#

Posted on May 7, 2022

Table of Contents

  • Calculate sum of two numbers using function in C#
    • Code to find sum of two numbers in C#
      • Add two numbers using function in C#
      • Add two numbers using function in C# – Entered by user

Calculate sum of two numbers using function in C#

In this article, we will discuss the concept of Calculate sum of two numbers using function in C#

In this post, we are going to learn how to write a program to  calculate sum of two numbers using function in C# language

Code to find sum of two numbers in C#

Add two numbers using function in C#

In this code, we are going to learn how to write a program to add two numbers with initialized variables using function in C# programming language

Program 1

//Write a program calculate sum of two numbers using function
using System; 
public class Sum_Two_Num_Function { 
    public static int 
    sum_Num(int fNum, int sNum) { //function definition with parameter
        int tot; 
        tot=fNum+sNum; //Find addition of two numbers
        return tot; //return tot to main method
        
    }
        public static void Main() { 
            int fNum=45; 
            int sNum=56; //variable declaration and initialization for numbers
            Console.WriteLine("The sum of {0} and {1} is: {2}: ",fNum,sNum,sum_Num(fNum,sNum)); } }
            //display total of two numbers

When the above code is executed, it produces the following result

The sum of 450 and 560 is: 1010:

 

Add two numbers using function in C# – Entered by user

In this code, we are going to learn how to write a program to add two numbers entered by user using function in C# programming language

Program 2

// Write a program calculate sum of two numbers using function
using System; 
public class Sum_Two_Num_Fun { 
    public static int sum_Num(int num1, int num2) { 
        //Function definition
        int total;//variable for calculate  total of two num
        total=num1+num2; //Calculate total
        return total; //return total
        
    } 
    public static void Main() { 
        Console.Write("Enter number for n1: ");//Ask input from the user 
        int n1=Convert.ToInt32(Console.ReadLine()); //reading the input
        Console.Write("Enter number for n2: "); 
        int n2=Convert.ToInt32(Console.ReadLine()); 
        Console.WriteLine("The sum of {0} and {1} is: {2}: ",n1,n2,sum_Num(n1,n2)); } }
        //Display addition of two numbers

When the above code is executed, it produces the following result

Enter number for n1: 666
Enter number for n2: 777
The sum of 666 and 777 is: 1443:

 

In this code, the user asked to enter two integers and the given integers are stored in variables n1 and n2 respectively.

Then these two numbers are added using the plus(+) operator and the result is stored in the total variable.

Finally the display statement is used to print the addition of this numbers using calling the function

 

Suggested for you

Write a program to sum of prime numbers 1 to n in Java language

Write a program to sum of prime numbers 1 to n in C language

Write a program to sum of prime numbers 1 to n in C++ language

Write a program to sum of prime numbers 1 to n in Python  language

 

Write a program to sum of natural numbers 1 to n in Java language

Write a program to sum of prime numbers 1 to n in C language

Write a program to sum of prime numbers 1 to n in C++ language

Write a program to sum of prime numbers 1 to n in Python  language

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes