I'm working on pset1 credit. To identify the valid credit card number. First I ask users to input a long long, which I made it a int so that i can make sure it fit the first requirement(any number > 0). Then I want to identify each of the digits from small to large(right to left). To do this, I use a for loop from 0 to 16(temporary).In each loop, I divide the input by 10 ^ i first, then use % to get the remain divided by 10 ^ (i + 1). Last, I print them all to make sure I get them all.
but Not only I got the wrong number, but also got a error message runtime error: division by zero Floating point exception
I'm wonder which part did I do wrong, or I shouldn't use this method to solve this pset.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main(void)
{
printf("Card number: ");
printf("\n");
long long cn = get_long_long();
int n = cn;
if (n <= 0)
{
printf("INVALID\n");
}
else
{
float each;
for (int i = 0; i < 16; i++)
{
each = (n / (10 ^ i)) % (10 ^ (i + 1));//the i_th digits
printf("%f\n", each);
}
//int nth = (n / 10) % 10;
//printf("%i\n", nth);
}
}