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.