-1

How to make output : 1 1 2 6 3 11 4 16 5 21. when i input start value = 1, and end value = 5

my code :

                Console.Write("input start value : ");
                start = int.Parse(Console.ReadLine());
                Console.Write("input end value : ");
                end = int.Parse(Console.ReadLine());
                Console.WriteLine("");

                for (int i = start; i <= end; i++)
                {
                    Console.WriteLine(i);
                    
                    for (int j = i; j <= end; j++)
                    {
                        int z = 1;
                        if (start != j)
                        {
                            z++;
                            Console.WriteLine((j * j) + z);
                        }
                        else
                        {
                            Console.WriteLine(start + " this j start value");
                        }
                    }
                }
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
im robot
  • 21
  • 3
  • Interleave `1, 2, 3, 4, 5` and `1, 6, 11, 16, 21`. – dxiv Mar 23 '21 at 05:48
  • can u fixing my code?? i dont understand – im robot Mar 23 '21 at 06:17
  • The code you posted is not fixable, since it doesn't seem to be related to the rest of the question at all. My first comment was only meant as a clue. See what you can make of it, then post back if you really get stuck, but at least show that you tried. – dxiv Mar 23 '21 at 06:44

2 Answers2

0

As @dxiv has posted in the comments, the pattern for the set of numbers is to combine 1,2,3,4,5 and 1,6,11,16,21. The pattern that I see is that the gap between the second set of numbers is equal to the ending number.

We can define a function which generates these numbers:

IEnumerable<int> GetNumbers(int start, int end)
{
    for (int number = start; number <= end; number++)
    {
        yield return number;
        yield return start + ((number - 1) * end);
    }
}

And can output the results like:

int start = 1;
int end = 5;

Console.WriteLine(string.Join(' ', GetNumbers(start, end)));

Output

1 1 2 6 3 11 4 16 5 21
Hayden
  • 2,902
  • 2
  • 15
  • 29
  • 2
    Worth reading: https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – Caius Jard Mar 23 '21 at 06:23
0

So it's not entirely clear to me if the 5 is used both as an end value for the 1,2,3,4,5 as well as a difference value for the 1,6,11,16,21 but I'll assume yes. Here's an algorithm for you to implement (this looks like homework, so think of this as a tip - you'll get more out of doing the coding yourself but this is how you should approach any coding exercise: write out the algorithm in the language you think in then translate it to c#)

  • ask the user for a start value and convert to int
  • ask the user for an end value and convert to int
  • work out a variable called n which is end minus start
  • make a for loop starting at x = 0, running while x is less than or equal to n ; incrementing x by 1
  • print out startValue plus x
  • print out startValue plus (endValue times x)
  • loop around

For a start and end of 1 and 5, the loop runs from 0 to 4. The first time the loop runs, x is 0, startValue is 1, so a 1+0 and a 1+(5*0) are printed - both 1. This continues up to the end value where x is 4, 4+1 is printed - which is 5 - and 1+(4*5) is printed - which is 21

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • why my output : 1 1 2 6 3 11 4 16 5 21 6 26 when i set start = 1, and end = 5 – im robot Mar 23 '21 at 07:15
  • my code int n = end - start; for (int i = 0; i <= end; i++) { if(i == 0) { Console.WriteLine(start + i); Console.WriteLine(start+ (end * i)); } while (i <= n) { i++; Console.WriteLine(start + i); Console.WriteLine(start + (end * i)); } } – im robot Mar 23 '21 at 07:16
  • Your code does `i<=end` in the for loop, but it should be `i<=n` – Caius Jard Mar 23 '21 at 07:20
  • ohh yes, because its <=, should be < ... okay thank you! – im robot Mar 23 '21 at 07:22