Printing numbers up to N into a file

Printing numbers up to N into a file

I'd like to print the first N integers, that is, "1, 2, 3, 4, ..., N-1, N", say N equals 1000, or 10000 or whatever. I'd also like to have the result stored as a file instead of having it only on the screen. How should I do it?

View Answers

November 12, 2010 at 4:46 PM

Hi Friend,

Try the following code:

import java.io.*;
class PrintNumbers{
    public static void main(String[] args) throws Exception{
        FileWriter fstream = new FileWriter(new File("numbers.txt"),true);
        BufferedWriter out = new BufferedWriter(fstream);
        int N=10000;
        for(int i=1;i<=N;i++){
        System.out.println(i);
        out.write(Integer.toString(i));
        out.newLine();
        }
        out.close();
        }
}

Thanks









Related Tutorials/Questions & Answers:
Printing numbers up to N into a file
Printing numbers up to N into a file  I'd like to print the first N integers, that is, "1, 2, 3, 4, ..., N-1, N", say N equals 1000, or 10000 or whatever. I'd also like to have the result stored as a file instead of having
Sum of first n numbers
Sum of first n numbers  i want a simple java program which will show the sum of first n numbers....   import java.util.*; public class...; } System.out.println("Sum of Numbers from 1 to "+n+" : "+sum
Advertisements
Printing numbers in pyramid format - Java Beginners
Printing numbers in pyramid format  Q) Can you please tel me the code to print the numbers in the following format: 1 2 3 4 5 6 7 8 9 10   Hi Friend, Try
ModuleNotFoundError: No module named 'average-n-numbers-calvine'
ModuleNotFoundError: No module named 'average-n-numbers-calvine'  Hi...: No module named 'average-n-numbers-calvine' How to remove the ModuleNotFoundError: No module named 'average-n-numbers-calvine' error? Thanks  
ModuleNotFoundError: No module named 'average-n-numbers-calvine'
ModuleNotFoundError: No module named 'average-n-numbers-calvine'  Hi...: No module named 'average-n-numbers-calvine' How to remove the ModuleNotFoundError: No module named 'average-n-numbers-calvine' error? Thanks  
How to put text file numbers to array and check the position of numbers?
How to put text file numbers to array and check the position of numbers?   I have numbers in text file data.txt 12 9 8 3 1
Probblems with printing if my innput of text file is correct..
Probblems with printing if my innput of text file is correct..  I am trying to make sure this block of code does what I think it will do. Can someone show me with some code how I can print out what is is labled with <----print
Java write even numbers to file
Java write even numbers to file In this section, you will learn how to write the even numbers to file. By using the PrintWriter class, you can write any type of data to the file. It has wide variety of methods for different primitive
how to create a set up file of java web application
how to create a set up file of java web application  I have already... if any one knows how to do the java web application as a set up file, please help... in this case I would like to make my product as a setup file,where my client can
how to create a set up file of java web application
how to create a set up file of java web application  I have already... if any one knows how to do the java web application as a set up file, please help... in this case I would like to make my product as a setup file,where my client can
how to create a set up file of java web application
how to create a set up file of java web application  I have already... to my client, so in this case I would like to make my product as a setup file,where... and applications, so if any one knows how to do the java web application as a set up
printing in swing
printing in swing  How can i print payslip in swing
applet printing
applets gridwise and in each applet i am drawing some graph so while printing whole... but while printing it is not getting printed on paper and rest everything it is printing
how to validate javascriptcode n i am attaching file give validations
how to validate javascriptcode n i am attaching file give validations ...","Ajax_Prepaid_GSM_Verification?q="+str,true); xmlhttp.send(); } <...; <option value="N">No</option>
how to validate javascriptcode n i am attaching file give validations
how to validate javascriptcode n i am attaching file give validations ...","Ajax_Prepaid_GSM_Verification?q="+str,true); xmlhttp.send(); } <...; <option value="N">No</option>
how to validate javascriptcode n i am attaching file give validations
how to validate javascriptcode n i am attaching file give validations ...","Ajax_Prepaid_GSM_Verification?q="+str,true); xmlhttp.send(); } <...; <option value="N">No</option>
how to validate javascriptcode n i am attaching file give validations
how to validate javascriptcode n i am attaching file give validations ...","Ajax_Prepaid_GSM_Verification?q="+str,true); xmlhttp.send(); } <...; <option value="N">No</option>
how to validate javascriptcode n i am attaching file give validations
how to validate javascriptcode n i am attaching file give validations ...","Ajax_Prepaid_GSM_Verification?q="+str,true); xmlhttp.send(); } <...; <option value="N">No</option>
printing in java
printing in java  hello i want to print a jInternalframe containing some lebels,but my printer sent me a paper with the following written on it: please what is the problem
how to validate javascriptcode n i am attaching file give validations
how to validate javascriptcode n i am attaching file give validations  <%@page import="java.sql.SQLException"%> <%@page import...=xmlhttp.responseText; } } xmlhttp.open("GET","Ajax_Prepaid_GSM_Verification?q="+str
Printing stars
Printing stars  I am a beginer and i have one assignment which couldnt give me any idea how to do it..The Question is "Get a string "001222014654" and print o/p as 0 - * 1- ** 2- * 4- ** 5-* 6-* I dont know how to split
Prime Numbers
to enter a positive integer n, and which then creates and populates an int array with the first n prime numbers. Your program should then display the contents... a positive value for n, and should loop until they do so
how to print the content of file in 2d matrix having same dimension as given in file(n*m).
how to print the content of file in 2d matrix having same dimension as given in file(n*m).   here is code: import java.io.File; import... IOException { Scanner s = new Scanner(new File("rfg.txt")); List list = new
we can create our own header file in java?n how to create?
we can create our own header file in java?n how to create?  we can create our own header file in java?n how to create
Application for print pyramid of numbers
Application for print pyramid of numbers  What is the code for printing the numbers like 1 1 1 1 2 2 1 1 3 4 3 1 1 4 7 7 4 1
prime numbers - Java Beginners
prime numbers  Write a java program to find the prime numbers between n and m
Calculate sum of even and odd numbers in Java
Calculate sum of even and odd numbers In this section, you will learn how to read the file that contains even and odd numbers and calculate their sum... numbers from the file and then calculate their sum. Here is the code
Java program for printing document
Java program for printing document  can u send me the java program for printing the office document? email: [email protected]
Prime numbers in Java between 1 and 100
which prints prime numbers between 1 and 100. Here is complete for printing prime...Prime numbers in Java between 1 and 100  Hi, How to display prime numbers in java between 1 and 100? Thanks   Hi, Prime numbers
ModuleNotFoundError: No module named 'Printing'
ModuleNotFoundError: No module named 'Printing'  Hi, My Python... 'Printing' How to remove the ModuleNotFoundError: No module named 'Printing... to install padas library. You can install Printing python with following
printing java series
printing java series  * *** ***** ******** ***** *** *   Post the format properly
Printing 2 arrays
Printing 2 arrays  Hi, I have 2 arrays: String [] head = {"Name...: fieldTaxReceipt.setText(personalHead[0] + "\t" + personal[0] + "\n" + personalHead[1] + "\t" + personal[1] + "\n" + personalHead[2] + "\t" + personal[2] + "\n
printing records from jsp - JSP-Servlet
printing records from jsp  Hi Plz tell me how to printing out pages from jsps with page numbers Thank you  Hi friend, For paging in Jsp visit to : http://www.roseindia.net/jsp/paging.shtml http
pop up
pop up  how to create pop up in html
Printing server information
Printing server information  Hello, I am learning PHP language. I trying to fetch the server Information Print. How could i will printing the server... connected to your PC? This it best example for the Printing server Information
printing example - Java Beginners
printing example  Is it possible to print java controls using print method? My problem is to print a student mark list using java? The mark list should like that of university mark list
automorphic numbers
automorphic numbers  how to find automorphic number in java   Hi Friend, Pleas visit the following link:ADS_TO_REPLACE_1 Automorphic numbers Thanks
Java Printing - Java Beginners
Java Printing  Can anybody plz help in giving the code for printing the contents of JPanel.. It contains JLabels and JTextFields only.. Plz.. Its urgetn.. Printing means sending the document to the Printer..  Hi Friend
sorting numbers
sorting numbers  How to sort the numbers in ascending order   import java.util.*; class SortNumbers{ public static void main(String...=input.nextInt(); list.add(num); } System.out.println("Numbers
how printing landscape?
how printing landscape?  I have code which print some data from data base with "drawString()" and it works correct when I chose "Portrait" from "Print Dialog", but it does not work if chose "Landscape". Is any way to print
printing hollow square
printing hollow square  Write an application (PrintingSquares.java) that prompts the user to enter the size of the side of a square, then displays a hollow square of that size made of asterisks. Your program should work
numbers
extracting phone number n storing in excel
extracting phone number n storing in excel  i need a program to open a doc file n extract phone numbers from it and store it in a excel sheet?? plz do reply and help me out with the problem
extracting phone number n storing in excel
extracting phone number n storing in excel  i need a program to open a doc file n extract phone numbers from it and store it in a excel sheet?? plz do reply and help me out with the problem
printing press training course
printing press training course  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: printing... that I can learn the topic "printing press training course". Also tell
Printing ArrayList - JSP-Servlet
Printing ArrayList   I have a code in servlet like this ArrayList total = new ArrayList (); session.setAttribute("efg",total); RequestDispatcher rd; rd = request.getRequestDispatcher("/checkout.jsp"); rd.forward
ModuleNotFoundError: No module named 'Printing-For-Noobs'
ModuleNotFoundError: No module named 'Printing-For-Noobs'  Hi, My... named 'Printing-For-Noobs' How to remove the ModuleNotFoundError: No module named 'Printing-For-Noobs' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'colour-printing'
ModuleNotFoundError: No module named 'colour-printing'  Hi, My... named 'colour-printing' How to remove the ModuleNotFoundError: No module named 'colour-printing' error? Thanks   Hi, In your python
printing series in java
printing series in java  1 123 12345 1234567 12345 123 1   Here is a code for the following pattern: 1 123 12345 1234567 12345 123 1 class Pattern{ public static void main(String[] args
Numbers pyramid
Numbers pyramid  Hi sir, Can you please tell me how to output this using nested for loops? 1 2, 1 1, 2, 3 4, 3, 2, 1 1, 2, 3, 4, 5 6, 5, 4, 3, 2, 1 1, 2, 3, 4, 5, 6, 7 8, 7, 6, 5, 4, 3, 2, 1 1, 2, 3, 4, 5, 6, 7, 8, 9 10, 9, 8

Ads