c#两个整数相除
Given (input) two integer numbers and we have to find the addition of given number in C#.
给定(输入)两个整数,我们必须在C#中找到给定数字的加法。
Example:
例:
Input:
First number: 10
Second number: 20
Output:
Addition of 10 and 20 is = 30
Program:
程序:
using System;
class AddTwoNumbers {
static void Main() {
try{
//declare two variables
int a = 0;
int b = 0;
//input numbers
Console.Write("Enter first number: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
b = Convert.ToInt32(Console.ReadLine());
//calculating sum
int sum = a+b;
Console.WriteLine("Addition of " + a + " and " + b + " is = " + sum);
}
catch(Exception ex){
Console.WriteLine("Error: " + ex.ToString());
}
}
}
Output
输出量
Enter first number: 10
Enter second number: 20
Addition of 10 and 20 is = 30
使用功能 (Using function)
Here, we are defining a function that will take two integer arguments and return the sum of the numbers.
在这里,我们定义了一个函数,该函数将接受两个整数参数并返回数字的总和。
Program:
程序:
using System;
class AddTwoNumbers {
//defining function
static int sumOfTwoNumber(int x, int y){
return x+y;
}
//main function
static void Main() {
try{
//declare two variables
int a = 0;
int b = 0;
//input numbers
Console.Write("Enter first number: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
b = Convert.ToInt32(Console.ReadLine());
//calculating sum by calling the function
int sum = sumOfTwoNumber(a,b);
Console.WriteLine("Addition of " + a + " and " + b + " is = " + sum);
}
catch(Exception ex){
Console.WriteLine("Error: " + ex.ToString());
}
}
}
Output
输出量
Enter first number: 10
Enter second number: 20
Addition of 10 and 20 is = 30
翻译自: https://www.includehelp.com/dot-net/addition-of-two-integer-numbers.aspx
c#两个整数相除