C# Adding Program - Learn Syntax & More

  • C#
  • Thread starter TheDemx27
  • Start date
  • Tags
    Program
In summary, the author wrote a program to add two numbers and loops, but found an easier way. They used console.writeLine() and console.readLine() to input the values and Int.parse() to check if the input was an integer. If it wasn't, they threw an error.
  • #1
TheDemx27
Gold Member
169
13
Just starting to learn C#. I just wrote this program to add two numbers and loops by looking up syntax etc. There HAS to be a more efficient way to do this.

Code:
using System;

    public class Program
    {
        static void Main()
        {
            bool repeat = true;
            while (repeat == true)
                {
                    string x;
                    string y;
                    int xint;
                    int yint;
                    string useranswer;


                    Console.WriteLine("Enter 'X':");
                    x = Console.ReadLine();
                    xint = int.Parse(x);
                    Console.Clear();

                    Console.WriteLine("Enter 'Y':");
                    y = Console.ReadLine();
                    yint = int.Parse(y);
                    Console.Clear();

                    int answer = xint + yint;

                    Console.WriteLine("{0} + {1} = {2}", x, y, answer);
                    Console.ReadLine();
                    
                    Console.WriteLine("Run again?: Y/N");
                    useranswer = Console.ReadLine();
                    Console.Clear();

                    if (useranswer == "y" || useranswer == "Y")
                    {
                        repeat = true;
                    }
                    else
                    {
                        repeat = false;
                    }
                }

            }

    }
 
Technology news on Phys.org
  • #2
You could pass X and Y from the command line.
 
  • #3
That's about as good as it gets, for reading input. C# doesn't have "easy to use" functions like scanf() in C or the << operator in C++. But those easy to use functions are not much use except for toy programs, because it's very hard to catch errors in the input.

You could make the code a neater by "factoring out" the repeated code and writing your own input function, something like
Code:
int getvalue(string prompt)
{
  Console.WriteLine(prompt);
  string x = Console.ReadLine();
  Console.Clear();
  return int.Parse(x);
}
...
xint = getvalue("Enter X:");
yint = getvalue("Enter Y:");
(Note, I haven't actually tested that code!)

Your test for repeating the loop is a bit longwinded. You could write
Code:
repeat = (useranswer == "y" || useranswer == "Y");
or even
Code:
repeat = (useranswer.ToLower() == "y");

You can also make your code shorter by declaring variables when you first use them, instead of by separate statements at the top of a function, as I did in
Code:
string x = Console.ReadLine();
or even
Code:
var x = Console.ReadLine();
where "var" means "the compiler can figure out what type of variable this is, so I don't really care".
 
Last edited:
  • Like
Likes 1 person
  • #4
you could just use a forms app instead of a console app which is much more efficient or
Console.WriteLine("{0} + {1} = {2}", x, y, xint+yint);
maybe that will work its been awhile since I've done a console app. get visual studio it's so superior.
 
  • #5
It's advisable to error check all inputs. You can do it like this. Once you get an input, check to see if it really is an integer:


try
{
Int.Parse(myinputstring);
}
catch (Exception ex)
{
Console.WriteLine("That was not an integer.");
}


And yes, it will probably be needful to use a goto statement, or else, use a flag to create a rather obscure loop, until you get all legal inputs.
 
  • #6
And never, ever use Int.Parse without enclosing it in a try/catch, because otherwise your program can throw an obscure error message to the user upon bad input. When I was teaching, that would earn you an instant failing grade.
 
  • Like
Likes TheDemx27

FAQ: C# Adding Program - Learn Syntax & More

What is C# and why is it important for adding programs?

C# is a programming language developed by Microsoft. It is widely used for creating various types of applications, including adding programs. C# is important for adding programs because it offers a wide range of features and tools that make it easier and more efficient to write code for adding functionality to a program.

How do I declare variables in C#?

In C#, variables are declared using the var keyword, followed by the variable name and an equal sign. For example: var num = 5; This declares a variable named num and assigns it a value of 5.

Can you explain the syntax for adding two numbers in C#?

To add two numbers in C#, you can use the + operator. For example, if you have two variables num1 and num2, you can add them together and store the result in a third variable like this: var result = num1 + num2; The result variable will now contain the sum of num1 and num2.

What is the difference between int and double in C#?

int and double are both data types in C# used for storing numerical values. The main difference between them is the range of values they can hold. int can hold whole numbers between -2,147,483,648 and 2,147,483,647, while double can hold decimal numbers with a larger range of values.

How can I use conditional statements in my adding program in C#?

C# offers several conditional statements, such as if, else if, and switch, that allow you to control the flow of your program based on certain conditions. These statements can be used to add different numbers or perform different operations depending on the input or other variables in your program.

Similar threads

Replies
18
Views
2K
Replies
10
Views
2K
Replies
5
Views
3K
Replies
13
Views
4K
Replies
3
Views
1K
Replies
3
Views
3K
Replies
2
Views
1K
Replies
2
Views
1K
Replies
2
Views
2K
Back
Top