Book of Potion making

Harry wants to find Voldemort's potion-making book but he is confused about how to get it.

The book has a special ISBN(International Standard Book Number) which is  unique numeric book identifier only for Voldemort's book printed on it. The ISBN is based upon a 10-digit code. The ISBN is valid if:
1xdigit1 + 2xdigit2 + 3xdigit3 + 4xdigit4 + 5xdigit5 + 6xdigit6 + 7xdigit7 + 8xdigit8 + 9xdigit9 + 10xdigit10 is divisible by 11.
Help Harry to find the book!

Example:

Input:  n = 1401601499
Output: Legal ISBN

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void potionMaking(long long n)
{
    int res = 0cnt = 0;
    int i = 10;
    while (n)
    {
        int x = n % 10;
        res += x * i;
        n = n / 10;
        i--;
        cnt++;
    }
    if (cnt < 10)
        cout << "Illegal ISBN\n";
    else if (res % 11 == 0)
        cout << "Legal ISBN\n";
    else
        cout << "Illegal ISBN\n";
}
int main()
{
    long long n = 1401601499;

    potionMaking(n);

    return 0;
}


No comments:

Post a Comment