Question:

In Java, how would I write an algorithm that adds all even numbered integers from 1-100&

by  |  earlier

0 LIKES UnLike

separated by a comma?

So far:

private static FileOutputStream output;

private static FileInputStream input;

private static ArrayList<Integer> inputList;

public static void main(String[] args) throws IOException

{

createOutputFile();

createInputFile();

printInputList(); }

private static void createOutputFile() throws IOException

{ //create the output file

output = new FileOutputStream("EvenNums.txt");

//output test values to the file

for(int i = 1; i <= 100; i )

output.write(i);

//close the output stream

output.close();

Help!

 Tags:

   Report

1 ANSWERS


  1. To print all even numbers from 1 to 100 to standart output:

    for (int i = 2; i &lt;= 100; i=i+2 ) {

      System.out.print(i);

      if (i&lt;100) {

        System.out.print(&quot;, &quot;);

      }

    }

    Сhange it to print to your file.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.