Thread: Finding the nearest even integer that's greater than the inputted number.

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    6

    Finding the nearest even integer that's greater than the inputted number.

    For the input of: 4.1, the output is supposed to be: 6. Please help, it seems that the output of the program that i wrote is just random.
    Edit:
    Code:
     
    #include <stdio.h>
    
    
    int main(void) {
      while (1) {
        printf("Please enter a number:\n");
        double number0;
           scanf("%lf",&number0);
        int number,check1,check2,result1,result2;
        number = (int)number0;
        result1 = number++;
        result2 = number+2;
        check1 = result1%2;
        check2 = result2%2;
        if (check1==0)
            printf("The nearest even integer that's greater than the entered number = %i. \n",result1);
            else
            printf ("The nearest even integer that's greater than the entered number = %i. \n", result2); 
      }
      return 0; }
    Attached Images Attached Images Finding the nearest even integer that's greater than the inputted number.-cantsolve-jpg 
    Last edited by Null_Void; 04-07-2020 at 07:29 AM.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    717
    Quote Originally Posted by Null_Void View Post
    For the input of: 4.1, the output is supposed to be: 6. Please help, it seems that the output of the program that i wrote is just random.
    Please post the actual code in [CODE] tags as instructed in the forum FAQ, NOT in fuzzy images!

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    6
    Quote Originally Posted by rstanley View Post
    Please post the actual code in [CODE] tags as instructed in the forum FAQ, NOT in fuzzy images!
    Sorry, is it okay now?

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    717
    On line 11 you are post incrementing number, then on the next line you are actually adding 3 to result2.
    The result is not random, just not correct.

    Could be done much simpler
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       // Initialize all local variables to 0 or some other value
       double number0 = 0.0;
       int number = 0;
    
       while (1) {
          printf("Please enter a number:\n");
    
          // scanf() needs error checking!
          // Check the return value from scanf() and handle an error
          scanf("%lf",&number0);
    
          number = (int)number0;
          if(number % 2 == 0) // 0 if even, 1 otherwise
          {
             number += 2;
          }
          else
          {
             number++;
          }
    
          printf("The nearest even integer that's greater than the entered number = %d. \n", number);
       }
       return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    6
    Thank you!

  6. #6
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    299
    Also, try and not develop the habit to write code for non-terminating programs. If you use something like while(1), make sure to include a terminating condition inside the loop at least, although, it is always better to have the loop condition in the for/while statements itself.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-11-2017, 01:26 AM
  2. Rounding float to nearest integer
    By xArt in forum C Programming
    Replies: 2
    Last Post: 01-31-2017, 07:05 AM
  3. Issue finding greater fraction
    By hss1194 in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2012, 06:28 AM
  4. Replies: 4
    Last Post: 02-11-2011, 03:29 PM
  5. finding nearest neighbour distance
    By zesty in forum C Programming
    Replies: 21
    Last Post: 12-02-2009, 05:20 AM

Tags for this Thread