C program to read and print elements of an one dim array
C program to read and print elements of an one dim array
In this tutorial, we will discuss the concept of C program to read and print elements of an one dimensional array
In this topic, we are going to learn how to read and print elements of an one dimensional array in C programming language using for loop while loop and do-while loop
We have already discuss that “What is an array”, type of arrays and how to access it
Arrays are the special type of variables to store Multiple type of values(int, string,char, etc)under the same name in the continuous memory location. we can be easily accessed through the indices(indexes)
Here,
- e1,e2,e3,e4 and e5 represent the elements of the array
- 0,1,2,3 and 4 represent the index of the array which is used to access the array elements
- 1,2,3,4 and 5 represent the length of the array
Code to read and print elements in a one dim array
Read and print elements Using for loop
In this program, we are briefing input elements of an array and then print them using for loop in C language
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int i,len; printf("Enter the Array length: "); scanf("%d",&len); int marks[len]; printf("\n"); printf("Enter the marks :"); for(i=0; i<len; i++){//input using for loop scanf("%d",&marks[i]); } printf("\ndisplay the marks\n"); for(i=0; i<len; i++){//display elements using for loop printf("%d\n",marks[i]); } getch(); return 0; }
When the above code is executed, it produces the following result
Program 2
In this program, we are briefing input elements of an array and then print them using for loop in C language
#include <stdio.h> #include <stdlib.h> int main() { int i,len; printf("Enter the Array length: "); scanf("%d",&len); int marks[len]; printf("\n"); printf("Enter the marks :"); for(i=0; i<len; i++){//input using for loop scanf("%d",&marks[i]); } printf("\ndisplay the marks\n"); for(i=0; i<len; i++){//display elements using for loop printf("%d\n",marks[i]); } getch(); return 0; }
When the above code is executed, it produces the following result
Read and print elements Using while loop
In this program, we are briefing input elements of an array and then print them using while loop in C language
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int i,len; printf("Enter the Array length: "); scanf("%d",&len); int marks[len]; printf("\n"); printf("Enter the marks :"); i=0; while(i<len){//input using while loop scanf("%d",&marks[i]); i++; } printf("\ndisplay the marks\n"); i=0; while(i<len){//display elements using while loop printf("%d\n",marks[i]); i++; } getch(); return 0; }
When the above code is executed, it produces the following result
Program 2
In this program, we are briefing input elements of an array and then print them using while loop in C language
#include <stdio.h> #include <stdlib.h> int main() { int i,len; printf("Enter the Array length: "); scanf("%d",&len); int marks[len]; printf("\n"); i=0; while(i<len){//input using while loop printf("Enter the value marks[%d] :",i); scanf("%d",&marks[i]); i++; } printf("\ndisplay the marks\n"); i=0; while(i<len){//display elements using while loop printf("marks[%d] :%d\n",i,marks[i]); i++; } getch(); return 0; }
When the above code is executed, it produces the following result
Read and print elements Using do-while loop
In this program, we are briefing input elements of an array and then print them using do-while loop in C language
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int i,len; printf("Enter the Array length: "); scanf("%d",&len); int marks[len]; printf("\n"); printf("Enter the marks :"); i=0; do{//input using do-while loop scanf("%d",&marks[i]); i++; }while(i<len); printf("\ndisplay the marks\n"); i=0; do{//display elements using do-while loop printf("%d\n",marks[i]); i++; }while(i<len); getch(); return 0; }
When the above code is executed, it produces the following result
Program 2
In this program, we are briefing input elements of an array and then print them using do-while loop in C language
#include <stdio.h> #include <stdlib.h> int main() { int i,len; printf("Enter the Array length: "); scanf("%d",&len); int marks[len]; printf("\n"); i=0; do{//input using do-while loop printf("Enter the value marks[%d] :",i); scanf("%d",&marks[i]); i++; }while(i<len); printf("\ndisplay the marks\n"); i=0; do{//display elements using do-while loop printf("marks[%d] :%d\n",i,marks[i]); i++; }while(i<len); getch(); return 0; }
When the above code is executed, it produces the following result
Similar post
Code to read and print integers of an array in Java
Code to read and print strings of an array in Java
Code to read and print characters of an array in Java
Code to read and print integers of an array in C++
Code to read and print strings of an array in C++
Code to read and print characters of an array in C++
Code to read and print integers of an array in C
Code to read and print strings of an array in C
Suggested for you
One dimensional array in C language
One dimensional array in C++ language
Two dimension array in Java language
Two dimension array in C language
Two dimension array in C++ language
Three dimension array in Java language
Three dimension array in C language
Three dimension array in C++ language