Generate prime numbers and stores them in a binary file

Generate prime numbers and stores them in a binary file

Write a program that generates N (given by user) prime numbers and stores them in a binary file â??primes.datâ??. Write another java program that reads the data from the file â??primes.datâ?? and displays the smallest number, the largest number, and the average. Write another java program that copies the contents of the file â??primes.datâ?? into a text file called â??primes.txtâ??. Open the file â??primes.txtâ?? using any text editor and make sure the contents of â??primes.datâ?? have been copied correctly

View Answers

May 9, 2012 at 3:58 PM

The given code generate the N number of prime numbers where N is given by the user. The generated prime numbers will then store into primes.dat file. Then read the data of the file and find out the largest, smallest number and average of prime numbers. The file is then copied to another file named 'primes.txt'.

import java.io.*;
 import java.util.*;
class PrimeNumber{
    private static void copyfile(String srFile, String dtFile){
    try{
    File f1 = new File(srFile);
    File f2 = new File(dtFile);
    InputStream in = new FileInputStream(f1);
    OutputStream out = new FileOutputStream(f2);

      byte[] buf = new byte[1024];
      int len;
      while ((len = in.read(buf)) > 0){
      out.write(buf, 0, len);
      }
      in.close();
      out.close();
      System.out.println("File copied.");
      }
      catch(Exception e){}
    }

    public static void main(String a[]) {
        try{
        BufferedWriter bw=new BufferedWriter(new FileWriter(new File("c:/primes.dat"),true));
        int c=0;
        Scanner input=new Scanner(System.in);
        System.out.print("Enter N:");
        int N=input.nextInt();
        int num=4;
        int count=0;
        System.out.println("First "+N+" Prime Numbers are: ");
        while(count<N){
        num++;
        if(num%2!=0){
        int i=3;
        while((num%i!=0)&&(i<=((num-1)/2)))     {
        i++;
        }
        if(num%i!=0){
        count++;
        bw.write(Integer.toString(num));
        bw.newLine();
         }
       }
     }
        bw.close();
        Scanner inp = new Scanner(new File("c:/primes.dat"));
        while(inp.hasNextLine()) {
          String line = inp.nextLine();
          c++;
        }
        int arr[]=new int[c];
     BufferedReader br=new BufferedReader(new FileReader("c:/primes.dat"));
     String str="";
     int k=0,sum=0;
     while((str=br.readLine())!=null){
       arr[k]=Integer.parseInt(str);
       sum+=arr[k];
       k++;
     }
     int smallest = arr[0];
        int largest = arr[0];
          for(int i=1;i < arr.length;i++){
            if(arr[i] > largest){
              largest = arr[i];
            }
          }
          for(int i=1;i<arr.length;i++){  
            if(arr[i] < smallest){  
              smallest = arr[i];  
            }  
          }  
        System.out.println("Largest number is: " + largest);
        System.out.println("Smallest number is: " + smallest);
        System.out.println("Average: " + (sum/count));
        copyfile("c:/primes.dat","c:/primes.txt");
        }
        catch(Exception e){
            System.out.println(e);
   }
    }
 }









