Arrays – Theory and Programs [Learn to code]

What are Arrays? Learn syntax for Arrays and different programs on Arrays.

  • 066. Write a program to accept a number n from user and then accept n array elements from user and reprint them in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
     int n,i,a[20];
     clrscr();
     printf("Enter number...");
     scanf("%d",&n);
     printf("Enter array elements :n" );
     for(i=0;i<n;i++)
    	{
    		printf("Enter element %d : ",i+1);
    		scanf("%d",&a[i]);
    	}
     for(i=0;i<n;i++)
    		printf("nElement %d : %d",i+1,a[i]);
     getch();
    }
  • 067. Write a program to accept a number n from user and then accept n array elements from user and reprint them in reverse order of inputs in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
     int n,i,a[20];
     clrscr();
     printf("Enter number...");
     scanf("%d",&n);
     printf("Enter array elements :n" );
     for(i=0;i<n;i++)
    	{
    		printf("Enter element %d : ",i+1);
    		scanf("%d",&a[i]);
    	}
     for(i=n-1;i>=0;i--)
    		printf("nElement %d : %d",i+1,a[i]);
     getch();
    }
  • 068. Write a program to accept a number n from user and then accept n array elements from user and print addition of those n array elements in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
     int n,i,a[20],sum=0;
     clrscr();
     printf("Enter number...");
     scanf("%d",&n);
     printf("Enter array elements :n" );
     for(i=0;i<n;i++)
    	{
    		printf("Enter element %d : ",i+1);
    		scanf("%d",&a[i]);
    	}
     
     for(i=0;i<n;i++)
    	sum=sum+a[i];
     printf("nSum of given array elements is : %d",sum);
     getch();
    }
  • 069. Write a program to accept a number n from user and print fibbonacci series up to nth level using arrays in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int a[20],n,i;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	a[1]=a[2]=1;
     
    	for(i=3;i<=n;i++)
    		a[i]=a[i-1]+a[i-2];
     
    	for(i=1;i<=n;i++)
    		printf(" %d",a[i]);
     
    	getch();
    }
  • 070. Write a program to accept a number n from user and then accept n array elements from user and print maximum and minimum array element from that set of array in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int n,i,a[20],max,min;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	printf("Enter array elements...n");
    	for(i=0;i<n;i++)
    	{
    		printf("Enter element %d : ",i+1);
    		scanf("%d",&a[i]);
    	}
    	max=min=a[0];
    	for(i=0;i<n;i++)
    	{
    		if(a[i]>max)
    			max=a[i];
    if(a[i]<min)
    			min=a[i];
    	}
    	printf("nMin : %d",min);
    	printf("nMax : %d",max);
    	getch();
    }
  • 071. Write a program to accept a number n from user and then accept n array elements from user, print positive & Negative numbers separately in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int n,i,a[20];
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	printf("Enter array elements...n");
    	for(i=0;i<n;i++)
    	{
    		printf("Enter element %d : ",i+1);
    		scanf("%d",&a[i]);
    	}
     
    	printf("nnNegative Elements : n");
    	for(i=0;i<n;i++)
    		if(a[i]<0)
    			printf("n%d",a[i]);
     
    	printf("nnPositive Elements : n");
    	for(i=0;i<n;i++)
    		if(a[i]>0)
    			printf("n%d",a[i]);
     
    	getch();
    }
  • 072. Write a program to accept a number n from user and then accept n array elements from user, print these array elements in ascending and descending order in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int n,i,a[20],j,max,min,temp;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	printf("Enter array elements...n");
    	for(i=0;i<n;i++)
    	{
    	printf("Enter element %d : ",i+1);
    		scanf("%d",&a[i]);
    	}
    	max=a[0];
    	for(i=0;i<n;i++)
    	{
    		for(j=0;j<i;j++)
    			if(a[i]>a[j])
    			{
    				temp=a[i];
    				a[i]=a[j];
    				a[j]=temp;
    			}
    	}
    	printf("Ascending order.....n");
    	for(i=0;i<n;i++)
    	{
    		printf(" %d  ",a[i]);
    	}
     
    	min=a[0];
    	for(i=0;i<n;i++)
    	{
    		for(j=0;j<i;j++)
    			if(a[i]<a[j])
    			{
    				temp=a[i];
    				a[i]=a[j];
    				a[j]=temp;
    			}
    	}
    	printf("nDescending order....n");
    	for(i=0;i<n;i++)
    	{
    		printf(" %d  ",a[i]);
    	}
     
    	getch();
    }
  • 073. Write a program to accept a mXn matrix and reprint it in matrix form in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,n,a[5][5];
    	clrscr();
    	printf("Enter order of matrix :n");
            scanf("%d%d",&m,&n);
    	printf("Enter matrix elements.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			scanf("%d",&a[i][j]);
     
    	for(i=0;i<m;i++)
    	{
    		for(j=0;j<n;j++)
    			printf("%d ",a[i][j]);
    		printf("n");
    	}
    	getch();
    }
  • 074. Write a program to accept two M x N matrices and add them in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,n,a[5][5],b[5][5],c[5][5];
    	clrscr();
    	printf("Enter order of matrix :n");
    	scanf("%d%d",&m,&n);
    	printf("Enter matrix elements of first matrix.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			scanf("%d",&a[i][j]);
     
    	printf("Enter matrix elements of second matrix.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			scanf("%d",&b[i][j]);
     
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			c[i][j]=a[i][j]+b[i][j];
     
    	printf("Resultant matrix .....n");
    	for(i=0;i<m;i++)
    	{
    		for(j=0;j<n;j++)
    			printf("%d ",c[i][j]);
    		printf("n");
    	}
    	getch();
    }
  • 075. Write a program to accept a M x N matrix and print addition of their array elements in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,n,a[5][5],sum=0;
    	clrscr();
    	printf("Enter order of matrix :n");
    	scanf("%d%d",&m,&n);
    	printf("Enter matrix elements.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			scanf("%d",&a[i][j]);
     
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			sum=sum+a[i][j];
     
    	printf("nSum of matrix elements.....%d",sum);
    	getch();
    }
  • 076. Write a program to accept a M x N matrix and print addition of diagonal elements of that matrix in C language
     
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,a[5][5],sum=0;
    	clrscr();
    	printf("Enter order of matrix :n");
    	scanf("%d",&m);
    	printf("Enter matrix elements.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<m;j++)
    			scanf("%d",&a[i][j]);
     
    	for(i=0;i<m;i++)
    		for(j=0;j<m;j++)
    			if(i==j)
    				sum=sum+a[i][j];
     
    	printf("nSum of diagonal elements.....%d",sum);
    	getch();
    }
  • 077. Write a program to accept a M x N matrix and print addition of upper triangular matrix elements in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,a[5][5],sum=0;
    	clrscr();
    	printf("Enter order of matrix :n");
    	scanf("%d",&m);
    	printf("Enter matrix elements.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<m;j++)
    			scanf("%d",&a[i][j]);
     
    	for(i=0;i<m;i++)
    		for(j=0;j<m;j++)
    			if(i<j)
    				sum=sum+a[i][j];
     
    	printf("nSum of upper triangular elements.....%d",sum);
    	getch();
    }
  • 078. Write a program to accept a M x N matrix and print addition of lower triangular matrix elements in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,a[5][5],sum=0;
    	clrscr();
    	printf("Enter order of matrix :n");
    	scanf("%d",&m);
    	printf("Enter matrix elements.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<m;j++)
    			scanf("%d",&a[i][j]);
     
    	for(i=0;i<m;i++)
    		for(j=0;j<m;j++)
    			if(i>j)
    				sum=sum+a[i][j];
     
    	printf("nSum of lower triangular elements.....%d",sum);
    	getch();
    }
  • 079. Write a program to accept two M X N matrices and print their multiplication in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,n,k,a[5][5],b[5][5],c[5][5];
    	clrscr();
    	printf("Enter order of matrix :n");
    	scanf("%d%d",&m,&n);
    	printf("Enter matrix elements of first matrix.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			scanf("%d",&a[i][j]);
     
    	printf("Enter matrix elements of second matrix.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			scanf("%d",&b[i][j]);
     
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    		{
    			c[i][j]=0;
    			for(k=0;k<n;k++)
    				c[i][j]=c[i][j]+a[i][k]*b[k][j];
    		}
     
    	printf("Resultant matrix .....n");
    	for(i=0;i<m;i++)
    	{
    		for(j=0;j<n;j++)
    			printf("%d ",c[i][j]);
    		printf("n");
    	}
    	getch();
    }
  • 080. Write a program to accept a M x N matrix and print it’s transpose matrix in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,n,a[5][5];
    	clrscr();
    	printf("Enter order of matrix :n");
    	scanf("%d%d",&m,&n);
    printf("Enter matrix elements.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			scanf("%d",&a[i][j]);
     
    	for(i=0;i<m;i++)
    	{
    		for(j=0;j<n;j++)
    			printf("%d ",a[j][i]);
    		printf("n");
    	}
    	getch();
    }

080. Write a program to accept a M x N matrix and print it’s transpose matrix in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i,j,m,n,a[5][5];
	clrscr();
	printf("Enter order of matrix :\n");
	scanf("%d%d",&m,&n);
printf("Enter matrix elements.....\n");
	for(i=0;i<m;i++)
		for(j=0;j<n;j++)
			scanf("%d",&a[i][j]);
 
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
			printf("%d ",a[j][i]);
		printf("\n");
	}
	getch();
}

