Saturday, January 30, 2010

CHAP 2[C]f Determining youngest age

If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the youngest of the three.

void main()
{
int r,s,a,small;
printf("\nInput the ages of Ram,Shyam and Ajay");
scanf("%d%d%d",&r,&s,&a);
small=(r>s?(s>a?a:s):(r>a?a:r));
if(small==r)
printf("\nThe youngest is Ram with age %d",r);
if(small==s)
printf("\nThe youngest is Shyam with age %d",s);
if(small==a)

printf("\nThe youngest is Ajay with age %d",a);

}

7 comments:

  1. the method you used to put values in (r>s?(s>a?a:s):(r>a?a:r)
    is not in chapter 2, so could you please explain what it does .And thanks for your codes

    ReplyDelete
  2. it is there...if you have a closer look.

    ReplyDelete
  3. This is great and easy code
    #include
    #include
    main()
    {
    int ram,shyam,ajay;
    cout<<"enter the age of ram,shyam and ajay:"<>ram>>shyam>>ajay;

    if(ram>shyam && ram>ajay)
    {
    cout<<"ram is youngest:"<ram && shyam>ajay)
    {
    cout<<"shyam is youngest:"<<endl;
    }
    else
    {
    cout<<"ajay is youngest:"<<endl;
    system("pause");
    }
    }

    ReplyDelete
  4. it is there in chap 2 ,,,see the conditional operator

    ReplyDelete
  5. #include
    #include
    void main()
    {
    int ajay,ram,shyam;
    printf("enter the ages of ajay,ram,shyam");
    scanf("%d %d %d",&ajay,&ram,&shyam);
    if(((ajay>ram)&&(shyam>ajay))||((ajay>ram)&&(shyam>ram)))
    {
    printf("\n the youngest of the three is ram");
    }
    if(((ram>ajay)&&(shyam>ajay))||((ram>ajay)&&(shyam>ram)))
    {
    printf("\n the youngest of the three is ajay");
    }
    if(((ram>shyam)&&(ajay>shyam))||((ram>ajay)&&(shyam<ajay)))
    {
    printf("\n the youngest of the three is shyam");
    }
    }

    ReplyDelete
    Replies
    1. what if there are the same age, the result would be different

      Delete
    2. this is the solution if there is the same age =), I hope so.

      #include

      main()
      {
      int r,s,a,small;

      printf("\nInput the ages of Ram,Shyam and Ajay");
      scanf("%d,%d,%d",&r,&s,&a);

      if((r<s)&&(r<a))
      {
      printf("Ram is the youngest");
      }
      else if((s<r)&&(s<a))
      {
      printf("shyam is the youngest");
      }
      else if((a<s)&&(a<r))
      {
      printf("Ajay is the youngest");
      }

      else if(r==a && a<s)
      {
      printf("Ram and Ajay are the youngest");

      }

      else if(a==s && a<r)
      {
      printf("Shyam and Ajay are the youngest");
      }

      else if(r==s && s<a)
      {
      printf("Shyam and Ram are the youngest");
      }
      else
      {
      printf("All are the same age");
      }
      return 0;

      }

      Delete