Search code examples
c#visual-studio-2019average

promblem while trying to define two numbers in c# programe for average stock calculator


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace average_stock_calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal  a, b, c, d, sum, average;
         

            Console.WriteLine("First purchase price:");
         
            a = Convert.ToDecimal(Console.ReadLine());
            
            Console.WriteLine("Second purchase price:");
            b = Convert.ToDecimal(Console.ReadLine());

            Console.WriteLine("Third purchase price:");
            c = Convert.ToDecimal(Console.ReadLine());

            Console.WriteLine("Fouth purchase price:");
            d = Convert.ToDecimal(Console.ReadLine());

            //Processing
            sum = a + b + c + d;
            average = sum/4;
            
            Console.WriteLine("Average buying price={0}", average);

            Console.ReadKey();
        }
    }
}

note: i want to add first purchase quntity and first purchase price together, i have no idea how to do it! can someone help? it should look like this, enter first purchase quntity & price and i shuold be able to enter quntity 10 and price 20 together, and then at the end i should get the average buy quantity and price together.


Solution

  • I made a simple modification for you.

    Codes:

    using System;
    
    namespace ConsoleApp9
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                Purchase a, b, c, d;
                Console.WriteLine("First purchase price and amount:");
                a = ReadL();
                Console.WriteLine("Second purchase price and amount:");
                b = ReadL();
                Console.WriteLine("Third purchase price and amount:");
                c = ReadL();
                Console.WriteLine("Fouth purchase price: and amount:");
                d = ReadL();
    
                //Processing
                Purchase Sum = new Purchase();
                Sum.price = a.price + b.price + c.price + d.price;
                Sum.amount = a.amount + b.amount + c.amount + d.amount;
    
                Purchase Average = new Purchase();
                Average.price = Sum.price / 4;
                Average.amount = Sum.amount / 4;
    
                Console.WriteLine("Average buying price={0},Average buying amount={1}", Average.price, Average.amount);
    
                Console.ReadKey();
    
                Purchase ReadL()
                {
                    Purchase X = new Purchase();
                    var inputs = Console.ReadLine().Split(' ');
                    X.amount = Convert.ToDecimal(inputs[0]);
                    X.price = Convert.ToDecimal(inputs[1]);
                    return X;
                }
            }
            class Purchase
            {
                public decimal price { get; set; }
                public decimal amount { get; set; }
            }
        }
    }
    

    Output:

    enter image description here