mardi 29 août 2017

Sort the age of 3 individuals

/* to find the age of individuals according to youngest to oldest */

#include <stdio.h>

int main(void)
{
  int age1, age2, age3, youngest, middle, oldest;
  {
    printf ("Enter the age of the first individual: ");
    scanf  ("%d", &age1);
    printf ("Enter the age of the second individual: ");
    scanf  ("%d", &age2);
    printf ("Enter the age of the third individual: ");
    scanf  ("%d", &age3);
  }
  if (age1==age2==age3);
  {
    printf("All individuals have the same age of %d", &age1);
  }
  else
  {
    youngest = age1;

    if (age1 > age2)
      youngest = age2;
    if (age2 > age3)
      youngest = age3;

    middle = age1;

    if (age1 > age2)
      middle = age2;
    if (age2 < age3)
      middle = age2;

    oldest = age1;

    if (age1 < age2)
      oldest = age2;
    if (age2 < age3)
      oldest = age3;

    printf("%d is the youngest.\n", youngest);
    printf("%d is the middle.\n", middle);
    printf("%d is the oldest.\n", oldest);
  }
  return 0;
}

I keep getting the error on line 21 which states that I have an 'else' with a previous 'if'. Any of the experts here can tell me where I went wrong? The display is also a little weird if I were to remove the 'else'.

Aucun commentaire:

Enregistrer un commentaire