Two dimensional (2D) array Program in C

Two dimensional (2D) array Program in C

#include<stdio.h>        //library or header file

main()            //main function from program start executing
{

int a[2][3];            //initialize array
int i,j,sum=0;          //intialize variable i and sum which we use in loop

printf("enter array elements on screen:\n");   //print message on screen

for(i=0;i<2;i++)
{
  for(j=0;j<3;j++)
  {

  scanf("%d",&a[i][j]);        //user give input to array

  }
}

printf("\n");      //newline after printing message on screen

for(i=0;i<2;i++)
{
  for(j=0;j<3;j++)
  {

   printf("Element are:%d\n",a[i][j]);     //print elements which user entered in array a[i][j]

  }
}


printf("\n");

for(i=0;i<2;i++)
{
  for(j=0;j<3;j++)
    {

      sum=sum+a[i][j];     //sum array elements

    }
 
   printf("sum is:%d\n",sum);    //print sum on screen
 
   sum=0;        // intialize sum=0 again because after i=0, j=0,1,2 loop run sum is performed. sum1=a[0][0]+a[0][1]+a[0][2] so when again in outer loop then intuialize sum=0 otherwise sum1 is  added in i=1,j=0,1,2

}

}     //main function end

Related Posts