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


ADVERTISEMENT

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 View Related

How To Write Random Number Generator That Will Not Contain Any Repeat Value In A Range

Apr 24, 2015

I am trying to write a Random Number Generator that will not contain any repeat value in a given range

something this I have try so far

import java.util.Random;
/**
* This class is used to generate a
* Range of Random Number in a range from int a to int b
*/
public class RandomNumberGenerator {
public static void main(String[] args) {
int[] arr=randomNummbers(1, 20);
for(int j=0;j<arr.length;j++){

[Code]...

although this code generate random numbers but some values are also getting duplicate.So how to write a program for random number that will not repeat any integer in range?

View Replies View Related

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

How To Create Simple Random Number Generator For Number Between 1 -10

May 18, 2015

I am working on a little nothing project, but I wanted to create a random number generator for a silly game where the user guesses the number.I have used google, but they are using LOG statements, what it does.

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

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

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

Random Number Generator Then Summing

Aug 29, 2014

I am brand new to programming and java. I decided I wanted to learn programming so now I'm in a class. I am into my second week and my assignment is to generate 100, 3 digit numbers then add them together. I understand how vast java is, but as of right now we have only studied if/else/switch statements, I was able to get the 100, 3 digit numbers generated, but I cannot add then together.

import java.util.Random;
import java.util.Math;
public class ThreeDigitGenerator {
public static void main(String[] args) {
int sum = 0;

[Code] ....

View Replies View Related

Random Number Generator Error

Dec 1, 2014

I am new at coding and I can't seem to figure out why my random number generator (numGen) won't let me compile. this is the game class to a game i'm making called Bagels. its very similar to the game mastermind in the sense that you have to try to guess a three digit number and the computer will return feedback giving you clues that will guess the number.

import java.util.*;
import java.io.*; 
public class BaglesGame {
public static numGen() {
Random Rainbow = new Random ();
firstDigit = "" + Rainbow.nexInt(10);
 
[Code] .....

View Replies View Related

Random Phone Number Generator

May 13, 2011

I am trying to make a phone number generator. I get the following error though when trying to compile.

my post

C:JAVA_PROGRAMMING_CODEcode1RandomPhoneNum>javac RandomPhoneNum.java
RandomPhoneNum.java:23: cannot find symbol
symbol : method toOctalString(int)
location: class RandomPhoneNum
strippedNum = toOctalString(num1);
^
1 error

Here is the code so far

import java.util.Random;
public class RandomPhoneNum
{
public static void main(String[] args)
{
System.out.println("This app prints out a random phone number.");

[Code] ....

Also, how did you people know about octal base numbers? I would never have known that. What I can do to improve my math ability?

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

Random Number Generator And Local Variables

Jul 31, 2014

Basically this code is supposed to create an int array of 50 elements, then pull the elements listed in positions 1-6 to make a lottery draw.

Problem is it gets down to that last "For" statement and then says "duplicate local variable i." If I attempt to separate it from the previous for loop with curly braces, then it gets mad and says it doesn't know what "local variable i" even IS. So I tried changing it to "j" in the last statement and it couldn't recognise that either. I feel like I need to change the variable name in that second for loop but I'm not sure how to make it understand that the second variable is going to be outputting the values from the first variable.

public class Lottery {
public static void main(String[] args) {
// TODO Auto-generated method stub
int []nums = new int [50];
for (int i = 1; i <50; i ++) {nums[i] = i;}

[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

Random Number Generator To Pick Index From Array?

Oct 31, 2014

Im creating my 2nd project ever and its a hangman game. I created an Array that has 20 different words in it and need to create a function that generates a random number to pick a random index of the array to obviously pick a random word. I cannot figure out the syntax for the life of me.

View Replies View Related

Random Number Generator To Pick Index From Array

Oct 31, 2014

I am creating my 2nd project ever and its a hangman game. I created an Array that has 20 different words in it and need to create a function that generates a random number to pick a random index of the array to obviously pick a random word.

View Replies View Related

Comparing Array Or Int To 4 Digit Number From Random Generator?

Apr 4, 2015

How do I compare an array (or maybe int?) (example: 3572) to a 4digit number from random generator? how many of the four digit match the generated numbers.

View Replies View Related

Quiz Game Program - Random Number Generator

Nov 18, 2014

I've run into some problems with my Quiz-game program. What I basicly want it to do is generate a random number that is tied to the other windows I have for the program (I want a new random question to be generated once I have guessed on the first one). I've read about a few different ways to approach this but everything I get is a pure number generator.

I imported

import java.util.Random;

into my program but I don't know where to go from there.

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

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

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

Random Phone Number Generator - Include Dashes In The Output

Apr 11, 2011

I've been working on a personal project again in Unit 3 of my text book and this time they want me to make a random phone number generator. Here's the actual directions:

"Write an application that creates and prints a random phone number of the form XXX-XXX-XXXX.

Include the dashes in the output.

Do not let the first three digits contain an 8 or 9 (but don't be more restrictive than that), and make sure that the second set of three digits is not greater than 742.

Think through the easiest way to construct the phone number. Each digit does not have to be determined separately."

With this assignment I came across 2 problems:

1. What I tried was that since the first 3 numbers have to be a random number with no 8's or 9's I tried doing this algorithm:

int first0, first1, first2, second, third, random0;
first0 = generator.nextInt (999);
random0 = generator.nextInt (8);
first1 = first0.replace('8', random0);
first2 = first1.replace('9' random0);

The problem is that Java won't allow me to generate random numbers this way. Are there any easier ways of generating numbers that doesn't contain specific number (such as 8 or 9 in this case?)

2. I was thinking about this throughout the whole project and what if I generate a number such as 27 (less than 3 or 4 digit number) rather than 3 or 4 digit numbers? How would it show 0027 or 027 instead of 27 for the phone numbers?

import java.util.Random;
public class pp0303 {
public static void main (String[] args){
Random generator = new Random();
int first0, first1, first2, second, third, random0;

[Code] ....

RESULTS:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot invoke replace(char, int) on the primitive type int
Cannot invoke replace(char) on the primitive type int
Syntax error on token "random0", delete this token
at pp0303.main(pp0303.java:15)

View Replies View Related







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