counting by fives

This is a discussion on counting by fives within the C Programming forums, part of the General Programming Boards category; how do i make my program count by fives or tens Code: #include <stdio.h> main() { int x; for ( ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    6

    Question counting by fives

    how do i make my program count by fives or tens
    Code:
    #include <stdio.h>
    
    main()
    
    {
    
    int x;
    
    for ( x = 100; x > 1; x-- )
    	printf("\n%d\n", x);
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,301
    Make the change of x in the loop a multiple of five (or ten). Alternately, print x*5 (or x*10).
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    Your x changes by a factor of five, duh.

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Replace x-- with x = x+5. And for tens, use x = x+10.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Quote Originally Posted by Babkockdood
    Replace x-- with x = x+5. And for tens, use x = x+10.
    This suggestion echos the spirit of the earlier posts, but note that if you actually implement it, you will end up with an infinite loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    Just decrement by 5.

  7. #7
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    How about this?
    Code:
    for ( x = 100; x > 1; x--, x--, x--, x--, x--)
    	printf("\n%d\n", x);
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    Code:
    X-=5

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Oh for God's sakes I think the OP got it already.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reference Counting
    By Dae in forum C++ Programming
    Replies: 10
    Last Post: 08-13-2009, 07:34 AM
  2. How to implement reference counting with 'Smart_ptr'?
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 06-10-2007, 05:28 AM
  3. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  4. counting and output, with arrays and functions
    By Wraithan in forum C++ Programming
    Replies: 7
    Last Post: 12-04-2005, 11:46 PM
  5. Counting using Lib Cstring
    By niroopan in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2002, 04:51 PM

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