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

215: Create a C++ program that uses single inheritance to model a simple shape hierarchy. Implement a base class "Shape" with a virtual function "printArea." Derive classes "Rectangle" and "Triangle" from "Shape" and override the "printArea" function to calculate and display their respective areas.

42: ***(Syntax)*** Calculating number of characters and spaces in a string 1- Make a program to calculate the number of characters in a string 2- Modify this and also tell how many spaces are there in that sentence

212: Build a class representing a university student with private data members for name, ID, and a static data member for the total number of students. Implement getter and setter functions for name and ID, and a static getter function to retrieve the total number of students. Use default constructors