Home > Blockchain >  Method Pro(k) C#
Method Pro(k) C#

Time:10-22

I have to do this example for my course work and I'm unsure what should I have to do with last case.

Create a method Pro(k) that generates k number of random integers and returns the sum of those in the interval [-10, 10]. In the main method, enter 3 positive integers a, b and c. To find and print: Pro( |c-b| ) * Pro(a).

This is my code for now:

In Main()

        Console.Write("Enter k: ");
        int k = int.Parse(Console.ReadLine());
        Pro(k);

        Console.WriteLine("Enter а = ");
        int a = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter b = ");
        int b = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter c = ");
        int c = int.Parse(Console.ReadLine());

        Console.WriteLine(Pro(c-b) * Pro(a));

    //method Pro(k) is out of Main()
    public static int Pro(int k)
    {
        int sum = 0;
        for (int i = 1; i <= k; i  )
        {
            int num = int.Parse(Console.ReadLine());
            if (num <= 10 && num >= -10)
            {
                sum = sum   num;
            }
        }
        Console.WriteLine("Sum is: "   sum);
        return sum;
    }

CodePudding user response:

This should be the correct answer |c-b| = Math.Abs(c-b) and it said Generates, not insert inside pro function so you should use random with interval (-10,10) te get those numbers. Anyway you advance a lot! please a console.writeline inside a method that return a value, also is a bad practice, you should return it first and then print it! hope it helps you!

static void Main(string[] args)
    {
        Console.Write("Enter k: ");
        int k = int.Parse(Console.ReadLine());
        Console.WriteLine("Sum is: "   Pro(k));
        

        Console.WriteLine("Enter а = ");
        int a = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter b = ");
        int b = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter c = ");
        int c = int.Parse(Console.ReadLine());

        Console.WriteLine(Pro(Math.Abs(c - b))* Pro(a));
    }
    public static int Pro(int k)
    {
        int sum = 0; 
        Random rd = new Random();
        for (int i = 1; i <= k; i  )
        {
            int rand_num = rd.Next(-10,10);
            sum = sum   rand_num;   
        }
        return sum;
    }

Please for future questions check Conduct & How to ask so we can help you !

  • Related