I just started learning C, and had this issue brought up when I was trying out code for a problem from a textbook.
Basically the code is supposed to tell the youngest person given the ages of three different people, as practice in using if-else statements.
/* To determine the youngest, if given the ages of three people */
main()
{
int ar, as, aa; //ar - Ram's age etc.
/* To get the input ages */
printf("Please enter Ram's age \n");
scanf("&d",&ar);
printf("Please enter Shyam's age \n");
scanf("&d",&as);
printf("Please enter Ajay's age \n");
scanf("&d",&aa);
/* To print the result */
if ((ar<as)&&(ar<aa))
printf("Ram is the youngest");
else if ((as<ar)&&(as<aa))
printf("Shyam is the youngest");
else if ((aa<as)&&(aa<ar))
printf("Ajay is the youngest");
else if ((ar==as)&&(ar<aa))
printf("Ram and Shyam are the youngest");
else if ((ar==aa)&&(ar<as))
printf("Ram and Ajay are the youngest");
else if ((as==aa)&&(as<ar))
printf("Shyam and Ajay are the youngest");
else if ((ar==aa)&&(ar==as))
printf("All three of them are the same age");
}
In case my formatting is still incorrect: https://repl.it/C1YL/3
However, when I input the ages, I don't get the right answer. More often than not, it always says the second person is the youngest. I can't really see what I'm doing which in fundamentally wrong. Any way to help?
Aucun commentaire:
Enregistrer un commentaire