Write a program on 1d array to display a no - Algomentor

Write a program on 1d array to display a no - data structure and algorithm made easy

one d array in c++ and java

Hi, welcome to the series of data structure and algorithm made easy this session of data structure and algorithm we learn about 4 programs the program is all about how to take an array and print the elements of the array.


  1. the first program is how to print static array in c++.
  2. the second program is about how to take an array as input and print the elements of the array in c++.
  3. he first program is how to print static array in java.
  4. the second program is about how to take an array as input and print the elements of the array in java.

So in the first program of array data structure, we will learn about basic coding of the array so first, we declare an array with predefined size "10" we just print the number we implement this program in c++.

Here is the code



#include<iostream>

using namespace std;

int main()

{

int arr[10] = {1,2,3,4,5,6,7,8,9,10};

for(int i=0;i<10;i++)

{

cout<<arr[i]<<" ";

}
}

the output of the following code is

one d array in c++ and java


In the second program of array data structure, we will ask user to input the size of the array and then we take input one by one and we print the array we implement this program in c++.

Here is the code

#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"printing Array element"<<endl;
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}

the output of the following code is

one d array in c++ and java



So in the first program of array data structure, we will learn about basic coding of the array so first, we declare an array with predefined size "10" we just print the number we implement this program in java.

Here is the code

import java.lang.*;
import java.io.*;

class Algomentor   //give your own class name
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
try {
    int arr[] = {1,2,3,4,5,6,7,8,9,10};
    for(int i=0;i<10;i++)
        {
        System.out.print(arr[i]+" ");
        }
} catch(Exception e) {
} finally {
}
}
}

the output of the following code is

one d array in c++ and java



In the second program of array data structure, we will ask the user to input the size of the array and then we take input one by one and we print the array we implement this program in java.

Here is the code

import java.lang.*;
import java.util.*;
import java.io.*;

class Algomentor   //give your own class name
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
try {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int arr[] = new int[n];
    for(int i=0;i<n;i++)
        {
        arr[i] = sc.nextInt();
        }
    for(int i=0;i<n;i++)
        {
        System.out.print(arr[i]+" ");
        }
} catch(Exception e) {
} finally {
}
}
}


the output of the following code is

one d array in c++ and java

To run the c++ code you need to install c++ I already made a blog for this How to install c++ you can read the article to hear.

For running your code in java If you want to run code locally you need to install Java JDK for it and need a text editor if you are using the online editor I recommend you to use CodeChef Editor for this is super easy to use and great fun.

So I you end up like reading our series of data structure and algorithm made easy blog and want more useful information you can follow us on the social handle you can message us there we will surely reply their and if you have any queries about this you can email us at algomentor@gmail.com.

Thank you so much for reading about lessons on data structure and algorithm and don't forget to share with your friends 😃.

Hi if you want to write a post on our website then please check this article on How to do a guest post.

Post a Comment

0 Comments