Tuesday 12 March 2019

Declaration of one dimensional array of numbers and characters in JAVA


-Declare and initialize two one dimensional arrays with 1) Numbers  2) Characters 1.1-Print the content of both arrays 1.2-Get the length of both arrays and print 


import java.util.Scanner;
public class SimpleJavaProgram
{
public static void main(String []args)
{
int arr[];


Scanner s=new Scanner(System.in);
int x;
System.out.println("How many elements you want to store in an array?");
x=s.nextInt();
arr=new int[x];
for(int i=0;i<arr.length;i++)
{
arr[i]=s.nextInt();
}
System.out.println("Print the values");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}

System.out.println("How many number you want to store in charactor array?");
int c=s.nextInt();
char ch[]=new char[c];

for(int i=0;i<ch.length;i++)
{
ch[i]=s.next().charAt(0);
}
for(int i=0;i<ch.length;i++)
{
System.out.println(ch[i]);
}
System.out.print(arr.length);
}
}

0 comments:

Post a Comment