| | |
Write a program for even numbers..
![]() |
•
•
Join Date: Mar 2009
Posts: 3
Reputation:
Solved Threads: 0
So I have to write a program which writes all even numbers between 0 and 50 INTO AN ARRAY and then display them on the screen....
My problem is how i am going to put it into an array I only did part of it but I can't continue doing it...:-
public class Even_Number{
public static void main (String args[]) {
int L[]=new int[50];
int i;
int c =0;
for (i=0; i<50; i++)
if (i%2==0)
{
c++;
L[c] = i;
}
}
}
My problem is how i am going to put it into an array I only did part of it but I can't continue doing it...:-
public class Even_Number{
public static void main (String args[]) {
int L[]=new int[50];
int i;
int c =0;
for (i=0; i<50; i++)
if (i%2==0)
{
c++;
L[c] = i;
}
}
}
Why can't you continue doing it ? As far as I am seeing you are going good.
Just one thing, if you know you are going to put only even numbbers in the array from amongst 50 numbers, instead of declaring the array as
you can safely do this:
Out of any set of fifty numbers there are never going to be more than 25 even numbers, isn't it so you are saving space on 25 numbers where each one is 4 bytes 25 x 4 = 100 bytes.
Just one thing, if you know you are going to put only even numbbers in the array from amongst 50 numbers, instead of declaring the array as
java Syntax (Toggle Plain Text)
int L[]=new int[50];
you can safely do this:
java Syntax (Toggle Plain Text)
int [] L = new int[50/2];
Out of any set of fifty numbers there are never going to be more than 25 even numbers, isn't it so you are saving space on 25 numbers where each one is 4 bytes 25 x 4 = 100 bytes.
Last edited by verruckt24; Mar 13th, 2009 at 1:28 pm.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
and you could do the next:
there's not really the need to perform a check for evens.
with the (i % 2) test you'll get the right contents, but it's easier just to add 2 every time during the loop
Java Syntax (Toggle Plain Text)
int[] evens = new int[25]; // as Verruckt pointed out, you don't need one of size 50 int location = 0; for ( int i = 2; i < 50; i = i + 2){ evens[location] = i; location += 1; }
there's not really the need to perform a check for evens.
with the (i % 2) test you'll get the right contents, but it's easier just to add 2 every time during the loop
![]() |
Similar Threads
- Numbers in Turbo C++ (C++)
- how to write a program for power (C)
- C++ hints to write a program, please help (C++)
- I have to write a program to read 10 integers into an array (C++)
- Write a C++ program, I need help (C++)
- Read/write to same file > once, Help (C++)
- Trying to write a program that you can enter #s and multiply at the end. (Java)
- write a program that simulates rooling a pair of dice. (Java)
Other Threads in the Java Forum
- Previous Thread: How to build TreePath?
- Next Thread: Creating a Jave file Dynamically
| Thread Tools | Search this Thread |
addball android api applet application apps array arrays automation awt binary bluetooth businessintelligence busy_handler(null) button card chat class client code collision component constructor crashcourse css database draw eclipse ee error eventlistener exception fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jni jpanel jtree julia jvm linux list loan machine map method methods migrate mobile netbeans newbie oracle output phone physics plazmic problem program programming project radio recursion scanner server service set sharepoint smart sms socket software sort sortedmaps sql string swing textfield threads transfer tree trolltech unlimited utility webservices windows





