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

88: Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: // Percentage >= 90% : Grade A Percentage >= 80% : Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage >= 40% : Grade E Percentage < 40% : Grade F

205: Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year. Write a program to create a catalog of books by taking user input and display books published after a certain year.

15: Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. -_-_-_-_-_-_-_-_-(line with spaces input concept)-_-_-_-_-_-_-_-_