简体   繁体   中英

C# -sum of two numbers, then ask for new number again and add

new to C# and programming and been practicing on loops.

Im trying to build a program that will initially ask for 2 numbers, then it will output the sum, then will ask for another number again, then add to the previous result. The loop will only stop when the user input 00.

Here is the code that i thought of, apologies for the poor coding yet. ><

Please suggest/use any loop that you may think that is efficient for this. Thanks!

 public static void getnum()
        {
            Console.Write("Enter number: ");
            int num = Int32.Parse(Console.ReadLine());

            Console.Write("Enter number: ");
            int num2 = Int32.Parse(Console.ReadLine());
        }

        static void Main(string[] args)
        {
            getnum(); 

            while (num!=00)
            {
                getnum();
                int sum = num + num2;
                Console.WriteLine("Sum is: " + sum);
            }
            Console.Read();

You can simplify it something like this

static void Main(string[] args)
        {
            int sum = 0;
            string currentNumber = "0";  
            while (currentNumber!="00")
            {
                int num = Int32.Parse(currentNumber);
                sum += num;
                Console.WriteLine("Sum is: " + sum);
                currentNumber = Console.ReadLine();
            }
            Console.Read();
        }

Here is a sample which does what you want:

 var answer = 0;
        var num = 0;
        var num2 = 0;

        while(answer!=1)
        {
            Console.Write("Enter number: ");
            num = Int32.Parse(Console.ReadLine());

            Console.Write("Enter number: ");
            num2 = Int32.Parse(Console.ReadLine());

            var sum = num + num2;
            Console.WriteLine("Sum is: " + sum);
            Console.Write("Press 1 to exit or 2 to continue: ");
            answer = Int32.Parse(Console.ReadLine());
        };

This code will work.

 class Program
{
    static int num1 = 0;
    static int num2 = 0;
    public static void getnum()
    {
        Console.Write("Enter number: ");
         num1 = Int32.Parse(Console.ReadLine());

        Console.Write("Enter number: ");
         num2 = Int32.Parse(Console.ReadLine());
    }

    static void Main(string[] args)
    {
        getnum();

        //This is the loop.          
        do
        {
            ShowNum();
            getnum();
        } while (num1 != 00);
    }

    static void ShowNum()
    {            
        int sum = num1 + num2 ;

        //Here we show the Sum.
        Console.WriteLine("Sum is: " + sum.ToString());         
    }
}

You can try this too

        string input = "";
        int Sum = 0, newNumber;
        while (input != "00")
        {
            Console.Write("Enter number(press 00 to print the sum): ");
            input = Console.ReadLine();
            if (!Int32.TryParse(input, out newNumber))
            {
                Console.WriteLine("Input is not a valid number; 0 is taken as default value");
            }
            Sum += newNumber;
        }
        Console.WriteLine(" Sum of entered numbers {0}",Sum);
        Console.ReadKey();

You need two things.

  • Read atleast two numbers.

  • Sum input numbers until user press '00'

.

public static void Main()
{
    int numCount = 0;
    int sum = 0;

    string input;
    do
    {

        Console.Write("Enter number: ");
        input = Console.ReadLine();
        int num;
        if (int.TryParse(input, out num))
        {
            sum += num;
            numCount++;
            Console.WriteLine("Sum is: " + sum);
        }
    } while (!(numCount > 2 && input == "00")); // Make sure at least two numbers and until user rpess '00'
}

Working Demo

You can also try with this

int input = 0;
int total = 0;

while (true)
{
    Console.WriteLine("Enter a number (Write Ok to end): ");

    string write = Console.ReadLine();
    if (write == "Ok")
    {
        break;
    }
    else
    {
       input = int.Parse(write);
       total = total + input;
    }

    Console.WriteLine("Total: " + total);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM