Add or Subtract two or more than two Integers in C-Sharp:-
In the above-given video, I have shown that how to add two or more than two integers can be added or subtracted by hard coding or taking input from the user. So in this article, I am going to show you step by step:-
Adding the Values:-
Step1:
First of all, All you had to do is to take two integers and variables and an extra variable for saving the value that comes by adding the two variables (a and b). using the hard-code method (hard-code method means setting the values of variables)
int a=5,b=10,c;
Step2:
And now I am going to use the formula for adding the values of the variables.
c=a+b;
Step3:
Now I am going to save the answer in variable c.
Console.Write("The answer is = {0}",c);
Step4:
Use Console.ReadKey to hold the result or answer.
Console.ReadKey();
Step5:
Press F5 or the Start button.
Output:
And now, I am going to show how to add Three values.
First of all, All you had to do is to take Three integers and variables and an extra variable for saving the value that comes by adding the two variables (a and b). using the hard-code method (hard-code method means setting the values of variables)
int a=5,b=20,c=25,d;
Step2:
And now I am going to use the formula for adding the values of the variables.
d=a+b+c;
Step3:
Now I am going to save the answer in variable d.
Console.Write("The answer is = {0}",d);
Use Console.ReadKey to hold the result or answer.
Console.ReadKey();
Press F5 or the Start button.
Output:
And now, I am going to add two values by taking any value from the user.
Now, I am going to add two values with a different method. Take three variables with no given value.
int a,b,c;
Step2:
And now I am going to take any two values from the user.
Console.Write("Enter First Value: ");
a=int.Parse(Consoel.ReadLine());
Console.Write("Enter Second Value: ");
b=int.Parse(Consoel.ReadLine());
And now I am going to use the formula for adding the values of the variables.
c=a+b;
Now I am going to save the answer in variable c.
Console.Write("The answer is = {0}",c);
Use Console.ReadKey to hold the result or answer.
Console.ReadKey();
Press F5 or the Start button.
After running the program,
The First Value I entered is 20 and
The Second Value I entered is 15.
Output:
And now, I am going to add three values by taking any value from the user.
Step1:
Now, I am going to add three values with a different method. Take four variables with no given value.
int a,b,c,d;
Step2:
And now I am going to take any three values from the user.
Console.Write("Enter First Value: ");
a=int.Parse(Consoel.ReadLine());
Console.Write("Enter Second Value: ");
b=int.Parse(Consoel.ReadLine());
Console.Write("Enter Third Value: ");
c=int.Parse(Consoel.ReadLine());
And now I am going to use the formula for adding the values of the variables.
d=a+b+c;
Now I am going to save the answer in variable d.
Console.Write("The answer is = {0}",d);
Use Console.ReadKey to hold the result or answer.
Console.ReadKey();
Press F5 or the Start button.
After running the program,
The First Value I entered is 25
The Second Value I entered is 25 and
The Third Value I entered is 25.
Output:
Subtracting the Values:-
Step1:
First of all, All you had to do is to take two integers and variables and an extra variable for saving the value that comes by subtracting the two variables (a and b). using the hard-code method (hard-code method means setting the values of variables)
int a=5,b=10,c;
Step2:
And now I am going to use the formula for subtracting the values of the variables.
c=a-b;
Step3:
Now I am going to save the answer in variable c.
Console.Write("The answer is = {0}",c);
Use Console.ReadKey to hold the result or answer.
Console.ReadKey();
Press F5 or the Start button.
Output:
And now, I am going to show how to subtract Three values.
Step1:
First of all, All you had to do is to take Three integers and variables and an extra variable for saving the value that comes by subtracting the two variables (a and b). using the hard-code method (hard-code method means setting the values of variables)
int a=5,b=15,c=20,d;
Step2:
And now I am going to use the formula for subtracting the values of the variables.
d=a-b-c;
Step3:
Now I am going to save the answer in variable d.
Console.Write("The answer is = {0}",d);
Use Console.ReadKey to hold the result or answer.
Console.ReadKey();
Press F5 or the Start button.
Output:
Step1:
Now, I am going to subtract two values with a different method. Take three variables with no given value.
int a,b,c;
Step2:
And now I am going to take any two values from the user.
Console.Write("Enter First Value: ");
a=int.Parse(Consoel.ReadLine());
Console.Write("Enter Second Value: ");
b=int.Parse(Consoel.ReadLine());
And now I am going to use the formula for subtracting the values of the variables.
c=a-b;
Now I am going to save the answer in variable c.
Console.Write("The answer is = {0}",c);
Use Console.ReadKey to hold the result or answer.
Console.ReadKey();
Press F5 or the Start button.
After running the program,
The First Value I entered is 20 and
The Second Value I entered is 5
Output:
And now, I am going to add three values by taking any value from the user.
Step1:
Now, I am going to subtract three values with a different method. Take four variables with no given value.
int a,b,c,d;
Step2:
And now I am going to take any three values from the user.
Console.Write("Enter First Value: ");
a=int.Parse(Consoel.ReadLine());
Console.Write("Enter Second Value: ");
b=int.Parse(Consoel.ReadLine());
Console.Write("Enter Third Value: ");
c=int.Parse(Consoel.ReadLine());
And now I am going to use the formula for adding the values of the variables.
d=a-b-c;
Now I am going to save the answer in variable d.
Console.Write("The answer is = {0}",d);
Use Console.ReadKey to hold the result or answer.
Console.ReadKey();
Press F5 or the Start button.
After running the program,
The First Value I entered is 30
The Second Value I entered is 25 and
The Third Value I entered is 15.
Output:
Comments
Post a Comment