Home > Blockchain >  How to properly count multiples of a factor within a range with a for loop? (C# Beginner)
How to properly count multiples of a factor within a range with a for loop? (C# Beginner)

Time:11-04

Given 3 int's "start", "end", and "factor", using a loop, count how many multiples of "factor" occur between start and end (inclusive).

I was given this question on a lab for my first programming class. I've been stuck on it for a few days now, and whenever I reach out for help with what I could be doing wrong, I'm given really vague "yep somethings definitely not right there" responses and I get stuck in an endless loop of trying to figure out what's wrong so I can google it.

I've tried a few different iterations of code, some have come close but still not right.

public static int Test1(int start, int end, int factor)
        {
            
            for(int i = start; i <= end; i  )
            {
                int result = i % factor;                    
                {
                    return i;
                }
            }

            return 0;
        }
public static int Test1(int start, int end, int factor)
        {
            
            for(int i = 0; start <= end; i  )
            {
                if(start   factor <= end)
                {
                    factor  ;
                    continue;
                }
                else
                {
                    return i;
                }
            }

            return 0;
        }
 public static int Test1(int start, int end, int factor)
        {
            for (int i = 0; start <= end; i  )
            {
                if (start   factor < end)
                {
                    start =factor;
                }
                else
                {
                    return i;
                }

            }

            return 0;
        }

Expected result: if (for example) start = 14; end = 35; factor = 3; result should equal 7 multiples of 3, outputting 7.

CodePudding user response:

This is the answer. what I did is I made a loop from start to end and in math we know that the multiple of somthing times the amount of times it is devided in equals the original number(int this case a number between start to end) so I made a condition that reflects that and finally returned count.

public static int Multiple(int start , int end , int factor)
    {
        int count = 0;
        for(int i = start; i <= end; i  )
        {
            if(i == factor * (i / factor))
            {
                count  ;
            }
        }
        return count;
    }

CodePudding user response:

Your code here is maybe the most near :

public static int Test1(int start, int end, int factor)
        {
            
            for(int i = start; i <= end; i  )
            {
                int result = i % factor;                    
                {
                    return i;
                }
            }

            return 0;
        }

But the problem is when you find a factor, you return "i". About other examples I don't understand how it may work.

Just use that :

public static int Test1(int start, int end, int factor)
{
    int numberFound = 0;
    for (int i = start; i <= end; i  )
    {
        if( i % factor==0);
        {
            numberFound  ;
        }
    }

    return numberFound;
}

When i

  • Related