"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:
Quote:
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?
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);
}
}