Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
2.33/5 (4 votes)
See more:
Given the number 10 we get the output 1,2,3,4,5,6,7,8,9,10 or 5 we get 1,2,3,4,5
Posted
Updated 11-Mar-14 21:03pm
v3
Comments
Alexander Dymshyts 11-Mar-14 4:01am    
You suggest us to write a program for you?
CPallini 11-Mar-14 4:32am    
As an additional task, write a recursive function to do that.

Try this

C#
string input = "10";
      //input = "1-5";
      int min = 0, max;
      string[] items = input.Split('-');
      if (items.Length == 2)
      {
          max = Convert.ToInt32(items[1]);
          min = Convert.ToInt32(items[0]);
      }
      else
      {
          min = 1;
          max = Convert.ToInt32(items[0]);
      }
      string output = "";
      for (int i = min; i <= max; i++)
          output += i + ",";

      output = output.TrimEnd(',');
      Console.WriteLine(output);



      Console.ReadLine();
 
Share this answer
 
Here is how you would do it in C:
C++
/* Prints a sequence of natural numbers determined by the input */
void printNaturals(unsigned int num)
{
  /* Declaration of variables */
  unsigned int idx;

  /* Ensure we have a natural number */
  if (num > 0)
  {
    for (idx = 1; idx <= num; idx++)
    {
      /* Print unsigned integers */
      printf("%u,", idx);
    }
  
    /* Perform housekeeping */
    printf("\n");
  }
} /* printNaturals */
 
Share this answer
 
v8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900