Crack the Code: IT Interview Questions and Answers - Programming Language Institute

Programming practice questions and answer

C language Array Programs Questions/Answer

 
1

Question- Write a C program to insert 5 elements in an array and print its value.



Answer-

Test Data :

Input 5 elements in the array :

element - 0 : 12

element - 1 : 14

element - 2 : 22

element - 3 : 4

element - 4: 9


Expected Output :

Your Array Element is: 12, 14, 22, 4, 9



#include<stdio.h>

#include<conio.h>

void main()

{

int A[5];

int a;

printf(“Enter Elements of array\n”);

for(a=0; a<5;a++)

{

printf(“\nEnter %d Element of Array: ”,a);

scanf(“%d”,&A[a]);

}

 printf(“Your Array Element is :”);

for(a=0; a<5;a++)

{

printf(“%d ,”,A[a]);

}

getch();

}

2

Question- Write a C program to insert 5 elements in an array and print it value in reverse order.



Answer-

Test Data :

Input 5 elements in the array :

element - 0 : 12

element - 1 : 14

element - 2 : 22

element - 3 : 4

element - 4: 9


Expected Output :

Your Array Element is: 9, 4, 22, 14, 12


#include<stdio.h>

#include<conio.h>

void main()

{

int A[5];

int a;

printf(“Enter Elements of array\n”);

for(a=0; a<5;a++)

{

printf(“\nEnter %d Element of Array: ”,a);

scanf(“%d”,&A[a]);

}

 printf(“Your Array Element is :”);

for(a=4; a>=0;a--)

{

printf(“%d , ”,A[a]);

}

getch();

}

3

Question- Write a C program to insert 5 elements in an array and print sum of all items.



Answer-

Test Data :

Input 5 elements in the array :

element - 0 : 12

element - 1 : 14

element - 2 : 22

element - 3 : 4

element - 4: 9


Expected Output :

Sum of Array Element is: 61


#include<stdio.h>

#include<conio.h>

void main()

{

int A[5];

int a,s=0;

printf(“Enter Elements of array\n”);

for(a=0; a<5;a++)

{

printf(“\nEnter %d Element of Array: ”,a);

scanf(“%d”,&A[a]);

}

 printf(“Sum of Array Element is :”);

for(a=0; a<5;a++)

{

s=s+A[a];


}

printf(“%d ”,s);

getch();

}

4

Question- Write a C program to copy the elements of one array into another array.



Answer-

#include <stdio.h>

void main()

{    

int arr1[100], arr2[100];    

int i, n;    

printf("\n Copy the items of one array into another array :\n");    

printf("----------------------------------------------------\n");       

printf("Input the number of items to be stored in the array :");    

scanf("%d",&n);    

printf("Input %d items in the array :\n",n);      

 for(i=0;i<n;i++)       

 {     

 printf("enter item - %d : ",i);      

scanf("%d",&arr1[i]);    

}    

/* Copy the elements of first array into second array.*/     

for(i=0; i<n; i++)   

 {       

 arr2[i] = arr1[i];    

}    

/* Prints the items of first array   */  

  printf("\nThe items stored in the first array are :\n");   

 for(i=0; i<n; i++)   

 {       

 printf("%d", arr1[i]);   

 }   

 /* Prints the items copied into the second array. */    

printf("\n\nThe items copied into the second array are :\n");   

 for(i=0; i<n; i++)   

 {        

printf("% d", arr2[i]);  

  }       

printf("\n\n");

}

5

Question- Write a C program to count a total number of duplicate elements in an array.



Answer-

#include <stdio.h>


void main()

{

    int arr1[100];

int arr2[100];

int arr3[100];

    int n,mm=1,ctr=0;

    int i, j;

       printf("\n\nCount total number of duplicate elements in an array:\n");

       printf("---------------------------------------------------------\n");


       printf("Input the number of elements to be stored in the array :");

       scanf("%d",&n);

   

       printf("Input %d elements in the array :\n",n);

       for(i=0;i<n;i++)

        {

      printf("element - %d : ",i);

      scanf("%d",&arr1[i]);

    }

/*----------------- copy in other array ------------------------------------*/

for(i=0;i<n; i++)

        {

arr2[i]=arr1[i];

arr3[i]=0;

        }

/*------------------- mark the elements are duplicate -------------------------*/

for(i=0;i<n; i++)

        {

for(j=0;j<n;j++)

{

if(arr1[i]==arr2[j])

{

arr3[j]=mm;

mm++;

}

}

mm=1;

        }

/*--------------- Prints the array ------------------------------------*/

   for(i=0; i<n; i++)

    {

      if(arr3[i]==2){ctr++;}  

    }

      printf("The total number of duplicate elements found in the array is: %d \n", ctr);

    

  printf("\n\n");

}