"Inclusive" problem with my program.

This is a discussion on "Inclusive" problem with my program. within the C Programming forums, part of the General Programming Boards category; Hey guys, I wrote a program for our assignment, and I'm having only one problem with it. The program is ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    27

    "Inclusive" problem with my program.

    Hey guys, I wrote a program for our assignment, and I'm having only one problem with it. The program is supposed to ask for an integer, find integers divisible by 5 between 1 and the integer you gave, list them, and give the count. My program does this, but for some reason it wont include the number you input.
    Like for example, I input the number 20, and the output gives:

    Enter a positive integer: 20
    15
    10
    5
    0
    the count is 4
    It wont include the number given, and its not supposed to count '0'. Here's my program:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int n;
    	int count = 0;
    	
    	
    	printf("Enter a positive integer: ");
    	scanf("%d", &n);
    {
    for(n>1; n-- ;)
    if(n%5 == 0){
    	printf("%d\n", n);
    	count++;}
    	printf("The count is %d\n", count);
    			
    
    }
    
    
    }
    What am I doing wrong?

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    add another condition with n>=5.
    So this will not include 0 now.
    the corrected code is given below

    Code:
    int main(void)
    {
            int n;
            int count = 0;
    
    
            printf("Enter a positive integer: ");
            scanf("%d", &n);
    {
    for(n>1; n-- ;)
    if(n%5 == 0 && n>= 5 ){
            printf("%d\n", n);
            count++;}
            printf("The count is %d\n", count);
    
    
    }
    
    
    }
    Enter a positive integer: 20
    15
    10
    5
    The count is 3

    Now the output will be as shown above
    Last edited by thillai_selvan; 02-25-2010 at 11:55 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you indented your code, you'd see some of the problems:


    Code:
    {
    for(i = 1, count = 0; i <=n ; i++) {
       if((i%5 == 0) && (i)) {
          count++;
          printf("\n Count is: %d Number Found: %d", count, i);
       }
    }
    Try that.

    Edit: Whoops! See program, below
    Last edited by Adak; 02-26-2010 at 12:26 AM.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: "Inclusive" problem with my program.

    I think you are doing wrong in that for loop. I changed that.
    And If you want to include the 0 also you can use the condition as n>=0 in the for loop.
    Try this code

    Code:
    #include <stdio.h>
    
    int main(void)
    {
            int n;
            int count = 0;
    
    
            printf("Enter a positive integer: ");
            scanf("%d", &n);
            {
                    for(;n>1; n-- )
                            if(n%5 == 0){
                                    printf("%d\n", n);
                                    count++;
                            }
                    printf("The count is %d\n", count);
            }
    }

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    Thought it is a better solution
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            int n;
            int count = 0;
    
    
            printf("Enter a positive integer: ");
            scanf("%d", &n);
    {
    for(n>1; --n ;)
    if(n%5 == 0){
            printf("%d\n", n);
            count++;}
            printf("The count is %d\n", count);
    
    
    }
    
    
    }

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    Ok, I see where I didnt add in the extra condition in my IF statement to get rid of the 0's. But I'm having problems with the program including 20 with the solutions.
    @Adak, I tried that and it didnt seem to work.
    @thillai_selvan, thanks that helped a lot.

    I'm wondering if my program is going on about this backwards. Would it include 20 if it worked its way up from 1 to the inputted number? I think my program is working down to 1, so its not going to include 20.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    Welcome jaja... According to your query I made a little changes. Hope this will solve your requirement completely.

    Code:
    int main(void)
    {
            int n;
            int count = 0;
    
    
            printf("Enter a positive integer: ");
            scanf("%d", &n);
    {
    for(;n>1; n--)
    if(n%5 == 0 && n>= 5 ){
            printf("%d\n", n);
            count++;}
            printf("The count is %d\n", count);
    
    
    }
    
    }
    Try this code. Now this will include your input number also

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    
    int main(void) {
      int i, n, count;
    
     
      printf("\n\n Enter a number: ");
      scanf("%d", &n);
      getchar();
    
      for(i = 1, count = 0; i <=n ; i++) {
        if((i%5 == 0) && (i)) {
          count++;
          printf("\n Count is: %d Number Found: %d", count, i);
        }
      }
    
      printf("\n\n\t\t\t     press enter when ready");
      getchar();
      return 0;
    }

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    Hey thanks a lot guys! It works great! I see where I made my mistakes. I was missing a simple ";"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Replies: 20
    Last Post: 06-13-2005, 12:53 AM
  4. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 01:35 AM

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