Home > front end >  Create function that creates and return a list of multiples of a number
Create function that creates and return a list of multiples of a number

Time:01-13

so I am super new to c# and to programming in general. Anyways I am trying to create a function that creates and returns a list of multiples of a number. For example, MultiplesOf(3,5), where the 3 is the starting number and 5 is the number of multiples, would result in {3, 6, 9, 12, 15}. This is what I have so far:

private static double[] MultiplesOf(double number, int length)     
{
    List<int> results = new List<int>();                           
    // TODO Problem 1 Start                                        
    for (int i = 1; i <= length; i  )                             
    {
        if ( i % number == 0)                                     
            results.Add(i);                                        
    }
    return results;                                                
}                 

Any suggestions would be greatly appricated!

CodePudding user response:

Instead of dividing the length by the current number i, why not just add the number to the previous number? Multiplying something is nothing different than just adding it again and again, is it? So the term 3 * 3 is the same like 3 3 3.

List<int> results = new List<int>();                           
// TODO Problem 1 Start                                        
for ( int i = 1; i <= length; i  )                             
{                     
    number  = number;                                         
    result.Add(number);                                         
}                                                              
return results; 

Apart from this your method should return List<int>, instead of double[].

CodePudding user response:

you just need to multiply the length[i] by the number you want to find the multiples on.

public List<int> MultiplesOf(int number, int length)     
{                                                                  
      List<int> results = new List<int>();  
      for ( int i = 1; i <= length; i  )                                                                                         
      {    
          results.Add(i * number);
      }                                                              
      return results;                                                
    }                                                                                                             
} 

CodePudding user response:

for ( int i = 1; i <= length; i  )                             
  {                                                                                           
          results.Add(i*number);                                        
  }                                                              
  return results;

CodePudding user response:

If you want to keep the return type of the method to array of double. please try this way

private static double[] MultiplesOf(double number, int length)
    {
        //return type of the method is array of double data type, so we are declaring the return variable with double[] datatype.
        double[] results = new double[length];
                                              
        for (int i = 1; i <= length; i  )
        {
            //instead of mod operation, you need to multiply the number and add to array.
            results[i-1] = (number*i);
        }
        return results;

    }

CodePudding user response:

You can modify your function like below:

    public static List<int> MultiplesOf(int number, int length)
    {
        List<int> results = new List<int>();
        // TODO Problem 1 Start                                        
        for (int i = 1; results.Count <= length - 1; i  )
        {
            if (i % number == 0)
                results.Add(i);
        }
        return results;
    }

This for loop above makes length times control. If you want a more performance way you can modify your function like this:

    public static List<int> MultiplesOf(int number, int length)
    {
        List<int> results = new List<int>();
        // TODO Problem 1 Start                                        
        for (int i = 1; results.Count <= length - 1; i  )
        {
            results.Add(i * number);
        }
        return results;
    }

For see the output

var x = MultiplesOf(3,5);

foreach (var item in x)
{
    Console.Write($"{item}, ");
}
  •  Tags:  
  • c#
  • Related