Perfect Number Method w/ Range - java

I can't get the "its not a perfect number" to display. I need help with last part. It doesn't allow me to change it to else.
package editmess;
import java.util.Scanner;
public class Editmess {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("\nPerfect Number Finder Program");
System.out.print("\nEnter the start value: ");
int starval = scanner.nextInt();
System.out.print("Enter the end value:");
int endval = scanner.nextInt();
for (int n1 = starval; n1 < endval; n1++) {
int sum = 0;
for (int n2 = 1; n2 < n1; n2++) {
if (n1 % n2 == 0) {
sum = sum + n2;
}
}
if (sum == n1) {
System.out.println(n1 + " is a perfect number");
if (sum != n1) {
System.out.println("There is no perfect number between " + starval + " and " + endval);
break;
}
}
}
}
}

You can't put an else statement inside and if statement unless there is another if statement there.
Here is the whole code. Also you weren't reading in all the numbers entered. The first for loop should be n1 <= endval; and not n1 < endval; so that it also checks the enval entered.
Scanner scanner = new Scanner(System.in);
int counter = 0;
System.out.println("\nPerfect Number Finder Program");
System.out.print("\nEnter the start value: ");
int starval = scanner.nextInt();
System.out.print("Enter the end value:");
int endval = scanner.nextInt();
for (int n1 = starval; n1 <= endval; n1++) {
int sum = 0;
for (int n2 = 1; n2 < n1; n2++) {
if (n1 % n2 == 0) {
sum = sum + n2;
}
}
if (sum == n1) {
System.out.println(n1 + " is a perfect number");
counter ++; //This will add one to the counter if this loop is enterd
}
if(n1 == endval){
System.out.println("FINISHED!");
break;
}
}
//If the counter is 0 then it will display the message
if(counter == 0){
System.out.println("THERE IS NO PERFECT NUMBERS");
}
}

Related

How to answer with (y,Y OR n,N) on math expression with random num and operators

The question: is n1>n2 OR n1<n2
The answer: y,Y OR n,N
The num + operators must be random.
If the user wrong he can try again - one more time.
So far I get to that point but Im stuck.
I need help with my code, or another way that will be easier for me to answer that.
public static void code1() {
Scanner s = new Scanner(System.in);
Random r = new Random();
int n1 = r.nextInt(10) + 1;
int n2 = r.nextInt(10) + 1;
int max = Math.max(n1, n2);
int min = Math.min(n1, n2);
boolean biggerThan = n1 % 2 == 0; //if this value is true it will print >
boolean smallerThan = n1 % 2 !=0;
System.out.print(n1);
if (biggerThan)
System.out.print(">");
if (smallerThan)
System.out.print("<");
System.out.println(n2);
char answer = s.next().charAt(0);
if(answer=='y'||answer=='Y'){
if(biggerThan) {
if (((max = n1) > (min = n2)))
System.out.println("Well done");
else if (((min = n1) > (max = n2))) //
System.out.println("Wrong answer");
}
if(smallerThan){
if (((max = n1) < (min = n2)))
System.out.println("Wrong answer");
else if (((min = n1) < (max = n2))) //
System.out.println("Well done");
}
}

How to make while loop check if there are 16 digits in a string

How do I make the loop check if there is 16 digits in a string and reset the string if there is not enough. I am trying to make a credit card program that will calculate the check digit. I have everything else working I just cant get the program to check the number of digits in the user inputted string.Thanks for any and all help!
import java.util.Scanner;
public class LuhnAlgorithm {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number credit card number (Enter a blank line to quit: ");
String nums = input.nextLine();
int i = 0;
char chk = nums.charAt(15);
while(!nums .equals("") ) {
if (nums.length()<16 || nums.length() > 15){ //How do I get this line to reset the while loop?
System.out.println("ERROR! Number MUST have exactly 16 digits.");
}
int sum = 0;
for( i = 0; i < 15; i++) {
char numc = nums.charAt(i);
int num = Character.getNumericValue(numc);
if ( i % 2 == 0 ) {
num = num * 2;
if ( num >= 10) {
num = num - 9;
}
}
sum = num + sum;
}
int sum2 = sum % 10;
if (sum2 > 0) {
sum2 = 10 - sum2;
}
int chk2 = Character.getNumericValue(chk);
System.out.println("The check digit should be: " + sum2);
System.out.println("The check digit is: " + chk);
if ( sum2 == chk2) {
System.out.println("Number is valid.");
}
else {
System.out.println("Number is not valid. ");
}
System.out.print("Enter a number credit card number (Enter a blank line to quit:) ");
nums = input.nextLine();
}
System.out.println("Goodbye!");
input.close();
}
}
You can include your code that you only want done if the length ==16 in an if statement.
Meaning, instead of:
if (nums.length != 16) {
//code if there is an error
}
//code if there is no error
you can do:
if (nums.length == 16) {
//code if there is no error
} else {
//code if there is an error
}
(I also want to point out that you set chk = nums.charAt(15) before your while loop, but you don't reset it in the while loop for the next time the user inputs a new credit card number.)
You can bring the prompts and all your initialization except the scanner itself into the while loop. Then if they say "", break to exit the loop. If they say a number that is too short or too long, say continue to go back to the prompting.
Thus:
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.print("Enter a number credit card number (Enter a blank line to quit: ");
String nums = input.nextLine().trim();
if (nums.length() == 0) {
break; //exits while loop
}
if (nums.length() != 16) { //How do I get this line to reset the while loop?
System.out.println("ERROR! Number MUST have exactly 16 digits.");
continue; //goes back to the beginning right away
}
//still here, process the number
char chk = nums.charAt(15);
int sum = 0;
for (int i = 0; i < 15; i++) {
char numc = nums.charAt(i);
int num = Character.getNumericValue(numc);
if (i % 2 == 0) {
num = num * 2;
if (num >= 10) {
num = num - 9;
}
}
sum = num + sum;
}
int sum2 = sum % 10;
if (sum2 > 0) {
sum2 = 10 - sum2;
}
int chk2 = Character.getNumericValue(chk);
System.out.println("The check digit should be: " + sum2);
System.out.println("The check digit is: " + chk);
if (sum2 == chk2) {
System.out.println("Number is valid.");
} else {
System.out.println("Number is not valid. ");
}
}
System.out.println("Goodbye!");
input.close();
}
}

