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
ADVERTISEMENT
Sep 25, 2014
1. Write a Java program that randomly generates a five-digit lottery number and prompts the user to enter a five-digit number. Each digit in the number is in the range between 0~9. The program should determine which prize the user wins according to the following rule:
- The user wins the first prize if the user input matches all five digits in the lottery number in exact order.
-The user wins the second prize if the user input matches any four digits in the lottery number in exact positions.
-The user wins the third prize if the user input matches any three digits in the lottery number in its exact position.
-The user wins the fourth prize if the user input matches any two digits in the lottery number in its exact position.
- The user wins the fifth prize if the user input matches any one digit in the lottery number in its exact position.here is my code. I have tried replacing the && statements with || and it always returns either case 1 or case default.
import java.util.Scanner;
import java.util.Random;
class Hw5 {
static int getPrize(int g1, int g2, int g3, int g4, int g5,
int u1, int u2, int u3, int u4, int u5) {
[code]...
View Replies
View Related
Sep 25, 2014
Write a Java program that randomly generates a five-digit lottery number and prompts the user to enter a five-digit number. Each digit in the number is in the range between 0~9. The program should determine which prize the user wins according to the following rule:
• The user wins the first prize if the user input matches all five digits in the lottery number in exact order.
• The user wins the second prize if the user input matches any four digits in the lottery number in exact positions.
• The user wins the third prize if the user input matches any three digits in the lottery number in its exact position.
• The user wins the fourth prize if the user input matches any two digits in the lottery number in its exact position.
• The user wins the fifth prize if the user input matches any one digit in the lottery number in its exact position.
and here is my code. I cant get it to print the right statements.
import java.util.Scanner;
import java.util.Random;
class Hw5 {
static int getPrize(int g1, int g2, int g3, int g4, int g5,
int u1, int u2, int u3, int u4, int u5) {
//code for determining the prize comparing (g1, g2, g3, g4, g5) to (u1, u2, u3, u4, u5)
if (u1 == g1 && u2 == g2 && u3 == g3 && u4 == g4 && u5 == g5)
[code]....
View Replies
View Related
Feb 17, 2014
I'm trying to output a ten digit phone number as a string on one file, and write another program to write it as a long because I don't know what value I need to use in order to get the code to run properly on either one.
View Replies
View Related
Apr 21, 2015
I have to write a program that inputs a 5 digit integer from the keyboard and prints if the input number is a palindrome or not. I got it to work, but when my result prints it says "0 is a palindrome" or "0 is not a palindrome". How can I get rid of the 0 and replace it with the number input by the user?
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
int number;
int digit;
int temp;
[code]....
View Replies
View Related
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
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
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
Sep 16, 2014
So I am currently writing my first assignment and have run into problems with my coding. The task was to have someone enter a 5 digit number and in return, I list each number on their respective lines. We also must create an error if a number other than 5 digits was entered. My problem is that when they enter a 1 or 2,3,4,6,7,8 digit number.. the error message occurs along with the rest of the messages (listing the numbers, etc). I want the program to end (or even re-ask to enter the numbers) if they incorrectly enter the data.
View Replies
View Related
Nov 6, 2014
I have returned with yet another problem but I think this one might be a bit easier to fix.
So the whole MO of this program is to take user input, and display the sum of the digits of the number they entered. I am supposed to do this by utilizing methods. So far this is what I have and when I compile it tells me that it "cannot find symbol", which I don't understand as I define what "m" is in the for loop. The other error is that it says:
"Exercise6_2.java:22: error: incompatible types: possible lossy conversion from long to int
return result;
^
I don't understand why it's giving me this error nor do I understand why result seems to inherently be an int. (Also the public static int sumDigits(long n) method was given to me by the book so that is what I am supposed to use).
import java.util.Scanner;
public class Exercise6_2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a integer");
long n = input.nextLong();
[Code]...
View Replies
View Related
Sep 28, 2014
which method i can use so that the program checks if the input value is a digit or else outputs a message saying that `the value entered is not a number.
View Replies
View Related
Jan 23, 2014
i have tried permutation with big Integer in Java. it works fine upto 4 integer input say 3456 P 2345 but nothing happens in console when i type 5 digit input..here is my code
public class cvic {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter n and r: ");
BigInteger n = scan.nextBigInteger();
BigInteger r = scan.nextBigInteger();
System.out.println("nPr = "+fact(n).divide(fact(n.subtract(r))));
[code]....
View Replies
View Related
Nov 23, 2014
<p:inputMask mask="?999999999" placeHolder ="" >
But my requirement is inputMask should take no limit no. of digits(only).but it is taking no. of 9 which i have given in mask attribute..
And if i am not giving mask attribute and i am handling with keyFilter(....regx) but it is allowed to paste alphabetic(Char) also. so mask attribute is required.
View Replies
View Related
Apr 10, 2014
I am trying to figure out how to import a txt file that has a color and a four digit number. Everything that I have tried comes up with some kind of error.
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.InputMismatchException;
[Code] ....
I couldn't find an edit button. I need to store the String and int from the txt file into an array. The problem that I keep having is that the compiler seems to want it as either two ints or two Strings.
View Replies
View Related
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
Feb 8, 2014
I want to limit the program to only accept a 12 digit input and an integer value but I don't know how to program it,
import java.util.Scanner;
public class testing4
{
public static void main(String []args) {
Scanner reader = new Scanner(System.in);
String input;
[Code]...
View Replies
View Related
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
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
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
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
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
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
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
Oct 7, 2014
URL....So the problem is that when I type in "PA" it will display about 24 Zips and Populations before it stops. The problem is in the ZIPs file. It goes down the list and then takes the Zip from the Zips file to the Zips in the Population file and displays the Population. It will go to population 513 and stop. Reason being, there is no ZIP code in the Population file to display a population. The loop then stops. How can I get the program to skip over the zip code when there is no corresponding ZIP code in the other file and continue showing the other Pops..Here's what I currently have completed:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Population {
//Declaring global variables.
Scanner fileScannerZip, fileScannerPop, inputFile;
private String lineZip, linePop;
int invalidZip;
[code]...
View Replies
View Related
Feb 15, 2015
I want to write a java program which displays to get the GPA of a student as a keyboard input. Then display the class of the degree according to the following criteria using a switch-case statement (if-else statements cannot be used). If the user insert an invalid gpa you should display a message Invalid GPA.
gpa ≥ 3.50 First Class Hons
3.49 ≥ gpa ≥ 3.00 Upper Second Class Hons
2.99 ≥ gpa ≥ 2.50 Lower Second Class Hons
2.49 ≥ gpa ≥ 2.00 Pass
2.00 ≥ gpa Fail
Here is my code:
import java.io.*;
public class q7
{
public static void main(String[] args)
{
InputStreamReader ISR=new InputStreamReader(System.in);
BufferedReader BR=new BufferedReader(ISR);
[code]...
.But when I use Command console to run this it says: 1111.jpg. I also wanna know is there another way to do this with switch- case statements.
View Replies
View Related
Oct 12, 2014
I just started Java a few days ago and I'm having trouble with ActionListener..
Here are my codes so far , i'm having trouble with the
btnDetermineAction10686696_842067395825457_2521361553839739125_n.jpg:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NumberAnalyzer extends JFrame{
private JButton btnDetermine, btnClear;
private JTextField txtNumber, txtFactors, txtPrime;
private JLabel lblNumber, lblFactors, lblPrime;
private JPanel pnlNumber, pnlNumber1,
[Code] ....
The output should be the one in the picture:
Input is placed at the NUMBER text field.
Identify its factors, write it at FACTORS text field.
Determine if it is prime. If prime, output at PRIME text field is “Yes”, “No” otherwise.
View Replies
View Related