Program to Scan and Print Array of Size n

Program to Scan and Print Array of Size n

  • Write a program to Scan and Print Array of Size n in C
  • Write a program to Scan and Print Array of Size n in C++
  • Write a program to Scan and Print Array of Size n in Python
  • Write a program to Scan and Print Array of Size n in PHP
  • Write a program to Scan and Print Array of Size n in Java
  • Write a program to Scan and Print Array of Size n in Java Script
  • Write a program to Scan and Print Array of Size n in C#

Explanation:

Input Array Size:

  • Read the size nnn of the array.

Scan Elements:

  • Use a loop to read nnn elements from the user and store them in the array.

Print Elements:

  • Use another loop to iterate through the array and print each element.

Program to Scan and Print Array of Size n

#include <stdio.h>

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

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter the size of the array: ";
    cin >> n;
    int arr[n];
    
    cout << "Enter " << n << " elements:" << endl;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    
    cout << "Array elements:" << endl;
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}

n = int(input("Enter the size of the array: "))
arr = []

print(f"Enter {n} elements:")
for _ in range(n):
    arr.append(int(input()))

print("Array elements:")
print(" ".join(map(str, arr)))

<?php
echo "Enter the size of the array: ";
$n = intval(trim(fgets(STDIN)));

$arr = [];
echo "Enter $n elements:\n";
for ($i = 0; $i < $n; $i++) {
    $arr[] = intval(trim(fgets(STDIN)));
}

echo "Array elements:\n";
foreach ($arr as $element) {
    echo $element . " ";
}
?>

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter the size of the array: ");
        int n = scanner.nextInt();
        int[] arr = new int[n];
        
        System.out.println("Enter " + n + " elements:");
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("Array elements:");
        for (int element : arr) {
            System.out.print(element + " ");
        }
    }
}

const prompt = require('prompt-sync')();

const n = parseInt(prompt("Enter the size of the array: "));
const arr = [];

console.log(`Enter ${n} elements:`);
for (let i = 0; i < n; i++) {
    arr.push(parseInt(prompt()));
}

console.log("Array elements:");
console.log(arr.join(" "));

using System;

class Program {
    static void Main() {
        Console.Write("Enter the size of the array: ");
        int n = int.Parse(Console.ReadLine());
        int[] arr = new int[n];
        
        Console.WriteLine($"Enter {n} elements:");
        for (int i = 0; i < n; i++) {
            arr[i] = int.Parse(Console.ReadLine());
        }
        
        Console.WriteLine("Array elements:");
        foreach (int element in arr) {
            Console.Write(element + " ");
        }
    }
}

List of All Programs