André
André

Reputation: 111

Error: Type or namespace definition, or end-of-file expected

i'm trying to make a program to calculate prime numbers but i get this error "Type or namespace definition, or end-of-file expected"

here's the code:

    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                public string PrimeNumbers(int n)
            {
                string result = n.ToString();
                try
                {
                    for (int i = 2; i < n; i++)
                    {
                        int rest = n % i;
                        if (rest == 0)
                        {
                            resultado = n + "isn't a prime number";
                            i = n + 1;
                        }
                        else
                        {
                            resultado = n + "it's a prime number";
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error checking prime number");
                    Console.WriteLine(e);
                }
                Console.WriteLine(result);
                Console.Read();
                return result;
            }
            }
        }
    }

And the braket from Main is giving me an error also "} expected", however i add or remove brakets, the error still there

Upvotes: 0

Views: 2752

Answers (1)

Vladimirs
Vladimirs

Reputation: 8609

You cannot have method declared within another method, I belie it should looks something like:

namespace ConsoleApplication2
{
    public class Program
    {
        public static void Main()
        {
                PrimeNumbers(100);
        }

        public static string PrimeNumbers(int n)
            {
                string result = n.ToString();
                try
                {
                    for (int i = 2; i < n; i++)
                    {
                        int rest = n % i;
                        if (rest == 0)
                        {
                            result = n + "isn't a prime number";
                            i = n + 1;
                        }
                        else
                        {
                            result = n + "it's a prime number";
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error checking prime number");
                    Console.WriteLine(e);
                }
                Console.WriteLine(result);
                return result;
            }
    }
}

Upvotes: 2

Related Questions