How can I implement a sequence of numbers in this Prime number generator?

I'm unsure of how to create a sequence of numbers that can be placed before each iteration of printed prime numbers. Thank you for any help you can offer.
public class CountingPrimes {
public static void main(String[] args) {
int flag = 0, i, j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the 1st number: ");
int firstNum = sc.nextInt();
System.out.println("Enter the 2nd number: ");
int secondNum = sc.nextInt();
System.out.println("Counting prime numbers between "
+ firstNum + " and " + secondNum + ":");
for (i = firstNum; i <= secondNum; i++) {
for (j = 2; j < i; j++) {
if (i % j == 0) {
flag = 0;
break;
} else {
flag = 1;
}
}
if (flag == 1) {
System.out.println(i);
}
}
}
}
Right now, my code outputs (after the user enters their two numbers):
Counting prime numbers between 1 and 14:
3
5
7
11
13
What I need my code to look like:
Counting prime numbers between 1 and 14:
1. 3
2. 5
3. 7
4. 11
5. 13
Also, if you could see any errors or improvements I could change, I would greatly appreciate it. Thank you again!
You can use a counter and print the counter as you print the prime number. Increment the counter each time.
int counter = 1;
int flag = 0, i, j;
.....
if (flag == 1) {
System.out.format("%d. %d\n", counter, i);
counter++;
}
a simple change:
import java.util.Scanner;
public class CountingPrimes {
public static void main(String[] args) {
int flag = 0, i, j;
int count = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the 1st number: ");
int firstNum = sc.nextInt();
System.out.println("Enter the 2nd number: ");
int secondNum = sc.nextInt();
System.out.println("Counting prime numbers between "
+ firstNum + " and " + secondNum + ":");
for (i = firstNum; i <= secondNum; i++) {
for (j = 2; j < i; j++) {
if (i % j == 0) {
flag = 0;
break;
} else {
flag = 1;
}
}
if (flag == 1) {
System.out.println(++count + "." + i);
}
}
}
}
Just add a count variable and increment it whenever you output a number:
...
int count = 0;
for (i = firstNum; i <= secondNum; i++) {
...
if (flag == 1) {
count++;
System.out.format("%d. %d%n", count, i);
}
}
Declare Count before for loop
int count = 0;
and then increment the count on every prime number.
if (flag == 1) {
System.out.println(++count+". "+i);
}
}

How would I get this output?

