C Program to read and print elements of array

Category: C Program

C Program to read and print elements of an array using loops and recursion

C Program to read and print elements of an array using loops

#include <stdio.h>

void main()
{
    int n, i, arr[50];
    printf("Enter number of elements: ");
    scanf("%d", &n);

    printf("Enter array elements -\n");
    for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    printf("\nArray elements are -\n");
    for (i = 0; i < n; i++)
        printf("%d\n", arr[i]);
}

C Program to read and print elements of an array using recursion

#include <stdio.h>

void printArray(int arr[], int start, int end)
{
    if (start > end)
        return;
    printf("%d\n", arr[start]);
    printArray(arr, start+1, end);
}

void main()
{
    int n, i, arr[50];
    printf("Enter number of elements: ");
    scanf("%d", &n);

    printf("Enter array elements -\n");
    for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    printf("\nArray elemenst are-\n");
    printArray(arr, 0, n-1);
}

Output

Enter number of elements: 5

Enter array elements -
5
4
3
2
1

Array elemenst are-
5
4
3
2
1

Recommended Posts