Write a program that will ask the user to enter a number and then place the 10 consecutive numbers in a single-dimensional array starting from the number entered by the user. The program will then display the elements inside index 2 and 4 and 6.
Write a program that will ask the user to enter a number and then place the 10 consecutive numbers in a single-dimensional array starting from the number entered by the user. The program will then display the elements inside index 2 and 4 and 6.
If you need help, please post the particular area you are struggling with and what you have tried. If you want someone to do this for you, pay a programmer.
import java.util.*;
public class SetB4
{
public static void main(String args[])
{
int[] a=new int[10];
Scanner sc=new Scanner(System.in);
System.out.println("Please enter elements...");
for(int j=0;j<10;j++)
a[j]=sc.nextInt();
}
}
------------------------------
sorry kcomissiong :)
this program work but the problem
is I'm struggle to print the output...
I try the
System.out.println([2][4][5]);
but it didn't work it say illegal statement :(
need help
first off you are asking the user to put in 10 numbers, not put in 1 number and then populate the array. This might be nit picking though.
It should be:
system.out.println("Enter the number");
int baseNumber = sc.nextInt();
for(I=0;i<10;i++)a[i]=i+1+baseNumber;
Printing it out you need to index the proper elements:
printf(" Elements, 2,4,6 are: %d %d %d", a[2], a[4], a[6]);
Figure out how to output variables and make sure you're indexing the proper place in the array. Google how if you're confused. That is how I, being a brilliant programmer, figure things like this out.
Thank you CravenMorhead :)
All times are GMT -7. The time now is 05:59 AM. |