Thread: Help with if statement program plz

  1. #1
    Registered User
    Join Date
    Nov 2009

    Help with if statement program plz

    I'm having difficulty writing a c program that asks the user to input 4 integers and it find the smallest and largest integer. I can only use 4 "if" statements to complete the program. I know the program does not compute the smallest integer correctly, but how can i fix it ? This is what I have so far..

    #include <stdio.h>
    int main()
      int num1, num2, num3, num4;
      int smallest, largest;
      printf("Enter four integers: ");
      scanf("%d %d %d %d", &num1, &num2, &num3, &num4);
     smallest = num1;
      if (num1 >= num2)
        if (num1 >= num3)
          if (num1 >= num4)
             largest = num1;
             largest = num4;
        else if (num4 >= num3)
          largest = num4;
          largest = num3;
        largest = num2;
    printf("Largest: %d\n", largest);
    printf("Smallest: %d\n", smallest);
    return 0;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Have you encountered arrays yet? Do you really need to keep track of each number, or just the big and small? Have you used loops yet?
    smallest = big number
    biggest = small number
        read a number
        if number < smallest
            smallest = number
        if number > biggest
            biggest = number
    Set your repeat condition depending on how many times you want to repeat, or until you enter something that means 'quit looping'.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Ya, just set your biggest number to 0, loop through all numbers saying if this number is bigger, set it to your current number. Than you only need one if statement (two if you do the smallest number as well).

    num = 0;
       if ( num[i] > num )
          num =  num[i];
    And essentially do the opposite for the smallest number...

    This will set num to the biggest number, because the only time it's overwritten is if a number is larger and vice versa. you will have to switch your implementation to an array.

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    We prefer not to give out solutions, especially in code, especially since Quzah just posted the pseudo code for that snippet you posted...
    Leave some practice for the OP.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice program, plz help
    By Johnta in forum C Programming
    Replies: 12
    Last Post: 02-22-2008, 01:53 PM
  2. Program not seeing the if statement
    By adjac in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2004, 08:04 PM
  3. help with program using switch statement
    By Aysha in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2002, 04:43 PM
  4. Can someone help me plz
    By Shadow in forum C Programming
    Replies: 5
    Last Post: 02-15-2002, 01:41 PM
  5. plz hlp me. program not running properly
    By jfl in forum C Programming
    Replies: 5
    Last Post: 02-11-2002, 03:58 PM