Hello!

I have a problem with my code. I don't know how to make that the code will include a subroutine which returns a random number between the two entered numbers as a result. Do anyone know how to make it?

The code is:

static void Main(string[] args)
        {
            Console.Write("Enter the first number: ");
            int num1 = int.Parse(Console.ReadLine());
            Console.Write("Enter the second number: ");
            int num2 = int.Parse(Console.ReadLine());

            for (int i = 0; i < 100; i++)
            {
                Random rand = new Random();
                int randomNumber = rand.Next(num1, num2);
                Console.WriteLine("The random number is: {0}", randomNumber);
                Console.ReadKey();
            }
        }
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 

 
static void Main(string[] args)
{
int a,b;

time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
Console.Write("Enter the first number: ");
int num1 = int.Parse(Console.ReadLine());
Console.Write("Enter the second number: ");
int num2 = int.Parse(Console.ReadLine());



if(rand() % 2)
 Console.WriteLine("The random number is: {0}", num1);
 else
 Console.WriteLine("The random number is: {0}", num2);

exit(EXIT_SUCCESS);
}

I hop[e it will help you, this select randomly one of two enter number

I don't see any subroutine here. Btw, is your code rly in C# language?

It's not C#, it's a mixture of C/C++ and C#.

And to help you (maybe), show me a method that takes two parameters and returns the difference between the two. We'll convert this to a random number soon, just want to see if you know how to create methods.

I'm not rly good with creating methods. Thats why I'm asking for help.
I want to learn it by examples.

So sorry, its in c++..............

static void Main()
        {
            int num1, num2;
            num1 = 50;
            num2 = 65;
            if(num1 %2==1)
                Console.WriteLine(num1 );
            else
                Console.WriteLine(num2);
            Console.ReadLine();
        }

        static Random _r = new Random();
        static int  F()
        {
            int n = _r.Next(676);
            return n;
        }

I hope it will help you

static void Main()
        {
            int num1, num2;
            num1 = 50;
            num2 = 65;
            if(num1 %2==1)
                Console.WriteLine(num1 );
            else
                Console.WriteLine(num2);
            Console.ReadLine();
        }

        static Random _r = new Random();
        static int  F()
        {
            int n = _r.Next(676);
            return n;
        }

Sorry once again for wrong post

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.