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);
}
Subscribe to:
Post Comments (Atom)
the method you used to put values in (r>s?(s>a?a:s):(r>a?a:r)
ReplyDeleteis not in chapter 2, so could you please explain what it does .And thanks for your codes
it is there...if you have a closer look.
ReplyDeleteThis is great and easy code
ReplyDelete#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");
}
}
it is there in chap 2 ,,,see the conditional operator
ReplyDelete#include
ReplyDelete#include
void main()
{
int ajay,ram,shyam;
printf("enter the ages of ajay,ram,shyam");
scanf("%d %d %d",&ajay,&ram,­am);
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");
}
}
what if there are the same age, the result would be different
Deletethis is the solution if there is the same age =), I hope so.
Delete#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;
}