Printing Number Of Prime Numbers In A Range?

Feb 13, 2015

The assignment is to make a program that prints the number of prime numbers in a range. This is what i have so far. The output is a list of 2s. I created the for loop to cycle through the range of 17-53 and nested a while loop within to test each incident of the for loop to check for divisors starting with 2 until the modulus result is 0 resulting in a false for being a prime number. Then the loop should increment to the next i value. The last part is an if statement that i had intended to add counters to the k variable that would keep track of the number of prime numbers.

boolean isPrime = true;
int j = 2;
int k = 1;
for (int i = 17; i <= 53; i++){
{
while (i % j == 0){
isPrime = false;

[Code] .....

View Replies


ADVERTISEMENT

Long Type Output File - Print Prime Numbers Between Given Range Of Numbers

Aug 22, 2014

I tried to create file and write the output of my program in it in java when i use WriteLong then the file does not contain long value, how I create this file my program is to print prime numbers between 500000 to 10000000

public class primenumber {
public static void main(String[] args) {
long start = 5000000;
long end = 10000000;
System.out.println("List of prime numbers between " + start + " and " + end);
for (long i = start; i <= end; i++) {
if (isPrime(i)) {
System.out.println(i);

[Code] ....

View Replies View Related

Prime Number Generator - Range From 0 To 199

Feb 5, 2015

public class printprimes2{
 public static void main(String[] args){
  for( int i = 1 ; i <=199 ; i++ ) //iterate 1 - 199; 2 is prime {
for ( int j = 2 ; j < i ; j++ ) //iterate 2 - potential composite EXCLUSIVELY; every number can be divided by one and itself

[Code] ....

It doesn't print only prime numbers but all numbers that range from 0 to 199. What do you think I am doing wrong?

View Replies View Related

Unable To Find Prime Palindrome Numbers Less That A Number By Program

Oct 10, 2014

I want to find the prime palindrome numbers less that a given number by my program. Here is my code, I am trying to find the method to solve the errors when I compile it. It said variable a might not have been initialized in line 41,62,86.

import java.util.Scanner;
public class Lab5{
public static void main (String[] args){
System.out.println("Please input a number");
Scanner Input=new Scanner(System.in);
int num = Input.nextInt();

[Code]...

View Replies View Related

Give Number Of Numbers Dividable By Either 2 / 3 Or 5 In A Range

Oct 31, 2014

I found an exercise online to create a small program . I have this code that I have done so far:

import java.util.Scanner;
 public class Test {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
long a = sc.nextLong(); long b = sc.nextLong();
long count = 0; // counter
for (long c = a; c <= b; c++) {
if (c % 2 == 0 || c % 3 == 0 || c % 5 == 0) {
count++;
}
}
System.out.println(count);
}
}

This program is suppose to give me the number of numbers which are dividable by either 2,3 or 5 in a range of a to b, where a<=b.

FOR EXAMPLE: a=5 b=8 ... output: 14 (since there are 14 numbers in between 5 and 8 which are dividable by either 2,3 or 5.)

It works great for all of the numbers except higher ones such as a=123456789012345678 b=876543210987654321. Here it doesn't give me any output. From what I know it is because the code is still running. But there must be a quicker way ...something that can modify the code so it finishes in the matter of seconds not hours. Something that will fasten the process of checking if the numbers are dividable...

View Replies View Related

Program That Takes User Odd Number And Print Prime Numbers Lower Than User Input

Nov 4, 2014

I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.

public class PrimeNumber
{
Scanner scan = new Scanner(System.in);
int userNum;
String neg;

public void getUserNum()

[code]...

View Replies View Related

List All Prime Numbers Between Two Entered Numbers

Oct 17, 2014

Program is to list all prime numbers between two entered numbers.

import java.util.Scanner;
public class question6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter lower int:");
int x = input.nextInt();

[Code] .....

View Replies View Related

JSF :: Printing Only Part Of Prime-faces Page?

Jan 15, 2014

I have a print button as below

<p:commandButton id="printit" value="Print" type="button" icon="icn-print">
<p:printer target=":topForm:data" />
</p:commandButton>

when i click print button only top half of the page is getting printed and bottom half is pushed to next page. How do i fix this issue.

View Replies View Related

Generate Random Numbers Without Certain Numbers In Range

Jul 31, 2014

I tried out doing number (generated randomly) != (another number) but that does not work. If I for example want a number between 1 and 10, but I do not want the number 5, what can I do in order to make this happen?

View Replies View Related

Number Guessing Program - Computer Guesses A Number That User Is Thinking Of Within Certain Range

Mar 28, 2015

I have a beginning Java Program I have been working on that creates a number guessing program where the computer guesses a number that the user is thinking of within a certain range. I so far have the program below, but need getting rid of a few kinks/ adding features.

-First, I need to set it up so that the user will be prompted and give a range. It should run and produce something like this:

Welcome to this first-ever mind-guessing program!

Please pick a range (higher than 1 and no larger than 50): 32

You will choose a number between 1 and 32... and I will try to guess it.

With each of my guess, you will tell me whether I am too high (h or H), too low (l or L), match (m or M), or you want to quit (q or Q). My objective is to find the number using as few guesses as possible.

-Second, the game is supposed to give up and restart after failing the five, guesses, but for some reason, after it fails the fifth time, it prompts a fifth guess once again instead, then restarts after- I need to prevent this, so that it should look something like this:

My fourth guess is 17: h
My guess is too high?

My fifth guess is 16: h
*** I am unlucky this round. I give up.

Let's play!

My first guess is 10:
etc..

import java.util.*;
import java.lang.Math;
public class numguessprac1 {
// Declaring variables
public static String input;
public static int quit;
public static int guess;
public static int wins;

[Code] ....

View Replies View Related

Numbers In Certain Range?

Sep 7, 2014

program that calculates and prints the sum of all numbers between two limits as the user types. Like if the user types 1 and 10 on the upper limit, it prints the following text: "1+2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55".

View Replies View Related

Sieve Code To Find Prime Numbers

Jan 16, 2015

I am doing a sieve code to find prime numbers.

public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println(" Enter the maximum number ");
int maxNumber = scanner.nextInt();
int[]numberList = createList (maxNumber);

[code]....

I have a problem with the output. It only get as far as marking the multiples of 2. It does not mark the multiples of 3 and so on.Example output: If the range is from 2 to 15 the output is :2, 3, 0, 5, 0, 7, 0, 9, 0, 11, 0, 13, 0, 15 note: 0 means its marked.As you can see it only marks multiples of 2 but it does not mark multiples of 3 above. I think the problem lies in the for loop inside my main method, because the index does not increment.

View Replies View Related

Weed Out All Prime Numbers Without Using Any Kind Of Division?

Dec 15, 2014

The challenge is to weed out all the prime numbers without using any kind of division (%, /). My code doesn't weed out certain numbers, such as many multiples of 5, the number 49, etc, and I am not sure why. Here is my code.

My logic for the for loops was this: Starting with the upper numbers of the ArrayList, find every number that is a multiple of that number and remove it from the ArrayList. Every time you find a multiple, increase the variable multiply, so the program knows what the next multiple to look for.

// program doesn't work yet.
import java.util.ArrayList;
// import java.util.ListIterator;

public class Sieve2
{
public static void main(String[] args)
{
int upperLimit = 55;
ArrayList<Integer> primes = new ArrayList<Integer>();

[Code] .....

And this is the output:

1 2 3 5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53 55

View Replies View Related

Program To Pull Prime Numbers Between Two Entered Values

Oct 18, 2014

Program to pull prime numbers between two entered values,

import java.util.Scanner;
public class question6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter lower int:");

[Code] ....

View Replies View Related

List Prime Number From N To M

Oct 15, 2014

I am trying to list of prime number from n to m but my program give only one number

import java.io.PrintStream;
import java.util.Scanner;
public class Check05B
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
PrintStream output = System.out;
output.print("Enter a number to test: ");

[Code] ....

View Replies View Related

Prime Numbers - Find N From List That Is Not Crossed And Cross Out All Its Multiples

Jun 20, 2014

the prime numbers from 1 to 2500 can be obtained as follows. From a list of the numbers of 1 to 2500,cross out al multiples of 2 (but not 2 itself). Then, find the next number (n, say) that is not crossed out and cross out all multiples of n (but not including n).

Repeat this last step provided that n has not exceeded 50 (the square root of 2500). The numbers remaining in the list (except 1) are prime. Write a program which uses this method to print all primes from 1 to 2500. Store your output in a file called primes.out.

View Replies View Related

Program That Checks If Number Is Prime Or Not?

Feb 11, 2015

I am writing a program that checks if a number is prime or not. Code below.

for(int i = 2; i < P; i++){ 
if (P % i == 0) {
System.out.println(P + " Can also be divided: " + i);
return;
}
}
System.out.println("Prime number.");

It works but, if a number is not a prime. I need to print out all the numbers that it can be divided with. For example if a number would be 8: it can also be divided with 1, 2, 4, 8.

View Replies View Related

Loading Winning Saved File - All Numbers Have Unique Set Of Prime Factors

Nov 7, 2014

The game doesn't seem to be working but you can win by loading a winning saved game.

Hint: Remember that all numbers have a unique set of Prime Factors.

I have been struggling to solve this. The code is not in error. I am trying to load a winning file but unable to solve.

Here is the java code:

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
public class Main {
static final int GAME_SIZE = 40; //Disk sizes go from 0->39
 // Produce a list of the first N prime numbers

[Code] ....

Results:

C:UsersSal_2>java Main
Welcome to Towers of Toast!!!
Type 'new' to start a new random puzzle
Type 'load' to load a saved puzzle
new
[0, 2, 3, 4, 5, 6, 8, 10, 14, 19, 20, 25, 26, 28, 35, 38, 39]
[7, 9, 11, 13, 15, 21, 22, 23, 27, 31, 32]
[1, 12, 16, 17, 18, 24, 29, 30, 33, 34, 36, 37]
|
|
XX |
|
XXX |
|
XXXX |
|
XXXXX |
|
XXXXXX |
X
XXXXXXXX XXXXXXX
XXXXXXXXXXXX
XXXXXXXXXX XXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXX XXXXXXXXXXX
XXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The game is broken! We saved your game, though. Here are your save game numbers:

146209709250387100944095470
52067710192022655041
21882855117530023387473

C:UsersSal_2>java Main
Welcome to Towers of Toast!!!
Type 'new' to start a new random puzzle
Type 'load' to load a saved puzzle
load
Enter save number for pole 1:
3
Enter save number for pole 2:
1
Enter save number for pole 3:
11
Exception in thread "main" java.lang.RuntimeException: Not all disks accounted f
or
at Main.main(Main.java:100)

C:UsersSal_2>

View Replies View Related

Improving 10001st Prime Number Project?

Mar 7, 2014

I have been working on a Project Euler problem which is to find the 10001st prime number. I made the project in java and it gave me the correct answer (104743) . I notice that it had taken 17 seconds to find the answer. how to improve the efficiency of my Java program - at the moment it is bruteforce.

static boolean isPrime;
static int primeCount;
static int forI = 1;
static int latestPrime;
public static void main(String[] args){
long startTime = System.currentTimeMillis();

[code]....

View Replies View Related

Return Biggest Prime Factor Of Given Number

Sep 10, 2014

One of our assignments this week was to make a program that when given a number it will return the biggest prime factor of that number. So for example if the program is given the number 15, the output is 3.

144 gives you 3 also.
17 gives you 17
21 gives you 7.

Anyways, after some time trying out various things and combining stuff i googled with my textbook I somehow stumbled upon a code that works. But I cannot for the life of me understand how it works. I think it has something to do with the inner loop and the outer loop. But I feel like i cant go on without understanding how it works.

Here is the code:

public class BiggestFactor {
public static void main( String[] args ) {
int N;
N = StdIn.readInt();
int n = N;

[Code] .....

View Replies View Related

Determining User Input To Be Prime Number

Feb 12, 2015

Any better way to write a program that takes a user number input and the program determines whether or not the number is prime or not. It was suppose to be a number between 0 and 8,000,000.

import java.util.Scanner;
public class prime1
{
public static void main(String args [])

[Code].....

View Replies View Related

Number Shifting In A Range

Apr 23, 2014

Any way to shift in a range from 0-9 when I already have the shift value.

The reason I am asking this is because I am writing a telephone validation program and I got most of it complete and all I need to do now is the shift an encrypted phone number given to me by the user, and shift it however many times my shift value is.

Example: I am trying to get this phone number, 545-319-8712 to become 212-086-5489. The shift value is 3. So basically since the phone number given to me is 3 numbers higher than the phone number I am trying to get, so if the first number I receive from the user is higher than 2 then I would shift the number the user gave me down by the shift value I have already gotten.

5 shift down 3 = 2, 4 shift down 3 = 1, etc. But I also want to know how I can make a number like "1" to shift down 3 to become 8. This is the range; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

If I have to shift a number down 4 spots and I get the number 1 from the user than I want to get the number 1 to first down four times to become 7.

[1] -> 0 -> 9 -> 8 ->[7]

Basically if I have to shift a number down 4 and the number is less than or equal to 3 then I want it to continue from 9 .

Then just reverse the steps if I have to shift a number up, USER gives me "090", i want "212" I shift the number up by 2.

View Replies View Related

Java Programming To Input Number And Display All Its Prime Digit?

Jan 9, 2015

I am new to java programming and using bluej for programming and i have tried this question what i have have given in title ... how to find out the errors in the following program:-

class Primedig {
public void getinput(int n) {
int ctr=0;
while(n!=0) {
int r=n%10;
for(int i=1;i<=r;i++)

[code] .....

output:-

For example input is 243 the output comes only 3 not 2& 3 both.

View Replies View Related

Java Program - Find Next Highest Or Lowest Prime Number

May 2, 2014

Java program takes positive int from user and determine if prime. Next the program finds next highest or next lowest prime number.

import java.io.*;
import java.util.*;
public class pa2take2 {
public static void main (String[]args){
int menus; //menu selection

[Code] ......

View Replies View Related

Generating Double Number From The Range

Apr 18, 2014

I am trying to generate a double number from the range [-1,2]

so what i did is this

xs[i]= Math.random() *(2-(-1))+(-1);

I did that as a loop to generate multiple numbers and i got this result:

initial x values 2.17 2.66 3.04 2.81 1.83 2.66 3.67 2.81 1.04 3.1 3 1.23 1.44 3.5 3.84 3.03 1.7 2.79 4 1.43

see some numbers are out of range! and i don't know why aren't there any minus numbers...

View Replies View Related

How To Do Number Range In If Statement In Java

Jul 28, 2014

For example:

if(JTextField = 15-30){
do this
}

I know it's simple but i have no clue how it's done....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved