An array is defined as the collection of similar type of data items stored at contiguous memory locations. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations. An array index starts with 0.
Syntax:-
data_type array_name[array_size];
int marks[5];
Example:-
#include<stdio.h>
int main(){
int i=0;
int marks[5];//declaration of array
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}//end of for loop
return 0;
}
Output:-
80
60
70
85
75
Exercises:-
- Write a C program to read and print elements of array.
- Write a C program to print all negative elements in an array.
- Write a C program to find sum of all array elements.
- Write a C program to find maximum and minimum element in an array.
- Write a C program to find reverse of an array.
- Write a C program to search an element in an array.
Comments
Post a Comment