Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Printing numbers up to N into a file
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


.
Printing numbers up to N into a file
1 Answer(s)      4 years and 2 months ago
Posted in : Java Beginners


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?


Advertisement
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
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... have used print() method to store the even numbers to the file. println
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>
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
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
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
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
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
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
Java program for printing document
Java program for printing document  can u send me the java program for printing the office document? email: ibrahimbest@gmail.com
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 java series
printing java series  * *** ***** ******** ***** *** *   Post the format properly
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
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
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
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 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
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
 

 

 

DMCA.com