Thread: Infinite Numbers C Program

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    1

    Infinite Numbers C Program

    Hello,

    Very new to programming and I am having trouble with an assignment
    I need to be able to have the user enter an infinite amount of numbers and when finished, display the highest and lowest number, number of . even numbers and average of odd numbers entered. Thank you!

    Code:
     :
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    
    
    int main() {
        
        
        int userNum=0, count=0, highNum=0, lowNum=0, evenNum=0, oddNum=0;
        
        
    printf("Enter a number greater than 0: (Enter -1 to stop) \n");
    scanf("%i", &userNum);
        
        while (userNum != -1){
            if (userNum > lowNum)
    highNum = userNum;
             if (userNum > highNum)
    lowNum = userNum;
            if (userNum%2 == 0)
    evenNum = userNum;
            if (userNum%2 != 0)
    oddNum = userNum;
    count = count + 1;
    printf("Enter a number:(-1 to stop)\n");
    scanf("%i", &userNum);
    }
        
        if (userNum%2 == 0)
    evenNum = userNum;
            if (userNum%2 != 0)
    oddNum = userNum;
        
            
    printf("The highest number entered is: %i \n", highNum);
    printf("The lowest number entered is: %i \n", lowNum);
    printf("The number of even numbers entered is: %i \n", evenNum);
    printf("The average of odd numbers entered is: %i \n", oddNum);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,162
    You mixed up the highNum and lowNum assignments.

    You're supposed to count the even numbers, not track the most recent even number.

    You need to remember to use braces when needed. If not, always use braces.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Producing Infinite Garbage
    By hotshotennis in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2013, 06:55 AM
  2. Why does the loop in my program turn infinite?
    By DaniiChris in forum C Programming
    Replies: 6
    Last Post: 07-08-2008, 02:44 AM
  3. My program's in an infinite loop!
    By Peach in forum C Programming
    Replies: 3
    Last Post: 02-24-2008, 01:58 PM
  4. Infinite Looping Program Prob's
    By dld333 in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 12:44 AM
  5. detect infinite loop in c program
    By abhivyakat in forum C Programming
    Replies: 19
    Last Post: 10-01-2003, 06:55 AM

Tags for this Thread