import java.util.Scanner;
public class EuclidGCD {
public static void main(String[] args) {
Scanner kbd = new Scanner (System.in);
System.out.print ("Enter First Number: ");
int n1 = kbd.nextInt();
System.out.print ("Enter Second Number: ");
int n2 = kbd.nextInt();
int gcd = 1;
int k = 2;
while (k <= n1 && k <= n2){
if (n1 % k == 0 && n2 % k == 0)
gcd = k ;
k ++;
}
System.out.println("The GCD of " + n1 + " and " + n2 + " is " + gcd);
}
}
so everything works but I realized that I do not want a neg number if a negative number is entered I want the output POSITIVE NUMBERS ONLY so would I need another while loop?
Just add a if statement checking if n1 and n2 are positive, and only excuted the rest of your code if they are. You shouldn't need another while loop
public static void main(String[] args) {
Scanner kbd = new Scanner (System.in);
System.out.print ("Enter First Number: ");
int n1 = kbd.nextInt();
System.out.print ("Enter Second Number: ");
int n2 = kbd.nextInt();
if(n1 < 0 || n2 < 0 ){
System.out.println("POSITIVE NUMBERS ONLY ");
}else{
int gcd = 1;
int k = 2;
while (k <= n1 && k <= n2){
if (n1 % k == 0 && n2 % k == 0)
gcd = k ;
k ++;
}
System.out.println("The GCD of " + n1 + " and " + n2 + " is " + gcd);
}
}

while loop - sum of even numbers and its average

http://pastebin.com/w8KntkE6#
package javaapplication9;
import java.text.DecimalFormat;
import java.util.Scanner;
public class JavaApplication9 {
public static void main(String[] args) {
// TODO code application logic here
{
int sum = 0, num, count = 0;
double average;
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer (0 to quit): ");
num = scan.nextInt();
while (num >= 0) // sentinel num of 0 to terminate loop
{
if (num % 2 == 0) {
count++;
sum += num;
System.out.println("The sum so far is " + sum);
System.out.print("Enter an integer (0 to quit): ");
num = scan.nextInt();
} else if (num % 2 != 0) {
continue;
}
System.out.println();
if (count == 0) {
System.out.println("No nums were entered.");
} else {
average = (double)sum / count;
DecimalFormat fmt = new DecimalFormat("0.###");
System.out.println("The average is " + fmt.format(average));
}
}
}
}
}
The code executes and it sums even numbers. However, when a odd number enters it returns an error or it breaks. I want it ignore any odd number. What is wrong with my code?
You continue the loop on odd numbers without modifying num - looks like it should infinite loop to me.
Am I missing something, or are you missing a nextInt() when you have an odd number? Since in the if even you have num = scan.nextInt();. You don't when num is odd.
Change
else if (num % 2 != 0){
continue;
}
to
else if (num % 2 !=0){
num = scan.nextInt();
continue;
}
just before continue statement add following code. I hope it will work correctly.
System.out.print("Enter an integer (0 to quit): ");
num = scan.nextInt();
Change to:
package javaapplication9;
import java.text.DecimalFormat;
import java.util.Scanner;
public class JavaApplication9 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
{
int sum = 0, num, count = 0;
double average;
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer (0 to quit): ");
num = scan.nextInt();
while (num >= 0) // sentinel num of 0 to terminate loop
{
if (num % 2 == 0)
{
count++;
sum += num;
System.out.println("The sum so far is " + sum);
System.out.print("Enter an integer (0 to quit): ");
num = scan.nextInt();
if (count == 0) {
System.out.println("No nums were entered.");
} else {
average = (double) sum / count;
DecimalFormat fmt = new DecimalFormat("0.###");
System.out.println("The average is " + fmt.format(average));
}
}
num = scan.nextInt();
}
}
Try this. I'm not quite sure when do you want to print the average, you could move it out of the while loop if you want to print it at the end.
public static void main(String[] args) {
// TODO code application logic here
{
int sum = 0, num, count = 0;
double average;
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer (0 to quit): ");
while ((num = scan.nextInt())> 0) // sentinel num of 0 to terminate loop
{
if (num % 2 == 0){
count++;
sum += num;
System.out.println("The sum so far is " + sum);
if (count == 0) {
System.out.println("No nums were entered.");
} else {
average = (double) sum / count;
DecimalFormat fmt = new DecimalFormat("0.###");
System.out.println("The average is " + fmt.format(average));
}
}
System.out.print("Enter an integer (0 to quit): ");
}
System.out.println("end");
}
}

Categories