Thread: For loop doesn't work for positive integers -- while loop works fine.

  1. #1
    Registered User
    Join Date
    Mar 2015
    Location
    Massachusetts
    Posts
    9

    For loop doesn't work for positive integers -- while loop works fine.

    Code:
    #include<stdio.h>
    Code:
    #include<stdlib.h>
    
    
    int sumFor(int daNumba);
    int sumWhile(int daNumba);
    
    
            /*      Main function           */
    
    
    int main()
    {
            /*      Declare variables       */
    
    
       int daNumba, startNumba, ansFor, ansWhile;
    
    
            /*      Display welcome message         */
    
    
       printf("\nWelcome to sumTimes2.\n");
    
    
            /*      Message indicating what the program will do     */
    
    
       printf("\nThis program will sum the integers between the one you enter and\n");
       printf("the integer that is twice that value.\n");
    
    
            /*      Ask the user to enter and receive an integer      */
    
    
       printf("\nPlease enter your integer: ", daNumba);
       scanf("%d", &daNumba);
    
    
            /*      sumFor program      */
    
    
       int sumFor(int daNumba)
       {
         int ansFor=0, startNumba=daNumba;
         for( ; startNumba <= (daNumba*2); )
         {
            ansFor += startNumba++;
         }
         for( ; startNumba >= (daNumba*2); )
         {
            ansFor += startNumba--;
         }
            return ansFor;
       }
    
    
            /*      sumWhile program        */
    
    
       int sumWhile(int daNumba)
       {
         int ansWhile=0, startNumba=daNumba;
         while(daNumba >0 && startNumba <= (daNumba*2))
         {
            ansWhile += startNumba++;
         }
         while(daNumba <= 0 && startNumba >= (daNumba*2))
         {
            ansWhile += startNumba--;
         }
            return ansWhile;
       }
    
    
            /*      Display test statistics         */
       printf("\nFor says the sum is %d\n", sumFor(daNumba));
       printf("While says the sum is %d\n", sumWhile(daNumba));
    
    
            /*      Display ending thank you message        */
    
    
       printf("\nThank you for using sumTimes2. Bye!\n\n");
    
    
            /*      Rieturn 0 for the compiler       */
    
    
       return 0;
    
    
    }
    

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    In sumFor, BOTH of the for loops run no matter what the input is, so you need some code logic which ensures only one runs - which one dependant on the sign of the input integer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-01-2012, 08:00 AM
  2. Replies: 2
    Last Post: 09-25-2012, 10:54 PM
  3. Loop doesn't work
    By renvaar in forum C Programming
    Replies: 15
    Last Post: 01-15-2010, 09:36 AM
  4. do-while loop doesn't work
    By agentsmith in forum C Programming
    Replies: 17
    Last Post: 01-01-2008, 12:10 PM
  5. why does this loop work, but the other doesn't?
    By Shadow12345 in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2002, 07:07 PM