Print Grandson's NameThe program must accept first and last names of three persons who are in a family tree and print the grandson's name.
Input Format:
The first line contains the first and last name of person 1 separated by a space.
The second line contains the first and last name of person 2 separated by a space.
The third line contains the first and last name of person 3 separated by a space.
Output Format:
The first line contains the first and last name of the grandson separated by a space.
Boundary Conditions:
Length of first and last names are from 3 to 100
Note:
The last name of a person is nothing but the father's first name.
Example Input/Output 1:
Input:
Arun Kumar
Swamy Nathan
Kumar Swamy
Output:
Arun Kumar
Code:
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i,j;
String[] son=new String[3];
String[] father=new String[3];
for(i=0;i<3;i++)
{
son[i]=sc.next();
father[i]=sc.next();
}
int flag=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(!son[i].equals(father[j]))
{
flag++;
}
}
if(flag==3)
break;
else
flag=0;
}
System.out.println(son[i]+" "+father[i]);
}
}
No comments:
Post a Comment