Department of Electrical Engineering
Department of Electrical Engineering
Department of Electrical Engineering
Lab Task:
Lab Task 1: Read data into the array: write a program to input 10 numbers using the array.
Code
#include <stdio.h>
void main()
{ int a[10];
int i;
printf("Enter 10 elements in the array:\n");
for(i=0; i<10; i++)
{ printf("Element %d: ",i+1);
scanf("%d",&a[i]);
}
printf("Elements in array are: ");
for(i=0; i<10; i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
Output
Lab Task 2: Print array: Write a program to display the first 10 natural numbers using array.
Code
#include <stdio.h>
int main()
{
int a[10];
int i;
printf("Elements in array are:\n");
for(i=0; i<10; i++)
{
a[i]=i+1;
printf("%d\n",a[i]);
}
return 0;
}
Output
Lab Task 3: Sum and average all elements: Write a program to find the sum and average of
n numbers using the array.
Code
#include <stdio.h>
int main()
{ int I ,n;
float s = 0.0, a;
printf ("Enter the value of n \n");
scanf("%d", &n);
int array[n];
printf("Enter %d numbers \n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Array elements \n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
for (i = 0; i < n; i++)
{ s+=array[i];}
a = s / n;
printf("\n Sum of all numbers = %.2f\n", s);
printf("\n Average of all numbers = %.2f\n", a);
return 0;
}
Output
Lab Task 4: Write a program in C to make such a pattern like a pyramid with numbers
increased by 1.
1
23
456
7 8 9 10
Code
#include <stdio.h>
#include<stdlib.h>
int main()
{
int N=4;
int S;
int T;
int J;
int count=1;
float I;
for(S=1; S<=N; S++)
{
for(J=I; J<=(N-S);J++)
{
printf(" ");
}
for(T=1; T<=S; T++)
{
printf("%d ", count++);
}
printf("\n");
}
return 0;
}
Output
Lab Task 5: Write a program in C to display the n terms of harmonic series and their sum.
A=1+ 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
Code
#include <stdio.h>
int main()
{
int i,n;
float s=0.0;
printf("Enter the number of terms : ");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
if(i<n)
{
printf("1/%d + ",i);
s+=1/(float)i;
}
if(i==n)
{
printf("1/%d ",i);
s+=1/(float)i;
}
}
printf("\nSum of Series upto %d terms : %f \n",n,s);
return 0;
}
Output
Code
#include <stdio.h>
void main()
{
int a[4][4];
int i,j;
printf("Enter 10 elements in the array:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("Element %d,%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("Elements in array are:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}
Output
Lab Task 7:
Initialize all location of the array to zero by the following command.
i.e int A[4][4]={0};
Program Code:
#include<stdlib.h>
#include<stdio.h>
int main()
{
int i[4][4];
int S;
int T;
for(S=0; S<4; S++)
{
for(T=0; T<4; T++)
{
printf("Enter Value:",S,T);
scanf("%d",&i[S][T]);
}
}
printf("Tow dimensional array element:\n");
for(S=0;S<4;S++)
{
for(T=0; T<4; T++)
{
printf("%d",i[S][T]);
if (T==3)
{
printf("\n");
}
}
}
return 0;
}
output
Output
Lab Task 9: Write a code that finds the sum of the first row's elements.
0 1 2 3
0
1
2
3
Code
#include <stdio.h>
void main()
{
int a[4][4];
int i,j;
int sum=0;
printf("Enter 10 elements in the array:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("Element %d,%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("Elements in array are:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=0; i<1; i++)
{
for(j=0; j<4; j++)
{
sum=sum+a[i][j];
}
}
printf("Sum of 1st Row is: %d\n",sum);
}
Output
Lab Task 10: Write a code that finds the sum of the third column's elements.
0 1 2 3
0
Code
#include <stdio.h>
void main()
{
int a[4][4];
int i,j;
int sum=0;
printf("Enter 10 elements in the array:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("Element %d,%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("Elements in array are:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=0; i<4; i++)
{
for(j=2; j<3; j++)
{
sum=sum+a[i][j];
}
}
printf("Sum of 3rd Column is: %d\n",sum);
}
Output
Lab Task 11: Write a code that finds the sum of all elements of the array.
Code
#include <stdio.h>
void main()
{
int a[4][4];
int i,j;
int sum=0;
printf("Enter 10 elements in the array:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("Element %d,%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("Elements in array are:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
sum=sum+a[i][j];
} } printf("Sum of All Elements is: %d\n",sum);
}
Output
Lab Task 12: Write a code that finds the greater number from the whole array.
Code
#include <stdio.h>
int main()
{
int i;
int arr[5];
printf("Enter Elements of Array: \n");
for (i=0; i<5; i++)
{
Conclusion:
The main conclusion of this lab experiment is that we have learned how to implements arrays in c
programing. Arrays are mostly and easily used to solve the basic daily life problem very easily by programming.