Arrays in C
1.What is Arrays in C ?
Ans: An array is collection of a similar type of data. Array is one of the most used data structures in C programming. It is a simple and fast way of storing multiple values under a single name.
1. Array Deceleration
Syntax : data_type array_name[size];
Example : int a[9];
2. Array Initialization :
Syntax : data_type array_name[9];
Example : int a[10] = {40,55,63,17,22,68,89,97,89}
3. Access array element :
Syntax : array_name[index no.];
Example : a[3]=17
Array Initialization with declaration : It means that we initialized the array and also assign the series of number in curly bracket { }.
Array Initialization without declaration : It means that we initialized the array and take the number of series from users.
Example :
1. Write a C program to print user input elements of array.
#include<stdio.h>
int main ()
{
int a[100];
int i,c;
printf("Enter the size of elements ");
scanf("%d",&c);
printf("Enter the series of number\n");
for (i=0;i<c;i++)
{
scanf("%d",&a[i]);
}
for (i=0;i<c;i++)
{
printf("%d : %d\n",i,a[i]);
}
}
Output :
2. Write a C program to find the Largest number in an array.
#include<stdio.h>
int main ()
{
int a[1000];
int num,i,j,s=0;
printf("Enter the size of the element ");
scanf("%d",&num);
printf("Enter the series of number\n ");
for (i=0;i<num;i++)
{
scanf("%d",&a[i]);
}
for (j=0;j<num;j++)
{
if (s<a[j])
{
s=a[j];
}
}
printf("%d is the larger number in series\n",s);
}
Output :
#include<stdio.h>
int main ()
{
int a[100];
int i,c,multi=1;
printf("Enter the size of elements ");
scanf("%d",&c);
printf("Enter the series of number\n");
for (i=0;i<c;i++)
{
scanf("%d",&a[i]);
}
for (i=0;i<c;i++)
{
printf("%d : %d\n",i,a[i]);
multi=a[i]*multi;
}
printf("miltiplication is %d",multi);
}
Output :
0 Comments