I want to put the number of characters and put whether it even or odd

Asked

Viewed 1,664 times

1

But my code is going wrong.

namespace Impar_ou_parr
{
    class Program
    {
        static void Main(string[] args)
        {
            string NumLetras;
            Console.WriteLine("Digite uma Palavra: ");
            NumLetras = Console.ReadLine();
            Console.WriteLine("Sua palavrra tem " + NumLetras.Length + " letras!");

            int x;
            x = int.Parse(Console.ReadLine());
            if (x % 2 == 0)
            {
                Console.WriteLine("Sua palavra tem um numero par de letras");
            }
            else
            {
                Console.WriteLine("Sua palavra tem um numero impar de letras");
            }
            Console.ReadLine();
        }
    }
}
  • What kind of error and where the same is occurring?

  • I don’t understand what the doubt is. Say the mistake, what should happen, because you need to take this x.

  • x = Numletras. (Console.Readline()); is giving error n know why

  • should not be x = int. Parse(Numletters.Length), for x to receive the word size?

  • x = int.Parse(Numletras.Length), still giving error, I had already tried

  • What error is appearing?

  • cannot Convert from 'int' to 'string'

  • ah, x = Numletras.Length

  • Lenght already returns an int, no parse needed

  • @Genezis does not add details of the pargunta in the comments. Edit your question and put the comments there. So that someone, with the same problem, will read, will understand the whole scenario just by the question.

Show 5 more comments

5 answers

5

I believe you want this:

using static System.Console;

namespace Impar_ou_parr {
    public class Program {
        public static void Main(string[] args) {
            WriteLine("Digite uma Palavra: ");
            WriteLine($"Sua palavra tem um numero {(ReadLine().Length % 2 == 0 ? "par" : "ímpar")} de letras");
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I gave a modernized, organized and clean code.

I didn’t have to ask for a number to know if it’s even, I had to take the word size to see if it’s even or odd.

  • Daora this modernized organized and cleaned, was!

  • If you were to keep more or less in the original form, try to use variable names that give meaning to what it does and use naming conventions. http://answall.com/q/31646/101 and http://answall.com/q/13890/101. Avoid declaring the variable and assign afterwards and create unnecessary variables.

2


In case you don’t need to give a int.Parse, because the same is already an integer. Also you do not need to fetch the value again in the console, because you already have your word in the variable NumLetras. That said try the code below, it spun in my machine

namespace Impar_ou_parr
{
    class Program
    {
        static void Main(string[] args)
        {
            string NumLetras;
            Console.WriteLine("Digite uma Palavra: ");
            NumLetras = Console.ReadLine();
            Console.WriteLine("Sua palavrra tem " + NumLetras.Length + " letras!");

            int x;
            x = NumLetras.Length;
            if (x % 2 == 0)
            {
                Console.WriteLine("Sua palavra tem um numero par de letras");
            }
            else
            {
                Console.WriteLine("Sua palavra tem um numero impar de letras");
            }
            Console.ReadLine();
        }
    }
}
  • NICE, thank you very much!

1

namespace Impar_ou_parr { class Program { static void Main(string[] args) { string NumLetras;

    Console.WriteLine("Digite uma Palavra: ");

    NumLetras = Console.ReadLine();
    int letrasTotal = NumLetras.Lenght;
  Console.WriteLine("Sua palavra tem " + letrasTotal + " letras!");


    if (letrasTotal % 2 == 0)
    {
        Console.WriteLine("Sua palavra tem um numero par de letras");
    }
    else
    {
        Console.WriteLine("Sua palavra tem um numero impar de letras");
    }
    Console.ReadLine();
}
  • Error CS0103 The name 'Numletras' does not exist in the Current context

  • @Genezis I edited the answer now, see if it works now

1

I believe this would be it:

namespace Impar_ou_parr { 
class Program { 
static void Main(string[] args) { 
string Palavra;

        Console.WriteLine("Digite uma Palavra: ");

        Palavra = Console.ReadLine();
        int x;
        x = Palavra.Length;
      Console.WriteLine("Sua palavra tem " + x + " letras!");


        if (x % 2 == 0)
        {
            Console.WriteLine("Sua palavra tem um numero par de letras");
        }
        else
        {
            Console.WriteLine("Sua palavra tem um numero impar de letras");
        }
        Console.ReadLine();
    }
}
}
  • It was! Vlw ai!!!!!

0

Try to do it this way:

static void Main(string[] args)
{
    string NumLetras;
    Console.WriteLine("Digite uma Palavra: ");
    NumLetras = Console.ReadLine();
    Console.WriteLine("Sua palavrra tem " + NumLetras.Length + " letras!");

    int x;
    int.TryParse(NumLetras.Length.ToString(),out x);
    if (x % 2 == 0)
    {
        Console.WriteLine("Sua palavra tem um numero par de letras");
    }
    else
    {
        Console.WriteLine("Sua palavra tem um numero impar de letras");
    }
    Console.ReadLine();
}
  • Why convert a number to string and then convert it to number again?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.