Arrays in C – Full explanation with examples and tutorials

Write a program to store 10 numbers in an array

//take input of 10 numbers in array and print them
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i, a[10];
printf("Enter the 10 number of the array\n");
for(i=0;i<10;i++)
{
scanf("%d", &a[i]);
}
printf("The elements of the Array are\n");
for(i=0;i<10;i++)
{
printf("A[%d]=%d\n",i,a[i]);
}
getch();
}

Next, write a program to store 10 numbers in an array and print them in the reverse order

//take the input of 10 numbers from user and print in reverse order
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i;
printf("enter the 10 numbers to be printed in reverse\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("The reverse order of the numbers are\n");
for(i=9;i>=0;i--)
{
printf("%d\n",a[i]);
}
getch();
}

Write a program to store numbers in two arrays. Add the contents of the two arrays and store the result in a third array.

//take data in two arrays from user, add them, and store result in third array
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],b[5],sum[5],i;
printf("Enter elements of first array\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("Enter elements of second array\n");
for(i=0;i<5;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<5;i++)
{
sum[i]=a[i]+b[i];
}
printf("The sum of the two arrays is\n");
for(i=0;i<5;i++)
{
printf("%d\n",sum[i]);
}
getch();
}

Write a program in C to store find the squares of the elements in an array

//take 10 numbers in an array and print square of the numbers in the same array
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int a[10],i;
printf("Enter the elements of the array which you want squared\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("The squared array is \n");
for(i=0;i<10;i++)
{
a[i]=pow(a[i],2);
printf("%d\n",a[i]);
}
getch();
}

How can you find if the numbers in an array are positive, negative, even or odd?

//take 10 numbers in an array and print how many positive,negative,even and odd
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,countpos=0,countneg=0,counteven=0,countodd=0,countevenodd=0;
printf("Enter 10 varied integers for an array\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
if(a[i]>=0)
{
countpos++;
}
else
{
countneg++;
}
countevenodd=a[i]%2;
if(countevenodd==0)
{
counteven++;
}
else
{
countodd++;
}
}
printf("Even numbers = %d\n",counteven);
printf("Odd numbers = %d\n",countodd);
printf("Positive numbers = %d\n",countpos);
printf("Negative numbers = %d\n",countneg);
getch();
}

Write a program in C to find the largest and smallest element in an array

//Take an array from the user and find the largest and smallest
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,greatest,lowest,a[10];
printf("Enter the elements of an array\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
greatest = a[0];
for(i=1;i<10;i++)
{
if(greatest>a[i])
greatest = greatest;
else
greatest = a[i];
}
printf("Greatest=%d",greatest);
lowest = a[0];
for(i=1;i<10;i++)
{
if(lowest<a[i])
lowest=lowest;
else
lowest=a[i];
}
printf("\nLowest=%d",lowest);
getch();
}

Write a program using C to arrange the elements of an array in an ascending order

//take in 10 elements of an array and rearrange the array in increasing order
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,a[10],temp;
printf("Enter the elements of the array\n");
for(i=0;i<10;i++)
{
scanf("%d", &a[i]);
}
for(int j=0;j<10;j++)
{
for(i=0;i<10;i++)
{
if(a[i]<a[i+1])
{
a[i]=a[i];
}
else
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf("The array arranged in increasing order is\n");
for(i=0;i<10;i++)
{
printf("%d\n",a[i]);
}
getch();
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.