Top 100 Networking Interview Questions click here
Basic Networking Interview Questions with answer click here
Basic Networking Interview Questions with answer click here
Please visit - https://c-programs-world.blogspot.com for new updates and replies
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int n,i,c=1,j=0,ans;
scanf("%d",&n);
int a[n][n];
if(n%2==0){ans=(pow(n,2)/2)+n/2;}
else
ans=(n-(n/2))*n;
int m=n-1,p=0,l=0,k;
while(c-1<ans)
{
for(i=l+p;i<=m&&c-1<ans;i++)
a[i][i-l]=c++;
++l;
for(j=m-l;j>=p&&c-1<ans;j--)
a[m][j]=c++;
for(k=m-1;k>l+p-1&&c-1<ans;k--)
a[k][p]=c++;
m--;p++;
}
for(i=0;i<n;i++)
{ for(j=0;j<=i;j++)
{ printf("%d ",a[i][j]);}
printf("\n");
}
return 0;
}
#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);int a[n][n],b[n*n],c=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);//printf("%d ",a[i][j]);
}
}
int top=0,bottom=n-1,left=0,right=n-1,direction=0;
while(top<=bottom&&left<=right)
{
if(direction==0)
{
for(i=left;i<=right;i++){
b[c]=a[top][i];c++;
//printf("%d ",b[c-1]);
}
top++;
}
else if(direction==1)
{
for(i=top;i<=bottom;i++)
{
b[c]=a[i][right];c++;
//printf("%d",b[c-1]);
}
right--;
}
else if(direction==2)
{
for(i=right;i>=left;i--)
{
b[c]=a[bottom][i];c++;
// printf("%d",b[c-1]);
}bottom--;
}
else if(direction==3)
{
for(i=bottom;i>=top;i--)
{
b[c]=a[i][left];c++;
// printf("%d",b[c-1]);
}left++;
}
direction=(direction+1)%4;
}
for(i=c-1;i>=0;i--)
{ printf("%d ",b[i]);
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int findmax(int a,int b)
{
if(a>b)return a;
else return b;
}
int main()
{
int n,i,j;
scanf("%d",&n);
int m=(n*2)-1;
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%d",1+findmax(abs(n-i-1),abs(n-j-1)));
}printf("\n");
}
return 0;
}
#include<stdio.h>
int main()
{
int n,sum=0,i,j,t,t1;
scanf("%d",&n);int a[n],k=0,b[n],c[n];
for(i=0;i<n;i++)
{scanf("%d",&a[i]);
sum=sum+a[i];}
sum=sum/2;
t1=a[0];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i]>a[j])
{ t=a[i];a[i]=a[j];a[j]=t;}
t=0;
for(i=0;i<n;i++)
{
if(a[i]<=sum)
{
sum=sum-a[i];
b[k++]=a[i];
}
else
c[t++]=a[i];
}
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
if(b[i]<b[j])
{sum=b[i];b[i]=b[j];b[j]=sum;}
}
}
for(i=0;i<t;i++)
{
for(j=0;j<t;j++)
{
if(c[i]<c[j])
{sum=c[i];c[i]=c[j];c[j]=sum;}
}
}
int flag=0;
for(i=0;i<k;i++)
{
if(b[i]==t1)
{flag=1;break;}
}
if(flag)
{
for(i=0;i<k;i++)
printf("%d ",b[i]);printf("\n");
for(i=0;i<t;i++)
printf("%d ",c[i]);
}
else
{
for(i=0;i<t;i++)
printf("%d ",c[i]);printf("\n");
for(i=0;i<k;i++)
printf("%d ",b[i]);
}
return 0;
}
1 | 4 | 5 | 21 |
3 | 22 | 1 | 5 |
22 | 21 | 1 | 6 |
3 | 32 | 2 | 2 |
#include<stdio.h>
#include<stdlib.h>
int max(int x, int y, int z);
int maxSum(int cost[50][50], int m, int n)
{
if (n < 0 || m < 0)
return 0;
else if (m == 0 && n == 0)
return cost[m][n];
else
return cost[m][n] +
max(maxSum(cost,m-1,n-1),maxSum(cost,m-1,n),maxSum(cost,m,n-1));
}
int max(int x, int y, int z)
{
if (x > y)
return (x > z)? x : z;
else
return (y > z)? y : z;
}
int main()
{
int n,i,j;
scanf("%d",&n);int a[50][50];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf(" %d ", maxSum(a,n,n));
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int n;
int checkgrid(int **a,int row,int col,int check1,int sum)
{ int i,j;
for(i=row;i<(row+sqrt(n));i++)
for(j=col;j<(col+sqrt(n));j++)
{
if(!(a[i][j]>=1 && a[i][j]<=n)){return 1;}
check1=check1+a[i][j];
}
if(sum!=check1)return 1;
return 0;
}
int main()
{ int sum=0,check1=0,check2=0,flag=0,i,j;
scanf("%d",&n);
if(n%2==0) sum=(pow(n,2)/2)+(n/2);
else sum=(n-(n/2))*n;
int **a=(int**)malloc(sizeof(int*)*n);
for(i=0;i<n;i++)a[i]=malloc(n*sizeof(int));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
{ check1=0;check2=0;
for(j=0;j<n;j++)
{
if(!(a[i][j]>=1&&a[i][j]<=n)){printf("no");
return 0;}
check1+=a[i][j];check2+=a[j][i];
}
if(check1!=sum||check2!=sum) {flag=1;break;}
}
if(flag!=1)
{
for(i=0;i<n;i=i+sqrt(n))
for(j=0;j<n;j=j+sqrt(n))
{
flag=checkgrid(a,i,j,0,sum);
if(flag){printf("no");return 0;}
}
}
if(flag)printf("no");
else printf("yes");
return 0;
}
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int n,i,j,t1,t2;
scanf("%d",&n);
int *a=(int *)malloc(n*sizeof(int));
int *b=(int *)calloc(n,sizeof(int));
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(sqrt(a[i])*sqrt(a[i])==a[i])
{
b[i]+=5;
}
if(a[i]%4==0&&a[i]%6==0)
{
b[i]+=4;
}
if(a[i]%2==0)
{
b[i]+=3;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(b[i]<b[j])
{
t1=a[i];t2=b[i];
a[i]=a[j];b[i]=b[j];
a[j]=t1;b[j]=t2;
}
else if(b[i]==b[j])
{
t1=a[i];a[i]=a[j];a[j]=t1;
}
}
}
for(i=n-1;i>=0;i--)
{
printf("(%d,%d)",a[i],b[i]);
}
return 0;
}
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”>
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 ;
<”wayne”, “rooney”>
<”rooney”, “ronaldo”>
<”shaw”, “rooney”>
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
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