Like Tree1Likes
  • 1 Post By nonoob

Determine how many numbers in a range are divisible by a third - loop help

This is a discussion on Determine how many numbers in a range are divisible by a third - loop help within the C Programming forums, part of the General Programming Boards category; Hello, the following may come off as a homework question but in reality I just need some help with loops. ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    10

    Determine how many numbers in a range are divisible by a third - loop help

    Hello, the following may come off as a homework question but in reality I just need some help with loops. I've pretty much finished the entire program, except for the actual calculation part.

    "Given a range of values determine how many integers within that range, including the end points, are multiples of a third value entered by the user. The user should be permitted to enter as many of these third values as desired and your output will be the sum of total multiples found."

    I've defined functions to take user input for the low range, high range and a do-while loop to take as many third inputs as the user wants (terminated by entering -1, as requested by the question)

    To actually calculate if they're divisible, I found out that if A%B = 0, then they are divisible, so I thought I would create a loop where each value in the range between A and B is checked against the third value to see if they output a zero.

    What I need to end up with is a program that tells the user how many integers are divisible by the numbers in the range, i.e:

    "Enter the low range value: 335

    Enter the high range value: 475
    Enter a value to check within the range: 17
    Enter a value to check within the range: -1
    There are 8 total values that are divisible by the numbers in the range."




    Going back to my original question, how would I create a loop or something to "check" how many values are equal to zero, and consequently increment a variable for each instance? (This is how I think it should be done)



    Really messy code incoming. As you can see towards the end the logic may be going offtrack, signs of my confusion taking place. If someone could at least send me on the right track I would greatly appreciate it.
    Code:
            #include <stdio.h> 
    //GLOBAL DECLARATIONS
    int getlowR();
    int gethighR(int);
    // int getloopThird();
    int checkDivisible(int, int);
    void printResult( int, int, int);
     
     
     
     
     
    int main(void)
    {
     //VARIABLE DECLARATIONS
     
     int lowRange = getlowR(); //Low range value, assigned by the function getlowR()
     int highRange = gethighR(lowRange); //High range values, assigned by the function gethighR()
    //  int thirdInteger = getloopThird(); //Third integer values returned by the function getloopThird()
     int numberRep = checkDivisible(lowRange, highRange);
     
     
     printResult(lowRange, highRange, numberRep);
     
      return(0);
    }
     
     
    int getlowR()
    {
      int lowRange;
     
     
      do
      {
       printf("Enter the low range value:");
       scanf("%d", &lowRange);
     
       if(lowRange < 0)
       {
         printf("\nError! Positive integers only!!\n\n");
       }
      }while(lowRange < 0);
     
      return(lowRange);
    }
     
     
    int gethighR(int lowRange)
    {
      int highRange;
     
     
      do
      {
        printf("Enter the high range value:");
        scanf("%d", &highRange);
     
     
        if(highRange < lowRange)
        {
          printf("\nError! The high range value must be greater than the low range value!\n\n");
        }
      }while(highRange < lowRange);
     
      return(highRange);
    }
     
     
    /*int getloopThird()
    {
      int thirdInteger;
     
     
      do {
     
      printf("Enter a value to check within the range:");
     
      scanf("%d", &thirdInteger);
      } while (thirdInteger != -1);
     
      return(thirdInteger);
    }
    */
     
    int checkDivisible(int lowrange, int highrange)
    {
      int numberRep;
      int thirdInteger;
     
      do
      {
      printf("Enter a value to check within the range:");
      scanf("%d", &thirdInteger);
     
      if (thirdInteger < -1)
      {
        printf("\n Error! Third integer cannot be less than -1!!\n\n");
      }
     
      while (lowrange<highrange)
      {
        lowrange++;
        numberRep = lowrange % thirdInteger;
      }
     
      } while (thirdInteger != -1);
     
     
      return(numberRep);
    }
     
     
    void printResult(lowRange, highRange, numberRep)
    {
      printf("Low range: %d, High range: %d, Third integer: , Reps: %d\n\n", lowRange, highRange, numberRep);

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    I suggest learning about for loops.

    Edit: Added link For, While and Do While Loops in C - Cprogramming.com

    Edit2: I suggest making a function that contains the for loop and return how many times it is divided with mod zero.

    Code:
    int checkDivisible(int lowrange, int highrange, int DivideByInteger)
    Edit3: I am guessing this line is wrong
    Code:
    numberRep = lowrange % thirdInteger;
    More likely
    Code:
    if (0 == lowrange % thirdInteger)
    {
       numberRep++;
    }

    Tim S.
    Last edited by stahta01; 03-01-2013 at 01:06 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    If a user enters more than one "third value", do all the numbers in the range have to multiples of ALL those, or just some?
    The problem is not stated clearly.
    stahta01 likes this.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Quote Originally Posted by stahta01 View Post
    snip
    Adding
    Code:
    if (0 == lowrange % thirdInteger)
    {
       numberRep++;
    }
    to my code fixed it, but it is only capable of calculating how many numbers are divisible with one third integer value.

    Also, they want the sum of all the numbers that are divisible with as many user inputted values as they want.

    Here's another example:

    Enter the low range value: 100
    Enter the high range value: 175
    Enter a value to check within the range: 10
    Enter a value to check within the range: 2
    Enter a value to check within the range: 19
    Enter a value to check within the range: -1
    There are 50 total values that are divisible by the numbers in the range.

    So basically what they want is (I think): (how many numbers are divisible by value 1?) + (how many numbers are divisible by value 2?) + ... (how many numbers are divisible by value x?) until the user inputs -1, in which case the loop ends and the user gets the value.

    Do I use the '+=' operator? I've seen this a few times and it seems as if it could be of use in this question.







  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Just keep 'third integers' in an array and loop through.
    A += N is equivalent to A = A + N. It makes such a common use a bit shorter especially if the 'A' variable is some ugly array/structure and index expression that's bad enough if mentioned once let alone twice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. determine if numbers are prime
    By royaro in forum C Programming
    Replies: 6
    Last Post: 02-13-2011, 12:04 PM
  2. Displaying Numbers divisible by user's desired number
    By DaniiChris in forum C Programming
    Replies: 9
    Last Post: 07-07-2008, 02:06 PM
  3. Simple C program displaying all numbers divisible by 6
    By DaniiChris in forum C Programming
    Replies: 25
    Last Post: 07-06-2008, 12:25 PM
  4. using rand() to create a range of numbers
    By keira in forum C Programming
    Replies: 7
    Last Post: 09-11-2007, 01:45 AM
  5. Random Numbers within a range OTHER than 1-X
    By Kaelin in forum C++ Programming
    Replies: 11
    Last Post: 02-16-2005, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21