169: Make a Credit Card Validator Program in C++ (1. Double every second digit from right to left, If doubled number is 2 digits, split them (9 x 2 = 18, 1 and 8).) (2. Add all single digits from step 1.) (3. Add all odd numbered didgits from right to left.) (4. Sum results from steps 2 & 3.) (5. If step 4 is divisible by 10, credit card # is valid.)


// Make a Credit Card Validator Program in C++

// (1. Double every second digit from right to left, If doubled number is 2 digits,
//     split them (9 x 2 = 18, 1 and 8).)
// (2. Add all single digits from step 1.)
// (3. Add all odd numbered didgits from right to left.)
// (4. Sum results from steps 2 & 3.)
// (5. If step 4 is divisible by 10, credit card # is valid.)

// 4032-6426-9444-5720

#include <iostream>
using namespace std;
bool isValid(string x);
int even_sum(string x, int size); // get sum of double of even numbers and broken values from rigth to left
int odd_sum(string x, int size);  // get sum of odd values from right to left
int get_num(string x, int i);     // gets number at position i of string

int main()
{
    string card;
    cout << "Enter your credit card number (without spaces): ";
    cin >> card;

    if (isValid(card))
    {
        cout << card << " is Valid number\n";
    }
    else
    {
        cout << card << " is Invalid number\n";
    }

    return 0;
}
bool isValid(string x)
{
    int size = x.size();             // passing size for iterating over the array
    return ((even_sum(x, size) + odd_sum(x, size)) % 10 == 0); // (even_sum + odd_sum)%10==0
}
int even_sum(string x, int size)
{
    int sum = 0;
    for (int i = size - 2; i >= 0; i -= 2)  // starting from 2nd number from right side
    {
        int y = get_num(x, i);     // getting int value at position i
        y *= 2;                    // multiplying by 2 (doubling)
        if (y < 10)
        {
            sum += y;
        }
        else
        {
            sum += ((y / 10) + (y % 10));   // If doubled number is 2 digits separate them and sum
        }
    }
    return sum;
}
int odd_sum(string x, int size)
{
    int sum = 0;
    for (int i = size - 1; i >= 0; i -= 2)   // starting from 1st number from right side
    {
        sum += get_num(x, i);               // getting number at i and sum
    }
    return sum;
}
int get_num(string x, int i)
{
    return (x.at(i) - '0');         // minusing ascii value of '0' from ascii of number at i
}


Comments

Popular posts from this blog

153: Write a program to read an amount (integer value) and break the amount into smallest possible number of bank notes. Note: The possible banknotes are 500, 100, 50, 20, 10, 5, 2, and 1

206: Write a program to create a class named "Circle" which has the property "radius". Define functions to calculate the area and circumference of the circle.

221: // In Task 2, we discussed multilevel inheritance with parameterized constructors for Student, UndergraduateStudent, and GraduateStudent classes in a university management system. Can you explain the advantages of using multilevel inheritance with specific details about the functions and data members in these classes? How were the parameterized constructors (e.g., setting student name, age, and ID) used to ensure that each class in the hierarchy correctly initializes its properties, such as creating an UndergraduateStudent named "John," aged 20, with a student ID of 12345