Related Tutorials/Questions & Answers:
Prime Numbers
Prime Numbers  Create a complete Java program that allows the user 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
prime numbers - Java Beginners
prime numbers  Write a java program to find the prime numbers between n and m
Advertisements
finding the prime numbers
finding the prime numbers  Hi, I am a beginner to java and I have problem with the code in finding the prime numbers, can someone tell me about... number is prime or not, follow the link and it will provide you in depth knowledge
Prime Numbers - IDE Questions
Prime Numbers  Create a program that calculates the prime numbers from any inputted start and end range of values (e.g. 5 and 28) and print the prime... = 0; System.out.println("Prime Numbers are:" ); for (int i = 1; i <
prime numbers application
prime numbers application  how to add a comment indicating that the method will determine if a number is a prime number. In the next line add... a comment stating that 1 is not a valid prime number, on the next line add
Palindromic Prime Numbers
Palindromic Prime Numbers  Write a program that finds the first palindromic prime number above 100,000. Include the value of this prime in a comment at the start of the code
ModuleNotFoundError: No module named 'Prime_Numbers'
ModuleNotFoundError: No module named 'Prime_Numbers'  Hi, My... 'Prime_Numbers' How to remove the ModuleNotFoundError: No module named 'Prime_Numbers' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'Prime_Numbers'
ModuleNotFoundError: No module named 'Prime_Numbers'  Hi, My... 'Prime_Numbers' How to remove the ModuleNotFoundError: No module named 'Prime_Numbers' error? Thanks   Hi, In your python
Prime Numbers from range
Prime Numbers from range  how to find prime numbers in a given range which are palindromes??   Hi Friend, Try the following code: import java.util.*; public class PrimeAndPalindrome { static final int MAXNUMBER
Generate Random Numbers
Generate Random Numbers  hi,,, Please give me answer with example How do you generate random numbers within a given limit with actionscript... This function generate random numbers with in given limit
generate file
generate file  How to generate file(.doc or .xls or .txt ) with containing data in it.By clicking on a link
ModuleNotFoundError: No module named 'Factors-and-Prime-Numbers'
ModuleNotFoundError: No module named 'Factors-and-Prime-Numbers'  Hi...: No module named 'Factors-and-Prime-Numbers' How to remove the ModuleNotFoundError: No module named 'Factors-and-Prime-Numbers' error? Thanks  
Generate array of random numbers
Generate array of random numbers You can generate a random number either...() but it is better to use Random class. If you want to generate a series of random numbers.... In this section, we are going to accept 10 numbers and display 5 numbers from them
Finding all palindrome prime numbers - Java Beginners
Finding all palindrome prime numbers  How do i write a program to Find all palindrome prime numbers between two integers supplied as input (start and end points are excluded
Prime numbers in Java between 1 and 100
Prime numbers in Java between 1 and 100  Hi, How to display prime numbers in java between 1 and 100? Thanks   Hi, Prime numbers... which prints prime numbers between 1 and 100. Here is complete for printing prime
How to generate a list of random numbers?
How to generate a list of random numbers?  Hi, How to generate a list of random numbers? I want code in scala programming language. Thanks   HI, You can use the code: Seq.fill(10)(Random.nextInt) This is example
How to generate a list of random numbers?
How to generate a list of random numbers?  Hi, How to generate a list of random numbers? I want code in scala programming language. Thanks   HI, You can use the code: Seq.fill(10)(Random.nextInt) This is example
How to generate a list of random numbers?
How to generate a list of random numbers?  Hi, How to generate a list of random numbers? I want code in scala programming language. Thanks   HI, You can use the code: Seq.fill(10)(Random.nextInt) This is example
generate random numbers and display the largest
generate random numbers and display the largest  Hi, I am using netbeans so if someone could help me with an answer in that form i'd appreciate it. Write a method to find the largest value in an array. Write a program that takes
Pick Prime Numbers from the ArrayList
Pick Prime Numbers from the ArrayList Programmers mostly used ArrayList... and non prime numbers separately. Here is the code: import java.util....(array); System.out.println("Prime Numbers are: "); for (int i = 0; i <
Generate random numbers from 1 to 100
Generate random numbers from 1 to 100  1)A class Called: RandomNumberGenerator that generate random numbers from 1 to 100 2)A class Test that tests... an object from the Random NumberGenerator class to generate your input test data
Java read binary file
Java read binary file  I want Java read binary file example code... at Reading binary file into byte array in Java. Thanks   Hi, There is many more examples at Java File - Learn how to handle files in Java with Examples
Generate array of random numbers without repetition
Generate array of random numbers without repetition In the previous section, you have learnt how to generate an array of random numbers from the given array... created an application that will generate the array of random numbers without
Generate random number between two numbers in Scala
Generate random number between two numbers in Scala  Hi, How to Generate random number between two numbers in Scala? Thanks   Hi, Following code can be used for generating random number in scala: val rand = new
Generate random number between two numbers in Scala
Generate random number between two numbers in Scala  Hi, How to Generate random number between two numbers in Scala? Thanks   Hi, Following code can be used for generating random number in scala: val rand = new
java binary file io example
java binary file io example  java binary file io example
Find Numbers which are palindrome and prime
Find Numbers which are palindrome and prime In this section, you will learn how to find the numbers from 1 to 500 which are palindrome and prime. To compute...() of boolean type. Now the list contains all the numbers which are palindrome
Java find prime numbers without using break statement
Java find prime numbers without using break statement In this tutorial, you will learn how to find the prime numbers without using break statement. You all are aware of Prime Numbers, these are the numbers which are either divided
parse a file into primitive binary format
parse a file into primitive binary format  Hi, I need help converting an audio file or any other file to its primitive binarty format, and when I read the inputstream I find it some integers and I don't know its binary
Java Write To File Binary
Java Write To File Binary In this tutorial you will learn how to write to binary file. A binary file is a file into which the bit patterns of mostly data types can be formed to a byte of 8 bits. Write to a binary file in java
binary search tree from text file
binary search tree from text file  How so I go about constructing a binary search tree from a text file, which has letters and numbers, which must be sorted and printed in ascending order. E.g. Text file contents 3 apples pears
Generate pdf file - JSP-Servlet
Generate pdf file   Hi Friends, How to generate the pdf file for the jsp page or in servets  Hi Friend, You need to download itext api. After that set the classpath and try the following code
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
ModuleNotFoundError: No module named 'binary-file-search'
ModuleNotFoundError: No module named 'binary-file-search'  Hi, My... named 'binary-file-search' How to remove the ModuleNotFoundError: No module named 'binary-file-search' error? Thanks   Hi, In your
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
how to generate pdf file in struts
how to generate pdf file in struts  I am developing a struts application.I am having one registration form when i am submitting the form the values are stored in database,the database name is registration. In another form i am
how to generate pdf file in struts
how to generate pdf file in struts  I am developing a struts application.I am having one registration form when i am submitting the form the values are stored in database,the database name is registration. In another form i am
How to write in Binary file in Java programming language
How to write in Binary file in Java programming language  I need some help about Java programming. Please explain me "How to write in Binary file in Java programming language
Java File Binary
Java File Binary In this section, you will learn how to write numeric data into the binary file. Description of code: Numeric data converts compactly and faster in a binary format than the text. In the given example, at first, we have
Generate random numbers in Java
Generate random numbers in Java - How to use the java.util.Random class... in Java which is used by developers to generate pseudo-random numbers in the Java... generate random numbers efficiently. But if you are using this class
How to generate build.xml file
How to generate build.xml file       This example shows how to generate the build.xml file. You may say that build.xml file is the backbone of ANT (Another Neat Tool
ModuleNotFoundError: No module named 'generate_nsis_file_list'
ModuleNotFoundError: No module named 'generate_nsis_file_list'  Hi...: No module named 'generate_nsis_file_list' How to remove the ModuleNotFoundError: No module named 'generate_nsis_file_list' error? Thanks   
Java Binary data file - Java Beginners
Java Binary data file  Hi, I have a binary data file(binfile.data) and the file has what is commonly referred to as variable length records.Specifically the format of the file is: HEADER SECTION//RECORD
Writing to and reading from a binary file in java.
Writing to and reading from a binary file in java.  I have written the following code to convert an ASCII text file to a binary file: public static... the binary file from another program as follows: m_dis = new DataInputStream
Generate RDF file in Java
Generate RDF file in Java       In this example we are going to generate our first RDF( Resource Description File). This example generates a  RDF file with the use
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
Reading binary file into byte array in Java
Example code of reading binary file into byte array in Java This example shows you how to read a binary file into byte array from Java program. This type... and use the byte array data for further processing. To the binary file into byte
Prime number program in java
a program to generate and check  prime number  in java. As we know prime...,stc; String c; do { System.out.println("Press 1 for generate prime...; number<=limit; number++){ //print prime numbers only
How to generate xml file using xpath
How to generate xml file using xpath  Hi, I have a requirement in whixh I have x path of all the nodes with its corresponding values and I need to make a xml using that,. It would be great is you can help me out with how
binary
binary  Hi I want to write a program in pascal that ask a user to input a decimal number and then return its binary equivalent in the minimum number of bits required to repesent the number. Thks

Ads