079. Write a program to accept two M X N matrices and print their multiplication in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i,j,m,n,k,a[5][5],b[5][5],c[5][5];
	clrscr();
	printf("Enter order of matrix :\n");
	scanf("%d%d",&m,&n);
	printf("Enter matrix elements of first matrix.....\n");
	for(i=0;i<m;i++)
		for(j=0;j<n;j++)
			scanf("%d",&a[i][j]);
 
	printf("Enter matrix elements of second matrix.....\n");
	for(i=0;i<m;i++)
		for(j=0;j<n;j++)
			scanf("%d",&b[i][j]);
 
	for(i=0;i<m;i++)
		for(j=0;j<n;j++)
		{
			c[i][j]=0;
			for(k=0;k<n;k++)
				c[i][j]=c[i][j]+a[i][k]*b[k][j];
		}
 
	printf("Resultant matrix .....\n");
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
			printf("%d ",c[i][j]);
		printf("\n");
	}
	getch();
}

078. Write a program to accept a M x N matrix and print addition of lower triangular matrix elements in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i,j,m,a[5][5],sum=0;
	clrscr();
	printf("Enter order of matrix :\n");
	scanf("%d",&m);
	printf("Enter matrix elements.....\n");
	for(i=0;i<m;i++)
		for(j=0;j<m;j++)
			scanf("%d",&a[i][j]);
 
	for(i=0;i<m;i++)
		for(j=0;j<m;j++)
			if(i>j)
				sum=sum+a[i][j];
 
	printf("\nSum of lower triangular elements.....%d",sum);
	getch();
}

