Question

Please answer in Visual Studio 2019 c# format. Not python. Thank you.

Q. Write a program that works as described in the following scenario: The user enters a credit card number. The program displ3. 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 resul

0 0
Add a comment Improve this question Transcribed image text
Answer #1


using System;
class Card {
static long card_num;
static int e_sum=0,o_sum=0;
static void Main()
{
//read the card number from the user
Console.WriteLine("Enter the card number :");
card_num = Convert.ToInt64(Console.ReadLine());
//check Isvalid() function
if(Isvalid(card_num)==true)
{
Console.WriteLine("Valid");
}
else
{
Console.WriteLine("Invalid");
}
}
//returns the size of the number passed
public static int GetSize(long d)
{
return d.ToString().Length;
}
//gets the prefix
public static long GetPrefix(long number,int k)
{
string x=number.ToString();
if(x.Length>k-1)
{
x=x.Substring(0,k);
long y=long. Parse(x);
return y;
}
else
{
return number;
}

}
public static bool PrefixMatched(long number,int d)
{
//here a default prefix value of lenth 4 is passed
long x=GetPrefix(438857,4);
//reads prefix of user inputed card number
long y=GetPrefix(number,d);
//if both are same returns true else false
if(x==y)
{
return true;
}
else
{
return false;
}
}
//checks the pased number is two digit or not if yes finds the sum of it's two digits
public static int GetDigit(int number)
{
int x;
if(number>9)
{
x=(number/10)+(number%10);
}
else
{
x=number;
}
return x;
}
//returns the sum of even place
public static int SumOfDoubleEvenPlace(long number)
{
e_sum=e_sum+Convert.ToInt32(number);
return e_sum;
}
//returns the sum of odd place
public static int SumOfOddPlace(long number)
{
o_sum=o_sum+Convert.ToInt32(number);
return o_sum ;
}
public static bool Isvalid(long number)
{
//initially checks the prefix if true executes following else return false for Isvalid()
if((PrefixMatched(card_num,4))==true)
{
string check=card_num.ToString();
for(int i=(GetSize(card_num)-1);i>=0;i--)
{
if(i%2==0)
{
e_sum=(SumOfDoubleEvenPlace(Convert.ToInt64(GetDigit(Convert.ToInt32(2*((check[i])-'0'))))));
}
else
{
o_sum=(SumOfOddPlace(Convert.ToInt64(GetDigit(Convert.ToInt32(((check[i])-'0'))))));
}
}
if((e_sum+o_sum)%10==0)
{
return true;
}
else
{
return false;
}
  
}
else
{
return false;
}
}
}

OUTPUT

Enter the card number :
4388576018410707
Valid
4 5 1e 14 17 18 19 20 1 using System; 2. class Card { 3 static long card_num; static int e_sum=2,0_sum=e; static void Main() 49 50 51 52 53 54 55 56 57 58 59 62 61 62 63 64 65 66 67 68 69 7e 71 72 73 74 75 76 77 78 79 se 81 82 83 84 85 86 87 { return94 95 96 97 98 99 lee 101 102 183 184 1e5 186 107 188 109 110 111 112} { o_sum=(SumofoddPlace(Convert.ToInt64(GetDigit(ConverEnter the card number : 4388576018410707 Valid

Add a comment
Know the answer?
Add Answer to:
Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit...

    Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits, and must start with: 4 for Visa cards 5 for Master cards 6 for Discover cards 37 for American Express cards The algorithm for determining...

  • REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter...

    REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter a credit card number. Display whether the number is valid and the type of credit cards (i.e., Visa, MasterCard, American Express, Discover, and etc). The requirements for this assignment are listed below: • Create a class (CreditNumberValidator) that contains these methods: // Return true if the card number is valid. Boolean isValid(String cardNumber); // Get result from Step 2. int sumOfDoubleEvenPlace(String cardNumber); // Return...

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card...

  • Credit card numbers follow certain patterns. A credit card number must have between 13 and 16...

    Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. The number must start with the following: 4 for Visa cards 5 for MasterCard cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or is scanned correctly by a scanner. Almost all credit card numbers...

  • I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the...

    I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the program should read your credit card (for example: 5278576018410787) and verify whether it is valid or not. In addition, the program must display the type (i.e. name of the company that produced the card). Each credit card must start with a specific digit (starting digit: 1st digit from left to ight), which also is used to detemine the card type according Table 1. Table...

  • Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type...

    Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type of credit card he/she would like to find the checksum for. 2. Based on the user's choice of credit card, asks the user for the n digits of the credit card. [Get the input as a string; it's easier to work with the string, so don't convert to an integer.] 3. Using the user's input of the n digits, finds the last digit of...

  • Please write c++ (windows) in simple way and show all the steps with the required output

    please write c++ (windows) in simple way and show all the steps with the required output Write a C++ program that simulates the operation of a simple online banking system The program starts by displaying its main menu as shown in Figure1 splay Account nformatson verity Your credit Card elect vour choice Figure 1 Main menu of the program The menu is composed of the following choices 1. When choice 1 is selected (Display Account information), the program should display...

  • Write java program to check that a (16-digit) credit card number is valid. A valid credit...

    Write java program to check that a (16-digit) credit card number is valid. A valid credit card number will yield a result divisible by 10 when you: Form the sum of all digits. Add to that sum every second digit, starting with the second digit from the right. Then add the number of digits in the second step that are greater than four. The result should be divisible by 10. For example, consider the number 4012 8888 8888 1881. The...

  • Credit card numbers adhere to certain constraints. First, a valid credit card number must have between...

    Credit card numbers adhere to certain constraints. First, a valid credit card number must have between 13 and 16 digits. Second, it must start with one of a fixed number of valid prefixes : 1 • 4 for Visa • 5 for MasterCard • 37 for American Express • 6 for Discover cards In 1954, Hans Peter Luhn of IBM proposed a “checksum” algorithm for validating credit card numbers . 2 The algorithm is useful to determine whether a card...

  • Please do it in c++ please show steps as much as possible to help me understand...

    Please do it in c++ please show steps as much as possible to help me understand the code thank you Credit Card error detection check -> Luhn Algorithm, (http://www.freeformatter.com/credit-card-number-generator-validator.html#cardFormats) From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if the product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT