write C Sharpe console app NO ARRAYs simple console application Credit card vali
ID: 3792747 • Letter: W
Question
write C Sharpe console app NO ARRAYs
simple console application
Credit card validation Problem. A credit card number must be between 13 and 16 digits. It must start with:
–4 for visa cards
–5 for mater cards
–37 for American express cards
–6 for discover cards
•Consider a credit card number 4380556218442727
•Step1: 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.
•Step2: now add all the single digit numbers from step1
4 + 4 + 8 + 2 + 3 + 1 + 7 + 8
•Step3: add all digits in the odd places from right to left in the card number
7+ 7 + 4 + 8 + 2 + 5 + 0 + 3 = 36
•Step4: sum the results from step 2 and step 3
37 + 36 = 73
•Step 5: if the results from step 4 is divisible by 10, the card number is valid; otherwise, it is invalid. In this case, the card number is not valid – because 75 not divisible by 10. use modulus operator
Explanation / Answer
program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CreditCardValidation
{
class Program
{
static void Main(string[] args)
{
long[] card = null;
Console.WriteLine("enter the credit card no ");
long creditCardNo = Convert.ToInt64(Console.ReadLine());
List<long> listOfInts = new List<long>();
while (creditCardNo > 0)
{
listOfInts.Add(creditCardNo % 10);
creditCardNo = creditCardNo / 10;
}
listOfInts.Reverse();
card= listOfInts.ToArray();
long sum = 0;
for (int i = card.Length - 2; i >= 0;i=i-2)
{
long no = card[i];
Console.WriteLine("the right to left gap between one place no {0}", no);
sum = sum + no;
}
long oddsum = 0;
for (int i = card.Length - 1; i >= 0;--i)
{
if (i % 2 != 0)
{
long no = card[i];
Console.WriteLine("odd places no {0}", no);
oddsum = oddsum + no;
}
}
long finalsum = oddsum + sum;
if (finalsum % 10 == 0)
{
Console.WriteLine("the sum is " + finalsum);
Console.WriteLine("card is valid");
}
else
{
Console.WriteLine("the sum is " + finalsum);
Console.WriteLine("card is not valid");
}
Console.ReadLine();
}//main
}//class
}//namespace
output
enter the credit card no
4380556218442727
the right to left gap between one place no 2
the right to left gap between one place no 2
the right to left gap between one place no 4
the right to left gap between one place no 1
the right to left gap between one place no 6
the right to left gap between one place no 5
the right to left gap between one place no 8
the right to left gap between one place no 4
odd places no 7
odd places no 7
odd places no 4
odd places no 8
odd places no 2
odd places no 5
odd places no 0
odd places no 3
the sum is 68
card is not valid
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.