Program to Read and Display entered numbers using an Array

Description

Introduction:
An Array is a user-defined data type, which is collection of similar data elements.
All the information stored in an array should be the same data type.
Array is used to process large amount of data.
The elements of the array are stored in consecutive memory locations.
Syntax:
The syntax of array declaration is: type name[size];
Type should be valid data type.
Number within bracket indicates the size of the array.
The index of the array starts from zero.
Access of Array elements:
    There is no single statement that can read, access or print all the elements of array.
    To access all the elements, must use a loop.
    All the elements of an array can be accessed by varying the value of the subscript.
    Subscript must be an integral value.
Storing values in Arrays:
    When array is declared, space is allocated for elements, no values are stored in the array.
    There are three ways to store values in array:
    1) To initialize the array elements during  declaration.
        When an array is initialized, need to provide value for every element in the array.
        Syntax:
            type array_name[size] = {list of values};
            Values are written within curly brackets and every value is separated by comma.
        It is compiler error to specify more values than there are elements in the array.
        While initializing the array at the time of declaration, the size can be omitted,
        the compiler will allocate space for all the initialized elements.
        Example:
             int marks[] = {98,97,90};
        If the number of values provided is less than the number of elements in the array,
        the un-assigned elements are filled with zeo.
    2) To input values for individual elements from the keyboard.
        An array can be initialized by inputting values from the keyboard.
        A while/do-while or a for loop is executed to input the value for each elements.
    3) To assign values to individual elements.
        Assign values to individual elements of the array using assignment operator.
        Example:
            array[5] = 100;
        One array can not be assigned to another array, even if same size and type.
        To copy one array to another, copy the values of every elements to another.

C/C++

/*C program to read and display array*/
//Save it as readDisplayArray.c

#include<stdio.h>
int main(){

    int i,n;

    printf("Enter the size of array : ");
    scanf("%d",&n);

    
    /*Declaration of an array*/
    
    int arr[n];

    printf("Enter the elements of array : ");
    for(i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }

    printf("The entered elements are : ");
    for(i=0;i<n;i++){
        printf("%d ", arr[i]);
    }
    
    return 0;
}

Java

/*Java program to read and display array*/
//Save it as ReadDisplayArray.java

import java.io.*;
import java.util.Scanner;

public class ReadDisplayArray {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the size of array : ");
        int n = scanner.nextInt();
        
        /*Declaration of an array*/
        int arr[] = new int[n];
        
        System.out.println("Enter the elements of the array : ");
        for(int i=0;i<n;i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("The elements you entered are : ");
        for(int i=0;i<n;i++) {
            System.out.print(arr[i]+ " ");
        }
    }
}

Related Programs

1) Reversal of an Array
2) Find the Average of Elements of Array
3) Program to form a number using entered digits
4) Program to insert a number at given position of an Array
5) Program to merge two sorted arrays
6) Program to read and display 2D array
7) Program to Search for an Element in an Array
8) Program to Rearrange an array such that arr[i]=i
9) Remove Duplicate Elements From Array
10) Program to make pair of elements alternatively
11) Program to Print How Many Numbers Smaller than Current Number
Share Me

Leave a Reply