077. Write a program to accept a M x N matrix and print addition of upper triangular matrix elements in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i,j,m,a[5][5],sum=0;
	clrscr();
	printf("Enter order of matrix :\n");
	scanf("%d",&m);
	printf("Enter matrix elements.....\n");
	for(i=0;i<m;i++)
		for(j=0;j<m;j++)
			scanf("%d",&a[i][j]);
 
	for(i=0;i<m;i++)
		for(j=0;j<m;j++)
			if(i<j)
				sum=sum+a[i][j];
 
	printf("\nSum of upper triangular elements.....%d",sum);
	getch();
}

076. Write a program to accept a M x N matrix and print addition of diagonal elements of that matrix in C language

 
#include<stdio.h>
#include<conio.h>
main()
{
	int i,j,m,a[5][5],sum=0;
	clrscr();
	printf("Enter order of matrix :\n");
	scanf("%d",&m);
	printf("Enter matrix elements.....\n");
	for(i=0;i<m;i++)
		for(j=0;j<m;j++)
			scanf("%d",&a[i][j]);
 
	for(i=0;i<m;i++)
		for(j=0;j<m;j++)
			if(i==j)
				sum=sum+a[i][j];
 
	printf("\nSum of diagonal elements.....%d",sum);
	getch();
}

075. Write a program to accept a M x N matrix and print addition of their array elements in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i,j,m,n,a[5][5],sum=0;
	clrscr();
	printf("Enter order of matrix :\n");
	scanf("%d%d",&m,&n);
	printf("Enter matrix elements.....\n");
	for(i=0;i<m;i++)
		for(j=0;j<n;j++)
			scanf("%d",&a[i][j]);
 
	for(i=0;i<m;i++)
		for(j=0;j<n;j++)
			sum=sum+a[i][j];
 
	printf("\nSum of matrix elements.....%d",sum);
	getch();
}

074. Write a program to accept two M x N matrices and add them in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i,j,m,n,a[5][5],b[5][5],c[5][5];
	clrscr();
	printf("Enter order of matrix :\n");
	scanf("%d%d",&m,&n);
	printf("Enter matrix elements of first matrix.....\n");
	for(i=0;i<m;i++)
		for(j=0;j<n;j++)
			scanf("%d",&a[i][j]);
 
	printf("Enter matrix elements of second matrix.....\n");
	for(i=0;i<m;i++)
		for(j=0;j<n;j++)
			scanf("%d",&b[i][j]);
 
	for(i=0;i<m;i++)
		for(j=0;j<n;j++)
			c[i][j]=a[i][j]+b[i][j];
 
	printf("Resultant matrix .....\n");
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
			printf("%d ",c[i][j]);
		printf("\n");
	}
	getch();
}

073. Write a program to accept a mXn matrix and reprint it in matrix form in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i,j,m,n,a[5][5];
	clrscr();
	printf("Enter order of matrix :\n");
        scanf("%d%d",&m,&n);
	printf("Enter matrix elements.....\n");
	for(i=0;i<m;i++)
		for(j=0;j<n;j++)
			scanf("%d",&a[i][j]);
 
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
			printf("%d ",a[i][j]);
		printf("\n");
	}
	getch();
}

072. Write a program to accept a number n from user and then accept n array elements from user, print these array elements in ascending and descending order in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int n,i,a[20],j,max,min,temp;
	clrscr();
	printf("Enter number : ");
	scanf("%d",&n);
	printf("Enter array elements...\n");
	for(i=0;i<n;i++)
	{
	printf("Enter element %d : ",i+1);
		scanf("%d",&a[i]);
	}
	max=a[0];
	for(i=0;i<n;i++)
	{
		for(j=0;j<i;j++)
			if(a[i]>a[j])
			{
				temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}
	}
	printf("Ascending order.....\n");
	for(i=0;i<n;i++)
	{
		printf(" %d  ",a[i]);
	}
 
	min=a[0];
	for(i=0;i<n;i++)
	{
		for(j=0;j<i;j++)
			if(a[i]<a[j])
			{
				temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}
	}
	printf("\nDescending order....\n");
	for(i=0;i<n;i++)
	{
		printf(" %d  ",a[i]);
	}
 
	getch();
}