Logical Programs using Arrays 

This “Logical programs using Arrays in C” page providing some of the code programs to explain how to use arrays in C programming.


 

Program to sum all the array elements :
#include<stdio.h>
void main()
{
int arr[5]={10,20,30,40,50},i,sum=0;
for(i=0 ; i<5 ; i++)
{
sum=sum+arr[i];
}
printf(“sum : %d\n”,sum);
}


Program to sum all the even integers of array :
#include<stdio.h>
void main()
{
int arr[7]={4,7,3,8,2,9,6},i,even_sum=0;
for(i=0 ; i<7 ; i++)
{
if(arr[i]%2==0)
{
even_sum+=arr[i];
}
}
printf(“Even_sum : %d\n”,even_sum);
}


WAP to find largest element in the array :
#include<stdio.h>
void main()
{
int arr[5]={5,17,13,27,19},i,large;
large = arr[0];
for(i=1 ; i<5 ; i++)
{
if(large<arr[i])
{
large=arr[i];
}
}
printf(“large : %d\n”,large);
}

Share this