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



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

Have Programming Question? Ask it here!
 
 
 


.
Printing numbers up to N into a file
1 Answer(s)      3 years and 8 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
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
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
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 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
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
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
printing java series
printing java series  * *** ***** ******** ***** *** *   Post the format properly
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
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
Numbers
Java NotesNumbers Two kinds of numbers. There are basically two kinds of numbers in Java and most other programming languages: binary integers (most commonly using the type int) and binary floating-point numbers (most commonly using
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
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
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
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
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
how to find inverse of n*n 2d array in java
how to find inverse of n*n 2d array in java  I reached upto code of printing the matrix as: Assume that matrix is square matrix where row=column code is: public class ReadContents{ public static void main(String args
prime numbers - Java Beginners
prime numbers  Write a java program to find the prime numbers between n and m
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
determinant of n*n matrix using java code
determinant of n*n matrix using java code  Here is my code... { double A[][]; double m[][]; int N; public input() { Scanner s=new Scanner(System.in); System.out.println("enter dimension of matrix"); N
pop up
pop up  how to create pop up in html
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
finding the eigenvalues and eigenvector of n*n matrix in java
finding the eigenvalues and eigenvector of n*n matrix in java  Here...[][] {{4,3,1,2},{1,1,1,1},{1,2,1,1},{1,1,1,1} }; double t, c, s; int p, q, icount..., rowOfElMax, maxElRow); q = rowOfElMax[0]; p = colRowOfElMax
n raised to power p
n raised to power p  wap to input 2 integers n and p.find and print n raised to the power p without using Math.pow() function
automorphic numbers
automorphic numbers  how to find automorphic number in java   Hi Friend, Pleas visit the following link: Automorphic numbers Thanks
Error n Exception
Error n Exception  Difference between errors and exceptions
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
printing the following output using for loop
printing the following output using for loop  1 2 3 3 4 5 4 5 6 7
printing the following output using for loop
printing the following output using for loop  1 2 3 3 4 5 4 5 6 7
Advertisements
 

 

 

DMCA.com