-2

I have problem with solution in this example.

Input:>6 
Output:0+1+2+3+4+5+6 = 21 

Input:> -15 
Output:-15<0  

Input:>0  
Output:0=0
public static string ShowSequence(int n)
{
    int sumInt = 0;
    string sum = "";
    for (int i = 0; i <= n; i++)
    {
        if (n == 0)
        {
            sum += i + "=";
        }
        else if (n < 0)
        {
            sum += n + "<";
        }
        else
       if (i == n)
        {
            sum += i + " = ";
        }
        else
            sum += i + "+";
        sumInt += i;
    }
    sum += sumInt;
    return sum;
}

Everything works except a negative number , because my program return 0 not -15<0. Thanks for help !

xdtTransform
  • 1,986
  • 14
  • 34
Kajman
  • 13
  • 3

3 Answers3

2

You check your input in the loop, and that's where it goes wrong. I modified your code, and pulled the check out of the for-loop, and returned early to avoid confusion.

https://dotnetfiddle.net/gLmo62

public static string ShowSequence(int n)
    {
        if (n == 0)
        {
            return n + "=";
        }

        if (n < 0)
        {
            return n + "<";
        }

        int sumInt = 0;
        string sum = "";
        for (int i = 0; i <= n; i++)
        {
            if (i == n)
            {
                sum += i + " = ";
            }
            else
            {
                sum += i + "+";
            }

            sumInt += i;
        }

        sum += sumInt;
        return sum;
    }
Natrium
  • 30,772
  • 17
  • 59
  • 73
0

Your Loop Condition i <= n; fails before the start of first iteration when the number is negative. For this reason, the execution never reaches the if/else conditions.

You could rewrite the method with condition checks prior to entering the loop.

public static string ShowSequence(int n)
{
        if (n == 0)
        {
            return n + "=";
        }

        if (n < 0)
        {
            return n + "<";
        }

        int sumInt = 0;
        StringBuilder sum = new StringBuilder();
        for (int i = 0; i <= n; i++)
        {
            sum.Append($"{i}{(i==n?" = ":"+")}");
            sumInt += i;
        }
        sum.Append(sumInt);
        return sum.ToString();
}

Please note I have also used StringBuilder, instead of strings. The string class is immutable, which means that each time you append/modify a string, you are essentially creating a new string object in memory. In situation like the case in hand, where the string is repeatedly modified, you should avoid the overhead of creating string for every modification. For this purpose, you could use the StringBuilder class.

As an additional note, you could further reduce the code by making use of Linq. For example,

public static string ShowSequence(int n)
{
    if (n == 0)
    {
        return n + "=";
    }

    if (n < 0)
    {
        return n + "<";
    }

    var strBuilder = new StringBuilder();
    strBuilder.Append(Enumerable.Range(0, n+1)
                               .Select(x => x.ToString())
                               .Aggregate((a, b) => a + "+" + b));
    strBuilder.Append($" = {(n*(n+1))/2}");
    return strBuilder.ToString();
}
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
0

You can simply state the special case: n == 0, `n < 0.
Then use Enumerable.Range to generate all number from 0 to n.
And String.Join, without the "Is that the last elements? Do I need one more + ?".
And Enumerable.Sum to compute the sum.

You will end up with a really straight forward code.

public static string ShowSequence(int n)
{
    if (n == 0)
    {
        return n + " = 0";
    }

    if (n < 0)
    {
        return n + " < 0";
    }

    // generate all numbers from 0 to n. Takes n+1 steps. 
    var enumeration = Enumerable.Range(0, n+1);

    var plusOperatorConcatenation = String.Join(" + ",  enumeration ) ;

    return plusOperatorConcatenation + " = " + enumeration.Sum();
}

Live Demo: https://dotnetfiddle.net/Iu0vyf

xdtTransform
  • 1,986
  • 14
  • 34