Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I can't declare a C# method

+7 votes
asked Jul 6, 2023 by Mateo Soares Rodrigues (190 points)

I really would appreciate if someone could help to find a solution to the following code:

using System;
using System.Collections.Generic;

class HelloWorld {
  static void Main() {
        Console.WriteLine("Calculador de Números Primos");
        int n = Console.ReadLine("Escreva um número inteiro:\n");
    
        public bool isPrimo(int x) {
            var dvs = new List<int>();
            for (int d = 1; d < x+1; d++) {
                if (x % d == 0) {
                    dvs.Add(d);
                }
            }
            
            if (dvs.Count == 2) {
                return true;
            }
            
            return false;
        }
        
        for (int a = 1; a < n; a++) {
            if (isPrimo(a)) {
                Console.WriteLine(a);
            }
        }
        
        if (isPrimo(n)) {
            Console.WriteLine($"O número {n} É PRIMO");
        } else {
            Console.WriteLine($"O número {n} NÃO É PRIMO");
        }
    }
}

The problem is that it doesn't understand the code in bold as a method declaration.

2 Answers

0 votes
answered Jul 10, 2023 by Peter Minarik (86,240 points)

I've fixed up your code with the following changes:

  1. Your class IsPrimo() should be outside of the Main() function.
  2. Your function IsPrimo() should be declared as a static function as you do not have a class instance of HelloWorld.
  3. Console.ReadLine() does not take any argument. You should print your request before calling ReadLine().
  4. Console.ReadLine() returns a string. To get a number (int), you need to parse it.
using System;
using System.Collections.Generic;

class HelloWorld
{
    public static bool IsPrimo(int x)
    {
        var dvs = new List<int>();
        for (int d = 1; d < x + 1; d++)
        {
            if (x % d == 0)
            {
                dvs.Add(d);
            }
        }
        if (dvs.Count == 2)
        {
            return true;
        }
        return false;
    }

    static void Main()
    {
        Console.WriteLine("Calculador de Números Primos");
        Console.WriteLine("Escreva um número inteiro:");
        int n = int.Parse(Console.ReadLine());
        for (int a = 1; a < n; a++)
        {
            if (IsPrimo(a))
            {
                Console.WriteLine(a);
            }
        }
        if (IsPrimo(n))
        {
            Console.WriteLine($"O número {n} É PRIMO");
        }
        else
        {
            Console.WriteLine($"O número {n} NÃO É PRIMO");
        }
    }
}
0 votes
answered Jul 13, 2023 by Gulshan Negi (1,580 points)

Hello, this is Gulshan Negi

Well, I searched about it on internet and I found another way to do what you are looking for, you just need to replace your code with the below code. 

using System;
using System.Collections.Generic;

class HelloWorld {
    static void Main() {
        Console.WriteLine("Calculador de Números Primos");
        Console.WriteLine("Escreva um número inteiro:");
        int n = Convert.ToInt32(Console.ReadLine());
        
        HelloWorld helloWorld = new HelloWorld();
        
        for (int a = 1; a < n; a++) {
            if (helloWorld.IsPrimo(a)) {
                Console.WriteLine(a);
            }
        }
        
        if (helloWorld.IsPrimo(n)) {
            Console.WriteLine($"O número {n} É PRIMO");
        } else {
            Console.WriteLine($"O número {n} NÃO É PRIMO");
        }
    }
    
    public bool IsPrimo(int x) {
        var dvs = new List<int>();
        for (int d = 1; d < x + 1; d++) {
            if (x % d == 0) {
                dvs.Add(d);
            }
        }
        
        return dvs.Count == 2;
    }
}

I hope it will work.

Thanks 

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...