Read by CSharp console

6

I am starting in the world of programming and I have just solved the following exercise.

Ingresar un número y mostrar la suma de los números que lo anteceden.
Al ingresar el 5 me da como resultado el 10

.

As I do so that by entering the console for example the 5 , I show 1+2+3+4 = 10 , I leave the code you make.

int num, suma = 0;

Console.Write("Ingresa un número:");
num = Convert.ToInt32(Console.ReadLine());

for (int i = 0; i < num; i++)

   suma = suma + i;

   Console.Write("La suma de los números que anteceden a el número: " + num + " es " + suma + ".");
   Console.ReadKey();
    
asked by Christopher Mendivel 05.08.2018 в 21:41
source

3 answers

4

You can do with a for cycle, concatenating the value of the cycle in each round, the program would be as follows:

        Console.Write("Ingresa un número: ");
        int num = Convert.ToInt32(Console.ReadLine());
        int suma = 1;

        Console.Write("La suma de los números anteriores a " + num + " es: 1");
        for (int i = 2; i < num; i++)
        {
            suma += i; //Para poder mostrar los números desde el uno, ya que i en un inicio va a valer 0
            Console.Write(" + " + i);
        }
        Console.Write(" = " + suma);

        Console.ReadKey();

The variable suma is initialized to 1 for the following reasons:

1) We already know that before any number is always going to be number one, then it's too much for us to put it in the for cycle later.

2) To be able to concatenate the "+" signs in each round, these have to be written at the beginning of each Console.Write() within the for cycle, so it is necessary to have a number already written before. Let me explain: Before starting the for cycle, write the sentence "The sum of the previous numbers to" + num + "is: 1" since, having that one there, we can concatenate, within the for, at the beginning of each writing a plus sign, since we know that there will always be a number before. This helps us so that, at the end of the cycles of the for cycle, no more signs appear. If we leave the plus sign at the end of the writing of the for cycle, it would be this way (in the case that num = 5): 1 + 2 + 3 + 4 + = 10

I hope it serves you, any doubt we are here to serve.

    
answered by 05.08.2018 / 23:17
source
2

Test stored all the values in a variable with its symbol and at the end you print it as you wish ... like this:

int num, suma = 0;
string result="";

Console.Write("Ingresa un número:");
num = Convert.ToInt32(Console.ReadLine());

for (int i = 0; i < num; i++){

   suma = suma + i;
    if (i==0) {
        result = result + i;
    }else{
        result= result + "+" +i;
    }

   Console.Write("La suma de los números que anteceden a el número: " + num + " es " + suma + ".");
   Console.ReadKey();
}

Console.Write("La suma de los números que anteceden a el número: " + result + " = " + suma + ".");
Console.ReadKey();

I hope you serve and marques, ReNiceCode ...

    
answered by 05.08.2018 в 23:06
1

Hello, change your code for this:

int num, suma = 0;

Console.Write("Ingresa un número:");
num = Convert.ToInt32(Console.ReadLine());

for (int i = 0; i < = num; i++) // añade el = en la condición 
    suma = suma + i;

Console.Write("La suma de los números que anteceden a el número: " + num + " es " + suma + ".");
Console.ReadKey();
    
answered by 06.08.2018 в 11:41