Wednesday, 19 July 2017

Program to Find Number of Grandchildren - Zoho Programming Test Question

                           Grand Children
Here we will see a c program to find number of grandchildren, great 
grandchildren great-great grandchildren and so-on of a given person. In 
this program, the input is a table with two columns, 'Child' and 'Father'.
Then we enter a name whose number grandchildren, great grandchildren
etc is to be calculated. We do not have to count children. An example is:
Given a two dimensional array of strings like
<”luke”, “shaw”>
<”wayne”, “rooney”>
<”rooney”, “ronaldo”>
<”shaw”, “rooney”>
Where in each row, the first string is “child”, second string is “Father”. And given “ronaldo” we have to find his no of grandchildren. Here “ronaldo” has total 3 grandchildren (2 grandchildren: wayne and shaw ; 
a great grandchild luke). So our output should be 3.
write a program to find the number of grandchilden for a given name.
                                          C - Solution
#include<stdio.h>
#include<string.h>
int n;
char name[20];

struct reln{
char child[20];
char father[20];
}r[10];

int count=0;
void countChildren(char name[])
    {
    int j;
    for(j=0;j<n;j++)
        {
        if(strcmp(name,r[j].father)==0)
            {
            count++;
            countChildren(r[j].child);
            }
        }
    }

void main()
{
int i;
printf("\nEnter the number of inputs: ");
scanf("%d",&n);
for(i=0;i<n;i++)
    {
    scanf("%s",r[i].child);
    scanf("%s",r[i].father);
    }
printf("\nEnter name of the one whose no. of grandchildren 
is needed: ");
scanf("%s",name);
for(i=0;i<n;i++)
    {
    if(strcmp(r[i].father,name)==0)
        countChildren(r[i].child);
    }
printf("\nNo .of grandchildren of %s=%d",name,count);
}
                                      JAVA - Solution
import java.util.Scanner;
import java.util.ArrayList;
class Main {
public static void main(String args[]) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();scan.nextLine();
ArrayList< String > arr=new ArrayList<>();
int i,j,flag;
String[] input=new String[n];
String[] father=new String[n];
String[] child=new String[n];
for(i=0;i<n;i++)
{
input[i]=scan.nextLine();
father[i]="";
child[i]="";
flag=0;
for(j=0;j<input[i].length();j++)
{
if(input[i].charAt(j)==',')flag=1;
if(((input[i].charAt(j)>='a'&&input[i].charAt(j)<='z')||
    (input[i].charAt(j)>='A' && input[i].charAt(j)<='Z'))&&flag==0)
child[i]=child[i]+input[i].charAt(j);
else if(((input[i].charAt(j)>='a'&&input[i].charAt(j)<='z')||
    (input[i].charAt(j)>='A'&&input[i].charAt(j)<='Z'))&&flag==1)
father[i]=father[i]+input[i].charAt(j);
}
}
String search=scan.nextLine();
for(i=0;i<n;i++)
{
if(father[i].equals(search))
{
arr.add(child[i]);
}
}
flag=0;
for(i=0;i<arr.size();i++)
  {
for(j=0;j<n;j++)
{
if(father[j].equals(arr.get(i))) 
        flag++;   
}
}
System.out.println(flag);
 
 }
}
 
 Sample Input:
4
<”luke”, “shaw”>
<”wayne”, “rooney”>
<”rooney”, “ronaldo”>
<”shaw”, “rooney”>
ronaldo

Sample Output:
2

No comments:

Post a Comment

Home Page

 Zoho Interview Questions Alternate Sorting   C program for Circular Pattern C program for Spiral Matrix C program to print ...