I'm trying to program the Luhn Algorithm within Java.
My current code is :
import java.util.Scanner;
public class luhnAlgorithm {
public static void main(String[] args) {
System.out.println("main() : Entry Point");
Scanner input = new Scanner(System.in);
long num;
int digit;
int sum = 0;
System.out.println("Enter the digits of a credit card number : ");
num = input.nextLong();
while (num > 0) {
digit = (int) num % 10;
num = num / 10;
if (num % 2 != 0 ) {
digit *= 2;
}
if (digit > 9) {
digit -= 9;
}
sum += digit;
}
if(sum % 10 == 0) {
System.out.println("Credit card number is valid.");
}
else
System.out.println("Credit card number is invalid. Please try again.");
System.out.println("main() : Exit Point");
}
}
The problem I'm having is that when I enter in a valid credit card number, for example : 4012888888881881 (via PayPal Test Credit Card Accounts), it says it's invalid. But when I put in my own debit card number it says it's valid.
I know there's some code messed up in here, I just can't figure out what.
Any help would be appreciated and thanks in advance!
I think that I know where the problem is. You do the multiplication in a wrong way:
if (num % 2 != 0 ) {
digit *= 2;
}
You multiply digit when num is odd and in Luhn's algorithm you should multiply digit when this digit is on the even place in number moving from right to the left. Try to think about adding some index which will help you to know if a digit is on even place or not.
You can think about splitting your integer to array and then check if index of array is even or add to your while loop some index.
Carefully study this https://en.wikipedia.org/wiki/Luhn_algorithm
for example if you have 68 then you have: first iteration: digit = 8, num = 6 and sum =8 second iteration: digit = 6, num = 0 here you should multiply your digit by 2, because it is on the even place in number, but you don't do that and sum = 14 instead of 20
Okay I actually figured it out :).
import java.util.Scanner;
/*
* Author : Jonathan Patterson
* Date : 10/28/15
* Program : Luhn Algorithm; validates credit card numbers
*/
public class luhnAlgorithm {
public static void main(String[] args) {
System.out.println("main() : Entry Point");
Scanner input = new Scanner(System.in);
long num;
double digit = 0;
int sum = 0;
int n = 1;
int i = 0;
System.out.println("Enter the digits of a credit card number : ");
num = input.nextLong();
while (num > 0) {
digit = num % 10;
num = num / 10;
System.out.println(n + " digit is : " + digit);
if (i % 2 != 0 ) {
digit *= 2;
}
System.out.println(n + " digit is : " + digit);
if (digit > 9) {
digit = (digit % 10) + 1;
}
else
digit *= 1;
System.out.println(n + " digit is : " + digit);
sum += digit;
n++;
i++;
}
System.out.println("Sum of the digits is : " +sum);
if(sum % 10 == 0) {
System.out.println("Credit card number is valid.");
}
else
System.out.println("Credit card number is invalid. Please try again.");
System.out.println("main() : Exit Point");
}
}
Adding an additional answer to this in case anyone else finds this post.
There is a bitbucket project with a valid implementation:
https://bitbucket.org/javapda/npi-validator/src/master/
This if for verifying NPI provider numbers which is an implementation of the Luhn algorithm.
package org.bitbucket.javapda.npi;
import java.util.ArrayList;
import java.util.List;
public class NpiValidator {
private String npi;
public NpiValidator(String npi) {
this.npi = npi.trim();
}
public boolean isValid() {
return npi.length() == 10 && complies();
}
private boolean complies() {
if (!npi.matches("[0-9]{10}")) {
return false;
}
Character lastDigit = npi.charAt(9);
List<Integer> everyOther = listWithEveryOtherDoubled(npi.substring(0, 9));
int sum = 0;
for (Integer num : everyOther) {
sum += sumOfDigits(num);
}
int total = sum + 24; // 24 to account for 80840
int units = total % 10;
int checkDigit = (units != 0) ? (10 - units) : units;
return (Character.getNumericValue(lastDigit) == checkDigit);
}
private List<Integer> listWithEveryOtherDoubled(String str) {
List<Integer> nums = new ArrayList<Integer>();
for (int i = 0; i < str.length(); i++) {
if (i % 2 == 0) {
nums.add(2 * Character.getNumericValue(str.charAt(i)));
} else {
nums.add(Character.getNumericValue(str.charAt(i)));
}
}
return nums;
}
private static int sumOfDigits(int number) {
int num = number;
int sum = 0;
while (num > 0) {
sum += (num % 10);
num = num / 10;
}
return sum;
}
public static void main(String[] args) {
System.out.println("Hello, World!");
// System.out.println(sumOfDigits(16));
System.out.println("1234567890".matches("[0-9]{10}"));
System.out.println("123456789".matches("[0-9]{10}"));
}
}
Related
I want to reverse an integer. I have trying by the following code, the problems occurs when the number is so large that its reverse gets larger than the int limit (for example 1534236469), so in that case it returns some garbage integer.
How can I make use of Integer.MIN_VALUE and Integer.MAX_VALUE to check if the reversed number is within the limits?
Note: it must use int variable type only.
int num = 1534236469;
int reverseInt = 0;
int multiplier = 1;
if ( num < 0 ) {
multiplier *= -1;
}
while ( num != 0 ) {
//get the last digit
int digit = num % 10;
//multiply the reverseInt by 10 and then add the last digit
reverseInt = (reverseInt * multiplier) + digit;
multiplier = 10;
num /= 10;
}
//how to fix this.
if (reverseInt < Integer.MIN_VALUE || reverseInt > Integer.MAX_VALUE ) {
System.out.println("Invalid number");
} else {
System.out.println("Reversed integer is " + reverseInt);
}
You just have to make the check before you add the next digit, so you can simply move your check up to the actual calculation. If your current reversed positive number is greater than Integer.MAX_VALUE/10, then you can't add another digit. Likewise with negative numbers.
The only thing I did was move this part of your code up:
if (reverseInt < Integer.MIN_VALUE || reverseInt > Integer.MAX_VALUE ) {
System.out.println("Invalid number");
} else {
then I added the /10 and a return statement, since the program must end when there's an overflow:
public class StackOverflowTest {
public static void main(String[] args) {
int num = -1534236469;
int reverseInt = 0;
int multiplier = 1;
if ( num < 0 ) {
multiplier *= -1;
}
while ( num != 0 ) {
// if this next step will push is into overflow, then stop:
if (reverseInt < Integer.MIN_VALUE/10 || reverseInt > Integer.MAX_VALUE/10) {
System.out.println("Invalid number");
return;
} else {
//get the last digit
int digit = num % 10;
// multiply the reverseInt by 10 and then add the last digit
reverseInt = (reverseInt * multiplier) + digit;
}
multiplier = 10;
num /= 10;
}
System.out.println("Reversed integer is " + reverseInt);
}
}
Alternatively you can treat it as a String, and then simply reverse the String (along with fiddling with the sign):
public class StackOverflowTest {
public static void main(String[] args) {
reverse(1534236469);
System.out.println();
reverse(-153423646);
}
public static void reverse(int num) {
System.out.println("int = " + num);
int number = num < 0 ? -num : num; // remove the sign
String reverse = new StringBuilder(String.valueOf(number)).reverse().toString();
System.out.println("reverse String: " + reverse);
try {
int reversed = Integer.parseInt(reverse);
reversed = num < 0 ? -reversed : reversed; // get back the sign
System.out.println("Reversed integer is " + reversed);
} catch (NumberFormatException ex) {
System.out.println("Invalid number");
}
}
}
Prints:
int = 1534236469
reverse String: 9646324351
Invalid number
int = -153423646
reverse String: 646324351
Reversed integer is -646324351
In Java 8+, change to:
reverseInt = Math.addExact(Math.multiplyExact(reverseInt, multiplier), digit);
The code will now throw ArithmeticException if the result overflows.
Whenever the value of Integer.MAX_VALUE is exceeded, it starts from Integer.MIN_VALUE i.e. Integer.MAX_VALUE + 1 evaluates to Integer.MIN_VALUE. You can use this feature to get the solution you are looking for.
Simply multiply reverseInt in a loop from 1 to 10 and if the value becomes negative, it means that the next value (reverseInt * multiplier) will exceed Integer.MAX_VALUE. Also, you need to add reverseInt * multiplier in a loop to a value from 0 to 9 and if the value becomes negative, it means that the next value (reverseInt * multiplier + digit) will exceed Integer.MAX_VALUE.
public class Main {
public static void main(String[] args) throws InterruptedException {
int num = 1534236469;
boolean valid = true;
int reverseInt = 0;
int multiplier = 1;
if (num < 0) {
multiplier *= -1;
}
while (num != 0) {
// get the last digit
int digit = num % 10;
for (int i = 1; i <= multiplier; i++) {
for (int j = 0; j <= 9; j++) {
if (reverseInt * i < 0 || (reverseInt * i + j) < 0) {
System.out.println("Invalid number");
valid = false;
break;
}
}
if (!valid) {
break;
}
}
if (!valid) {
break;
}
// multiply the reverseInt by 10 and then add the last digit
reverseInt = reverseInt * multiplier + digit;
multiplier = 10;
num /= 10;
}
if (valid) {
System.out.println("Reversed integer is " + reverseInt);
}
}
}
Output:
Invalid number
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();
}
}
I'm supposed to create an application that takes a number from the user in order to create a range from 1 to the number that the user enters. I then need to calculate the sum of all odd numbers between 1 and the number that the user enters. This works fine with positive integers but not with negative?
import java.util.Scanner;
public class OddSumApplication {
public static void main (String[]args){
int sum = 0;
System.out.print("Enter a positive or negative integer: ");
Scanner in = new Scanner(System.in);
int num = in.nextInt();
if (num == 1){
System.out.println(num);
System.out.println("Sum = " + num);
}
else{
while (num != 1){
if (num >= 1){
if (num % 2 == 1){
sum = sum + num;
num = num - 2;
}
else{
num = num - 1;
}
}
else{
if (num % 2 == 1){
sum = sum + num;
num = num + 2;
}
else{
num = num + 1;
}
}
}
}
sum = sum + 1;
System.out.print("\nSum = " + sum);
}
}
It's because negative odd number % 2 returns negative one. Try to give it into absolute value.
...
if (Math.abs(num%2) == 1) {
sum = sum + num;
num = num + 2;
} else {
num = num + 1;
}
A good check to find a number is odd is to check if the remainder by dividing the number is not 0 rather then checking if it is 1. The method would look like:
public boolean isOdd(int number) {
return !(number % 2 == 0);
}
Below is my code.
package com.ofss.java.examples;
import java.util.Scanner;
class ArmstrongNumber {
public static void main(String[] args) {
int c = 0, a;
int n1, n2;//Range in which armstrong number need to find
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number");
n1 = s.nextInt();
System.out.println("Enter the second number");
n2 = s.nextInt();
for (int i = n1; i <= n2; ++i) {
while (i > 0) {
a = i % 10;
System.out.println(a);
i = i / 10;
System.out.println(i);
c = c + (a * a * a);
System.out.println(c);
}
if (i == c)
System.out.println(c + "armstrong number");
else
System.out.println(c + "Not armstrong number");
}
}
}
I am getting incorrect results after executing. Code runs for infinite number till you stops it. It must print number between 151-154 (153 as armstrong).
Also, it is incorrectly printing 153 as not Armstrong number.
Armstrong Number
...is a number that is the sum of its own digits each raised to the power of the number of digits.
You should not change i since this is also used in
for (int i = n1; i <= n2; ++i)
Or you will probably never exit that loop eiter since you expect i to be negative at the end of the first iteration. Hard to increment until it reach n2.
Use a different variable to keep track of i safely.
int j = i;
while(j > 0) ...
About Armstrong number:
Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits
You need to put each digit to the power of the length of the number (the number of digit).
153 = 1^3 + 5^3 + 3^3
1634 = 1^4 + 6^4 + 3^4 + 4^4
Here is the method for it :
public static boolean isArmstrongNumber(int number){
int power = Integer.toString(number).length(); //just to get the number of digit...
int tmp = number;
int digit , sum = 0;
while(tmp > 0){
digit = tmp % 10;
sum += Math.round(Math.pow(digit , power));
tmp /= 10;
}
return sum == number;
}
Using this check from 0 to 10.000 gives :
0
1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
Same as Wikipedia :
The sequence of base 10 narcissistic numbers starts: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, ...
Note that using a method remove the risk of forgetting to reset your variable like c in your case. Correcting this would give you a few more "correct" results (well the one with 3 digits)
You can also use less mathematics to read the number and use char[], remember that you need to substract '0' value to get the numeric value for a character :
public static boolean isArmstrongNumber(int number){
char[] digits = Integer.toString(number).toCharArray();
int power = digits.length;
int sum = 0;
for(char c : digits){
int digit = c - '0';
sum += Math.round(Math.pow(digit, power));
}
return sum == number;
}
There are two things.
you are updating i everytime as you have used it in while, so use different variable than i for this calculation.
int num = i;
c is used to compare sum of cube is same as number but you are not resetting it once the one iteration is over. so make c=0 inside for loop.
c = 0;
Also while printing you are using c, there should be i which is correct and real number.
Below is the working code you may try.
public static void main(String[] args)
{
int c = 0, a;
int n1, n2;//Range in which armstrong number need to find
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number");
n1 = s.nextInt();
System.out.println("Enter the second number");
n2 = s.nextInt();
for (int i = n1; i <= n2; ++i)
{
int num = i;
while (num > 0)
{
a = num % 10;
num = num / 10;
c = c + (a * a * a);
}
if (i == c)
System.out.println(i + "armstrong number");
else
System.out.println(i + "Not armstrong number");
c = 0;
}
}
public class ArmstrongNumber {
private final int n1, n2;
public ArmstrongNumber(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
protected static boolean isArmstrong(int n) {
if(n < 0)
return false;
int remaining=n;
int sumCube=0;
while (remaining>0) {
int d = remaining % 10;
sumCube += cube(d);
remaining /= 10;
}
return n == sumCube;
}
private static int cube(int d) {
return d*d*d;
}
public Integer[] find() {
List<Integer> results = new ArrayList<>();
for (int i = n1; i <= n2; ++i)
{
if (isArmstrong(i))
results.add(i);
}
return results.toArray(new Integer[0]);
}
}
There are lot of things to improve in your code. One of the working logics as below:
for (int i = n1; i <= n2; ++i) {
int sum = 0, remainder = 0, digits = 0, temp = 0;
temp = i;
while (temp != 0) {
digits++;
temp = temp / 10;
}
temp = i;
while (temp != 0) {
remainder = temp % 10;
sum = sum + (int) Math.pow(remainder, digits);
temp = temp / 10;
}
if (i == sum)
System.out.println(i + " is an Armstrong number.");
else
System.out.println(i + " isn't an Armstrong number.");
}
As above, I have two methods,
public static int reverse(int number)
and
public static boolean isPalindrome(int number)
I should arrange them to check whether a number is palindrome or not. I tried to apply the hint (a number is palindrome if (its reversal == itself)) but it did not help.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("This program checks whether a number is palindrome or not. Enter a number: ");
Scanner input = new Scanner(System.in);
int inputNumber = input.nextInt();
// if inputNumber is palindrome
System.out.println("The number is palindrome.");
// else
System.out.println("The number is not a palindrome.");
}
public static int reverse(int number)
// reverse number
public static boolean isPalindrome(int number)
// check if the number is palindrome
}
}
In order to reverse a number you should divide it by 10 and add the remainder to a new reversed number multiplied by 10.
For example, this will be the steps of reverting 123:
reversed = 0, target = 123
reversed = 0 * 10 + 123 % 10 = 0 + 3 = 3, target = 123 / 10 = 12
reversed = 3 * 10 + 12 % 10 = 30 + 2 = 32, target = 12 / 10 = 1
reversed = 32 * 10 + 1 % 10 = 320 + 1 = 321, target = 1 / 10 = 0
Here is implementation of this algorithm in Java:
public static int reverse(int number) {
int reversed = 0;
while (number > 0) {
reversed *= 10;
reversed += number % 10;
number /= 10;
}
return reversed;
}
public static boolean isPalindrome(int number) {
return number == reverse(number);
}
Try this method for reverse.
Step0 : temp = 0 number = 4554
Step 1 : temp = 4 number = 455
Step 2 : temp = 4*10 + 5 = 45 and number = 45
Step 3 : temp = 45*10 + 5 = 455 and number = 4
Step 4 : temp = 455*10 + 4 = 4554 and number = 0
if (inputNumber == reverse(inputNumber)) {
System.out.println("The number is palindrome.");
}else{
System.out.println("The number is not a palindrome.");
}
public static int reverse(int number) {
int temp = 0;
while (number > 0) {
temp = temp * 10 + number % 10;
number = number / 10;
}
return temp;
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer: " );
int inputNumber = input.nextInt();
if (isPalindrome(inputNumber))
System.out.println(inputNumber + " is a palindrome. ");
else
System.out.println(inputNumber + " is not a palindrome. ");
input.close();
}
public static int reverse(int n1) {
int reversedNumber = 0;
while (n1 != 0) {
reversedNumber *= 10;
reversedNumber += (n1 % 10);
n1 /= 10;
}
return reversedNumber;
}
public static boolean isPalindrome(int n2) {
if (n2 == reverse(n2))
return true;
else
return false;
}
}
This is a solution to the problem. But this does not cover the case in which I could enter "011" and the program will say that "11" is a palindrome. How could I solve this?
The following algorithm will work
1. Loop until the input number become 0.
1.1 remainder = number % 10;
1.2 reverse = reverse * 10 + remainder;
1.3 number = number / 10;
2. Return reverse when the loop breaks. That means number became 0.
Check full implementation here.