Thread: Counting backwards:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Counting backwards:

    So I been browsing the internet and doing random practice exercises before I start my C++ class next week.

    This question wanted me to count 1-99 backwards and print "found one" when the number was divisible by 5. This is what I got so far.

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, const char * argv[])
    {
    
        
        int i = 0;
        
        for (i = 0; i<100; ++i) {
            
            printf("%d\n",i);
            
            if ((i%5)== 0) {
                printf("found one\n"); //every number this is divisible by 5 write found one 
            }
            
            i++;
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Code:
    for (i = 99; i > 0; --i)

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Quote Originally Posted by jocdrew21 View Post
    This question wanted me to count 1-99 backwards and print "found one" when the number was divisible by 5. This is what I got so far.
    Code:
    #include <stdio.h>
    
    
    int main(int argc, const char * argv[])
    {
     
         
        int i;
         
        for (i = 99; i > 0; i--)//This will count from 99 decrementally till it reaches 1 
         {
            printf("%d\n",i);
             
            if ((i%5)== 0) 
            {
                printf("found one\n"); //every number this is divisible by 5 write found one 
            }
        }
        return 0;
    }
    Good luck with your C++ programing which school btw

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I tried that but it looped forever with negitive numbers. I'll try again, most likely I did something wrong...

    Thomas Edison State in New Jersey...

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    It worked... I wonder what the heck I did it make it produce huge numbers like -16000. I'll have to look at my compiler at home. Thank you for the help.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Also try implementing with do while loop for practice. Why are you practicing C if your starting C++?

    answer for above:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int number = 99;
    
    	do
    	{
    		printf("%d\n", number);
    		if (number %5 == 0)
    		printf("found one\n");
    		number--;
    	}while (number > 0);
    
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I bought "Jumping into C++" from this website so I have been reading that. I also have been reading Objective-C by Big Nerd Ranch which is where I found this little program. Big Nerd Ranch briefly goes over C and actually taught me some small details I missed. I have been writing C++ programs from "Jumping into C++", and C++ seems to be very similar to C just has different syntax’s and larger library. Something’s are much easier to work with too, like strings and the ability to write into a for statement.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by jocdrew21 View Post
    I bought "Jumping into C++" from this website so I have been reading that. I also have been reading Objective-C by Big Nerd Ranch which is where I found this little program. Big Nerd Ranch briefly goes over C and actually taught me some small details I missed. I have been writing C++ programs from "Jumping into C++", and C++ seems to be very similar to C just has different syntax’s and larger library. Something’s are much easier to work with too, like strings and the ability to write into a for statement.
    Argh! No, this is the wrong approach. That's like studying a Spanish vocabulary book and an Italian grammar book, then pronouncing it all with a French accent, and thinking you then know any of those languages. You'll end up not really knowing any of them. They are three totally separate languages, despite the misleadingly similar names. Pick one and only one programming language to start learning. There is much more that is different between C and C++ besides just "syntax's and a larger library". C++ takes a fundamentally different approach to problem solving (object-oriented), and while a C++ compiler will usually accept C programs, it doesn't mean you're writing good C or C++. Learning to program is more than just learning a bit of syntax, to really learn to program is to learn to think and to solve problems in a particular way, using the features and methodologies supported by a language. When you transition languages, the first step is just to learn the new syntax, library functions, etc. But that's the easy part, the hard part is learning to think about problem solving in a whole new way. Wait until you have a pretty thorough knowledge of one language before starting to learn another, especially since you're new to programming in general. If you're starting a C++ class, then stick strictly to C++.

  9. #9
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Ok.... Your right, I'll stick with C++. After the C++ class is over I have a whole 3 month class on Data Structures which is entirely in C++.

    Won't knowing C help with C++?

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by jocdrew21 View Post
    Won't knowing C help with C++?
    Only very marginally. Once you get past the similar names and syntax, the similarities fall off rapidly, especially when it comes to the more important aspects like how you solve problems. If you had lots of time to become a C expert (and a general programming/problem solving expert) before you learn C++, then it might be worth it. But you don't, and your subsequent classes are C++ too, so just stick to C++ for now. Trying to learn too much at once will give you a superficial (at best) knowledge of these languages, and you wont be able to do anything very interesting in any of them.

  11. #11
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Will do, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can anyone tell me why this outputs backwards
    By claudiat13 in forum C++ Programming
    Replies: 2
    Last Post: 05-06-2010, 05:16 AM
  2. Backwards uint16_t?
    By DavidDobson in forum C Programming
    Replies: 7
    Last Post: 05-11-2009, 09:45 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. Backwards
    By cyberCLoWn in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2004, 08:11 AM
  5. Backwards Up-Down
    By Cactus_Hugger in forum Windows Programming
    Replies: 1
    Last Post: 11-08-2003, 05:56 PM