Click here to Skip to main content
11,495,743 members (60,512 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
hi im very new to c and i decided to write this program, can anyone tell me why its not counting down from the number entered to 0?
#include <stdio.h>

int printloop(int k )
{
    int b;
        for (b = k; b < 0; b--)
     printf("%d\n",b);
}
 
int main()
{
int c;
 
scanf("%d",&c);
if( c > '0' && c < '6')
 
printloop(c);
 
return 0;
}
Posted 25-Sep-12 9:14am
Edited 25-Sep-12 9:19am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

To start with:
if( c > '0' && c < '6')
c is an integer defined but you compare it with a character '0' & '6'. Remove quotes around 0 & 6
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 25-Sep-12 14:26pm
   
One bug nailed down, a 5.
--SA
Sandeep Mewara at 25-Sep-12 14:29pm
   
Thanks SA.
Marcus Kramer at 25-Sep-12 14:32pm
   
+5 for the "other" issue.
Sandeep Mewara at 25-Sep-12 14:39pm
   
:)
ridoy at 25-Sep-12 15:29pm
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Because in your for (b = k; b < 0; b--), you aren't allowing it to do any work.
The second clause "b<0" indicates that this loop will only run if b < 0.
Since you set b == k which is greater than 0, the loop will not run.
Your solution is for (b = k; b >= 0; b--)
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 25-Sep-12 14:26pm
   
Another bug nailed down, a 5.
--SA
Marcus Kramer at 25-Sep-12 14:32pm
   
That error was simply too common a mistake that rookies make. I've learned a long time ago to always check the for loop args before anything else when someone says the loop isn't doing anything. Thanks.
Sergey Alexandrovich Kryukov at 25-Sep-12 17:41pm
   
Sure...
--SA
Sandeep Mewara at 25-Sep-12 14:29pm
   
5! Marcus!

:)
Marcus Kramer at 25-Sep-12 14:32pm
   
Thanks Sandeep.
flaringdon at 25-Sep-12 14:34pm
   
thanks marcus and sandeep
ridoy at 25-Sep-12 15:29pm
   
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 692
1 CHill60 318
2 Sascha Lefèvre 275
3 RyanDev 230
4 Sergey Alexandrovich Kryukov 210
0 Sergey Alexandrovich Kryukov 10,362
1 OriginalGriff 8,821
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100