Perfect Number Program

Oct 28, 2014

import java.util.Scanner;
public class pdonahue_Perfect
{
public static void main (String args[])
{
int test;
int value=0;
Scanner input = new Scanner(System.in);

[code]....

i have just a couple of issues when running my program.
-First, When i enter 0 it should print..

Please enter a possible perfect number: 0
0:NOT PERFECT

-Second, when i get a perfect number. it should show the answer in descending order..

Please enter a possible perfect number: 6
6:3 2 1

mine shows: Please enter a possible perfect number: 6 6: 1 2 3

**everything else compiles correctly with no errors

View Replies


ADVERTISEMENT

Algebra - Perfect Square Java Program / Find Smaller Integer N

Sep 22, 2014

Write a program that prompts the user to enter an integer m and find the smallest integer n such that m * n is a perfect square. (Hint: Store all smallest factors of m into an array list. n is the product of the factors that appear an odd number of times in the array list. For example, consider m = 90, store the factors 2, 3, 3, 5 in an array list. 2 and 5 appear an odd number of time in the array list. So, n is 10.)

so far my program is just like this.

import java.lang.Math;
import java.util.Scanner;
public class PerfectSquare {
public static void main(String[] args) {
Scanner m = new Scanner(System.in);
int Fint;

[Code] .....

how do i make the program find the smallest integer n?

View Replies View Related

Checking For Perfect Numbers

Feb 10, 2015

I'm trying to write code for a program that checks numbers upto a number that the user gives. I've looked around, and the nearest matches I can see are people using for loops with predetermined amounts of looping.The only real tools I'm allowed to use are basic arithmetic, and while loops. The program is supposed to have 2 nested while loops.

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

[code]...

View Replies View Related

Off-by-one Error In Perfect Shuffle Method

Jan 29, 2015

I am supposed to create a method that simulates a perfect shuffle of a given array of integers. According to the assignmment, this is what a perfect shuffle is:

This procedure is called a perfect shuffle if the interleaving alternates between the two half-decks.Unfortunately, the perfect shuffle comes nowhere near generating all possible deck permutations.In fact, eight shuffles of a 52-card deck return the deck to its original state!Consider the following perfect shuffle algorithm that starts with an array named cards that contains 52 cards and creates an array named shuffled.

Initialize shuffled to contain 52 empty elements.
Set k to 0.
For j = 0 to 25,
− Copy cards[j] to shuffled[k];
− Set k to k+2.
Set k to 1.
For j = 26 to 51,
− Copy cards[j] to shuffled[k];
− Set k to k+2.

This approach moves the first half of cards to the even index positions of shuffled, and it moves the second half of cards to the odd index positions of shuffled.The above algorithm shuffles 52 cards. If an odd number of cards is shuffled, the array shuffled has one more even-indexed position than odd-indexed positions. Therefore, the first loop must copy one more card than the second loop does. This requires rounding up when calculating the index of the middle of the deck. In other words, in the first loop j must go up to (cards.length + 1) / 2, exclusive, and in the second loop j most begin at (cards.length + 1) / 2.

View Replies View Related

Determine All Of Perfect Squares Between 1 And 100 - Precision Error?

Mar 26, 2015

I am teaching myself Java and am trying to write a function that will determine all of the perfect squares between 1 and 100 but am running into a problem...

Here's my code:

package sqrroot;

public class SqrRoot {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double sroot, rerr;
int count = 0;
for(double num = 1.0; num <= 100.0; num++){

[Code] ....

and here is the output:

run:
0.0
1.0 is a perfect square.
0.0
4.0 is a perfect square.

[Code] ....

There are 49 perfect squares between 1 and 100.
BUILD SUCCESSFUL (total time: 6 seconds)

Which is clearly wrong. Is there something wrong with my code or is this due to inherent imprecision in the double type or the Math.sqrt function?

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

Program That Takes A User Inputted Number And Converts It To A Binary Number

May 5, 2014

trying to write a program that takes a user inputted number and converts it to a binary number.

Here's what I have:

package com.java2novice.algos;
import java.util.Scanner;
public class Converter {
static Scanner console = new Scanner(System.in);
public void printBinaryFormat(int number){
int binary = console.nextInt();

[Code]...

Bugs on:

Line 13
Line 17
Line 23

View Replies View Related

Program Is To Accept Number And Display New Number After Removing All Zeros

Dec 29, 2014

ex
Sample input:2340980
Sample Output:23498

this is the program i have tried but is dosent work

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

[Code]....

View Replies View Related

Create Little Program Which Enter Number / Program Says Triangle Exist Or Not

Mar 16, 2014

i want create little program which enter the number, ant program says triangle exist or not. So code :

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

[code]...

So when i put first num like 1, next num like 2 and next num like 100 , program says Triangle exist.

View Replies View Related

How To Stop Program When Entering Certain Number

Dec 2, 2014

how many integers the user wants to use. The user will enter for example a 4. The user inputs a 2, 4, 6, and 10. So then we get our outputs...Then the code will ask if you want to run this program again. My question is, if the user inputs a -1 for example 2, 4, 6, -1....the code will not continue. I wanted to use a while loop, such as while (scores != -1) but it doesn't work.

Enter the amount of integers you want to use4
Intenger # 1 2
Intenger # 2 4
Intenger # 3 6
Intenger # 4 10
You entered 2.0 4.0 6.0 10.0
Average = 5.5
Variance = 8.75
Standard Deviation = 2.96
Do you have another set of numbers?

[code]....

View Replies View Related

GUI Program - Convert Input Name To Number

Mar 12, 2015

A GUI program that allows user to enter their name and the program will convert it in number using array.

For example if i input "EUNISE" the output should be 521149195

Because the letter equivalent of

a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
g = 7
h = 8
i = 9
j = 10
k = 11
l = 12
m = 13
n= 14
o = 15
p = 16
q = 17
r= 18
s = 19
t = 20
u = 21
v = 22
w = 23
x = 24
y = 25
z = 26

View Replies View Related

Program To Detect Number 7 And Its Multiples

Apr 25, 2015

I just started learning Java, I was asked to write a simple program that prints a message if it encounters the number 7 or its multiple.

This should be achievable using simple loops and simple operations...

Here is my attempt:

Java Code:

Scanner scan = new Scanner(System.in);
int i, j, temp;
i = scan.nextInt(); // Awaiting user input
j = scan.nextInt(); // Awaiting user input
if (i > j){ // i = 10 j = 0
temp = i; // temp = 10

[Code]...

Now it works fine for numbers that are less than 70...

Example for output with i = 15, and j = 25:

Java Code:

15
16
!!!
18
19
20
!!!
22
23
24
25 mh_sh_highlight_all('java');

I am not sure what to do in case if for example 'i' and 'j' are very big numbers

I mean, I need it to detect the 7s even if it in the thousands place, actually - no matter how big is the number... So far I only made it to work for numbers that are less than 70...

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

Program That Calculates Fibonacci Number

Nov 19, 2014

Its a program that calculates Fibonacci number.This program uses recursion.

import java.util.Scanner;
public class FibonacciMemoization{ 
static int[] fib = new int[60];
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a Number :");
int number = input.nextInt();

[code]...

View Replies View Related

How To Have User Import A Number Into Program

Nov 14, 2014

So im trying to make a coin flip game in java. Im relatively new to the language and the only other language i knew was javascript so im still learning. Ive already made one before using just one class and putting all the code inside, but im now trying to do it with methods since im trying to learn them. The basic idea of the game is that the user picks how many coins they'd like to flip, they pick heads or tails, then the computer flips the coins and calculates if there was more heads or tails to tell the user if they won or not. Im not quite complete with the program as you can see but ive already run itno an error. I was just doing some tests on the code i already have and i found that when i call the method settingUpCoin in my main class the program terminates. So basically, when i run it, it executes userImp right, transform right, but then it dosent let you enter a value for howManyCoins and terminates before you get to settingUpCoin.

(Notes: this was all done in eclipse luna build or the java ide. The class files are all separate in the actual thing i just put them together here to demonstrate my code.

import java.io.IOException;
public class Coin
{
double myCoin;
int numOfCoins;
int counter;
double arrayOfCoins[] = { };

[code]....

View Replies View Related

Creating Number To Word Program?

Apr 1, 2015

I was given the assignment of creating a number to word program for my first college java programming homework.

Here is what I have created so far :

import java.util.*;
public class PrintNumberInWord { // saved as "PrintNumberInWord.java"
public static void main(String[] args) {
int number = 5;
Scanner sc = new Scanner (System.in); {
System.out.println ("Enter a number");
System.out.println(" ");
if (number == 1) {

[Code] .....

The first lines were made for us so we could follow a guideline, however, no matter what I type the command prompt displays 5 to me, I know thats because 5 is defined in the beginning but backspacing 5 causes the program not to work at all, how can I get this program to work properly?

View Replies View Related

Program To Add Space Between Input Number

Nov 2, 2014

I am writing a program where whatever number I input in my scanner, it will space them...

For example: If I input 456, the output should be 4 5 6... here is my code:

import java.util.Scanner;
public class midterm {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String number = "";

[Code] ....

the current output for this program is:
456 456 456

View Replies View Related

Average Program - Number Format

May 5, 2015

I am writing a program using a given test drive... I am supposed to write two classes according to the test drive. The calculated correct answer should be 115.50 but i keep getting 115.52 instead and i just can't figure out what i'm doing wrong.

Here is the test drive code:

import java.text.NumberFormat;
public class FinalTester
{
public static void main(String[] args)
{
double avgPurchase;
Order[] lastYear = new Order[4];

[Code] ....

View Replies View Related

Java Program With Random Number Guessing?

Jun 16, 2014

Write I program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number.

public class NumberGuess
{
public static void main(String[] args) {
// Create a Random object.
final static Random rand = new Random();
// max number is the upward bound number
final int MAXNUMBER = 10;
/**
* Method should return a random number within the upward bound MAX_NUMBER.

[code]....

I get compiler error message

NumberGuess.java:25: error: ';' expected
int getRandomNumber ();
^
NumberGuess.java:63: error: reached end of file while parsing
System.out.println("You got it right in " + numberOfGuesses + " guesses.");
^

How do I fix this?

View Replies View Related

How To Make The Program Understand 10 Number System

Oct 24, 2014

I want to create a program where i can output a number. ex. 3452 And the program will output 3000: three thousand 5: five 53: fifty-three

How can i make the program understand the 10-number system?

View Replies View Related

Writing A Program That Determines If Number Is Palindrome Or Not

Apr 3, 2015

I'm in the process of writing a program that determines if a number is a Palindrome or not. I'm not allowed to use strings and am required to use the getSize method shown below. I believe my issue that I'm having is that the "num" is not being called in the getSize method, and therefore not running the while loop, or at least that is what I believe is the issue.

import javax.swing.*;
public class getSize{
public static void main( String[] args )
{
int num;
num = Integer.parseInt (JOptionPane.showInputDialog ( "Please input a number" ));

[code]....

View Replies View Related

Program Will Print Out Largest Number And Exit

Oct 1, 2014

Description of the program: read a sequence of positive integers from terminal. When user types

-1, the program will print out the largest number and exit.

Program mid.java
import java.util.Scanner;
public class test
{
public static int num;
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
num = in.nextInt();
int large = num;
while(num > 0);
{
if(0 > num && num > large)
large = num;
}
System.out.println("The largest number is : " + large);
}
}

View Replies View Related

Lottery Game - Number Guessing Program

Sep 6, 2014

I wrote the following code for a lottery game. The problem comes when I run it. If one number of a 3 digit number matches, it's supposed to print "you win 1000" But regardless if a number matches or not, it says "sorry, no matches" ...

import java.util.Scanner;
public class Lottery {
public static void main(String[] args){
//generate lottery number
int lottery = (int)(Math.random() * 1000);
//Prompt the user to enter numbers
Scanner input = new Scanner (System.in);
System.out.print("Enter your numbers (3 digits): ");
int guess = input.nextInt();

[Code] .....

View Replies View Related

Create A Program That Allows User To Enter A Day Using A Number

Oct 13, 2014

so I had to create a program that allows the user to enter a day using a number and then enter a year and after they did that it would create an entire calendar for that year ..so I have that but the only issue is I can not get the numbers to line up neatly.how to do the entire thing in loops, I tried a couple in here..what this would look like as loops instead of switches and cases and if else

import java.util.Scanner;
public class Calendar {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
 
[code]....

View Replies View Related

Program Should Output Depending On Input Number

Apr 15, 2014

The program should has the output depending the number I'll input. (The number should be from 1-9) . This is the program's output should be:

Input number: 4

Output:
# # # 1 # # # # #
# # # 2 # # # # #
# # # 3 # # # # #
1 2 3 4 5 6 7 8 9
# # # 5 # # # # #
# # # 6 # # # # #
# # # 7 # # # # #
# # # 8 # # # # #
# # # 9 # # # # #

(Example 2):
Input numbers: 8

Output:
# # # # # # # 1 #
# # # # # # # 2 #
# # # # # # # 3 #
# # # # # # # 4 #
# # # # # # # 5 #
# # # # # # # 6 #
# # # # # # # 7 #
1 2 3 4 5 6 7 8 9
# # # # # # # 9 #

(Example 3):
Input number: 5

Output:
# # # # 1 # # # #
# # # # 2 # # # #
# # # # 3 # # # #
# # # # 4 # # # #
1 2 3 4 5 6 7 8 9
# # # # 6 # # # #
# # # # 7 # # # #
# # # # 8 # # # #
# # # # 9 # # # #

I'm not asking the code to make this program, I have trouble to understand the algorithm to make this program ...

View Replies View Related

Write Program To Sort Names According To Number Of Vowels?

Feb 15, 2014

write a program to sort names according to number of vowels sort({sam,ratan,alok,raj}) the op is {sam,ratan,alok,raj}

View Replies View Related







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