(Financial: credit card number validation) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. It must start with
- for Visa cards
- for Master cards
- for American Express cards
- for Discover cards
In 1954, Hans Luhn of IBM proposed an
- 1. Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.
- 2. Now add all single-digit numbers from Step 1.
4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
- 3. Add all digits in the odd places from right to left in the card number.
6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
- 4. Sum the results from Step 2 and Step 3.
37+38=75
- 5. If the result from Step 4 is divisible by 10, the card number is valid; otherwise, it is invalid. For example, the number 4388576018402626 is invalid, but the number 4388576018410707 is valid.
Write a program that prompts the user to enter a credit card number as a 1ong integer. Display whether the number is valid or invalid. Design your program to use the following methods:
/** Return true if the card number is valid */
public static boolean isValid(long number)
/** Get the result from Step 2 */
public static int sumOfDoubleEvenPlace(long number)
/** Return this number if it is a single digit, otherwise, • return the sum of the two digits */
public static int getDigit(int number)
/** Return sum of odd-place digits in number */
public static int sumOfOddPlace(long number)
/** Return true if the number dis a prefix for number *I
public static boolean prefixMatched(long number, int d)
/** Return the number of digits in d */
public static int getSize(long d)
/ ** Return the first k number of digits from number. If the • number of digits in number is less than k, return number. */
public static long getPrefix(long number , int k)
Here are sample runs of the program: (You may also implement this program by reading the input as a string and processing the string to validate the credit card.)
Trending nowThis is a popular solution!
Chapter 6 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Electric Circuits. (11th Edition)
INTERNATIONAL EDITION---Engineering Mechanics: Statics, 14th edition (SI unit)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
- (True or False) Seven different positive integers are randomly chosen between 1 and 2022 (including 1 and 2022).There must be a pair of these integers has a difference that is a multiple of 6.arrow_forward(PYTHON) A Krishnamurthy number is a number which sum of the factorial of its digits is equal to the number itself. For example: Let us consider the number 145. Factorial sum = 1! + 4! + 5! = 1 + 24 + 120 = 145. Therefore 145 is a Krishnamurthy number. Other examples include: 1, 2, 40585. Write a program that does the following: • asks the user to input an integer. • computes whether the number is a Krishnamurthy number. • then finally prints the result. Note: You are not allowed to use the built-in function math.factorial.arrow_forward10 - In an examination, 500 students appeared. Out of these students, 38 % got A+ grade, 45 % got B+ and the remaining just passed. Assuming that no student failed, find the number of students who got A+, B+ and the number of students who just passed.(Python code)arrow_forward
- *Please help in javascript* Summary: Given integer values for red, green, and blue, subtract the gray from each value. Computers represent color by combining the sub-colors red, green, and blue (rgb). Each sub-color's value can range from 0 to 255. Thus (255, 0, 0) is bright red, (130, 0, 130) is a medium purple, (0, 0, 0) is black, (255, 255, 255) is white, and (40, 40, 40) is a dark gray. (130, 50, 130) is a faded purple, due to the (50, 50, 50) gray part. (In other words, equal amounts of red, green, blue yield gray). Given values for red, green, and blue, remove the gray part. Ex: If the input is: 130 50 130 the output is: 80 0 80 import java.util.Scanner; public class LabProgram {public static void main(String[] args) {/* Type your code here. */}}arrow_forward(Find the smallest n such that n2 12,000) Use a while loop to find the smallestinteger n such that n2 is greater than 12,000.arrow_forward(Find the smallest n such that n^2 > 12,000) Use a while loop to find the smallest integer n such that n^2 is greater than 12,000. (PYTHON)arrow_forward
- (IN C LANGUAGE) Cumulative Addition: Computer selects a number between 7 and 23 at random. User will only add 2, 3 or 5 numbers to reach that number. For example: To reach 14:User will enter 5 5 2 2 (4 input).Also he can enter 2 2 2 2 2 2 2 (7 input) or 3 3 3 3 2 (5 input)arrow_forwardPlease code in python Kiki is making “Happy National Pizza Day” (February 9th) cards for all of her friends. She’s a bit disappointed with the cards not looking special enough, so she decides to add glitter to them to make them look more special. However, she’s short on glitter, so she decides to add glitter selectively. She uses a box with N*N divided sections to store her cards. Each section is huge and each card is small, so she can put multiple cards in a given section and they will never overlap. We model the way that she organizes the cards and adds glitter using two commands: Command 1: 1 x y: add a card to the box in section (x, y). Command n: 2 x1 y1 x2 y2: Add one unit of glitter to each of the cards in the sections from (x1, y2) to (x2, y2). Help Kiki determine the total number of units of glitter that she placed on the cards. Input The first line will contain N (1 <= N <= 500) and C (1 <= C <= 500 ), the square size of the card storage box…arrow_forward(Demonstrate cancellation errors) A cancellation error occurs when you are manipulating a very large number with a very small number. The large number may cancel out the smaller number. For example, the result of 100000000.0 + 0.000000001 is equal to 100000000.0. To avoid cancellation errors and obtain more accurate results, carefully select the order of computation. For example, in computing the following series, you will obtain more accurate results by comput- ing from right to left rather than from left to right: 1 1 1 1+ + 3 + ... 2 п Write a program that compares the results of the summation of the preceding series, computing from left to right and from right to left with n = 50000.arrow_forward
- *Using Python The scientist has 500 cages in which to hold her rabbits. Each cage holds one pair of rabbits. Assuming that no rabbits ever die, when will she run out of cages? Your program must do the following: Print a table that contains the following information for each month. The number of months that have passed. The number adult rabbit pairs (those over 1 month old). The number of baby rabbits pairs produced this month. The total number of rabbit pairs in the lab. Calculate how many months it will take until the number of rabbits exceeds the number of available cages. Stop printing when you run out of cages. Print a message giving how many months it will take to run out of cages Output file should look like the following. Comments in the file begin with '#', and must appear as shown too: Code must contain def main(): #main function need in all programs for automated testing """ Program starts here """ #end of main program if __name__ == '__main__': main() #excucte main…arrow_forwardUse While loop to solve the problems Note: Python language for solution 4. Write a program that uses a while loop, the program should do the following: The program should repeatedly read numbers until the user enters "done". Once "done" is entered, print out the total, count, and average of the numbers. 5. Write a program to find the sum of even numbers until n, where n is an int entered by the user, example: n = 11, sum = 2+4+6+8+10, sum = 30.arrow_forward(Bar-Chart Printing Program) One interesting application of computers is drawing graphsand bar charts. Write a program that reads five numbers (each between 1 and 30). For each numberread, your program should print a line containing that number of adjacent asterisks. For example,if your program reads the number seven, it should print *******.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,