Program to Find Number of Grandchildren:
Sample Input:
4
<”luke”, “shaw”>
<”wayne”, “rooney”>
<”rooney”, “ronaldo”>
<”shaw”, “rooney”>
ronaldo
Sample Output:
2
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);
}
Sample Input:
4
<”luke”, “shaw”>
<”wayne”, “rooney”>
<”rooney”, “ronaldo”>
<”shaw”, “rooney”>
ronaldo
Sample Output:
2
Zoho c programming question-Program to Find Number of Grandchildren
Reviewed by astin salvi
on
10:54 AM
Rating:
No comments: