Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Method Pro(k) C#

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
    }

>Solution :

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 !

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading