Thread: Displaying Numbers divisible by user's desired number

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    48

    Exclamation Displaying Numbers divisible by user's desired number

    well...I was doing a program to ask the user from the range he wants the program to generate numbers from and also ask the user for a number and then generates all the numbers divisible by this number I did this so far but not working well...its always in a never-ending loop...Any Help?? Suggestion??

    Code:
    Code:
    #include <stdio.h>
    int main()
    {
    int x,ul,ll,j=0;
    
    printf("Please enter your desired number you want its divisible numbers\n");
    scanf("%i",&x);
    
    
    printf("Please enter the lower limit you desire\n");
    scanf("%i",&ll); //ALERT: getchar(); --> its only with characters!
    
    printf("Please enter the upper limit you desire\n");
    scanf("%i",&ul);
    
    
    	for (j <= ll; j <= ul; j++)
    	{
    		if (x < j)
    		{
    	if (j % x == 0)
    			{
    				printf("\t%i\t", x);
    				j++;
    
    			}
    		}
    		else
    		{       if ( j == x)
    			{
    				j = 0;
    				printf("\n");
    			}
    		}
    	}
    
    	return (0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Shouldn't you be using your previous program (the one where things are divisible by 6), and replacing all those 6's with your user input variable?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    well..i have a problem in the lower and upper limit thing

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    Well..I got this so far

    Code:
    #include <stdio.h>
    int main()
    {
    int x,ul,ll,j=1;
    
    printf("Please enter your desired number you want its divisible numbers\n");
    scanf("&#37;i",&x);
    
    
    printf("Please enter the lower limit you desire\n");
    scanf("%i",&ll); //ALERT: getchar(); --> its only with characters!
    
    printf("Please enter the upper limit you desire\n");
    scanf("%i",&ul);
    
    
    	for (j >= ll && j <= ul; j--)
    	{
    		if (j < x)
    		{
    	if (j % x  == 0)
    			{
    				printf("\n%i", x);
    				j++;
    
    			}
    		}
    		else
    		{       if ( j == x)
    			{
    				j = 0;
    				printf("\t\t");
    			}
    		}
    	}
    
    	return (0);
    }

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    I think the error is in here

    Code:
    for (j >= ll && j <= ul; j--)
    or what do you think? I truely dunno ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, I see that you have been trying hard, so I am going to give you an example of what you have been trying to do. However, I have included a bug, namely, the program does not print out the multiples of the number in the given range, unless the lower limit of the range is itself a multiple of the number. Your task is to fix this bug.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int num, lower, upper;
        int i;
        printf("Enter the integer: ");
        scanf("&#37;i", &num);
        printf("Enter the lower limit: ");
        scanf("%i", &lower);
        printf("Enter the upper limit: ");
        scanf("%i", &upper);
    
        printf("The multiples of %i in the given range are:\n", num);
        /* Insert bug fixing code here. */
        for (i = lower; i <= upper; i += num)
        {
            printf("%i ", i);
        }
        printf("\n");
    
        return 0;
    }
    Note how well formatted my example code is. I have used meaningful variable names (or at least tried to, I am not sure what to call the number that the multiples are based on. Divisor?) instead of abbreviations that may be difficult to understand at a glance.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    Well..That's too advanced for my tiny little brain..but,I will try

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	 int num, lower, upper;
        int i;
    	 printf("Enter the integer: ");
        scanf("&#37;i", &num);
    	 printf("Enter the lower limit: ");
        scanf("%i", &lower);
        printf("Enter the upper limit: ");
        scanf("%i", &upper);
    
        printf("The multiples of %i in the given range are:\n", num);
    	 /* multiples won't be all in one variable
    	 and it can't be outside the loop */
        for (i = lower; i <= upper; i += num)
    	 {
    		  printf("%i ", i);
        }
    	 printf("\n");
    
    	 return 0;
    }

    Here's the fixed one:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	 int num, lower, upper;
    	 int i;
    	 printf("Enter the integer: ");
    	 scanf("%i", &num);
    	 printf("Enter the lower limit: ");
    	 scanf("%i", &lower);
        printf("Enter the upper limit: ");
    	 scanf("%i", &upper);
    
    
    	 for (i = lower; i <= upper; i += num)
    	 {
    if (lower < num)
    		{
    	if (lower % num  == 0)
    			{
    				printf("\n%i", num);
    				lower++;
    
    			}
    		}
    		else
    		{       if ( lower == num)
    			{
    				lower = 0;
    				printf("\t\t");
    			}
    		}
    
    	 printf("\n");
    
    	 return 0;
    }
    }
    I tried...will post if i did any changes
    Thanks for your help lasernight its soooooooooo apreciated

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    My hint is what I gave in my example: insert the bug fixing code before the loop, and do not modify the loop. The trick is to find the correct value of lower such that it is the smallest multiple of num greater than or equal to the lower limit provided by the user.

    To find this new lower limit, you can either use a loop with the idea of checking if the number is a multiple, or you can use a little bit of mathematics.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    I will try again

    Thanks a million!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. rounding numbers
    By maple23 in forum C Programming
    Replies: 3
    Last Post: 05-26-2008, 12:33 AM
  3. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 04:31 AM
  4. choosing a number from a list of numbers
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-15-2001, 12:06 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM