I am currently trying to solve the hacker version of pset 1 (credit.c
). I have successfully completed the rest of the psets (hacker and standard versions). I need to sum up the even (starting from 0) digits or alternate placing digits. E.g., given the number 5677, I need to have a program that will sum up 5 + 7 = 12, since they are even placing. The first digit is when i == 0
.
My idea for this is to get an input from the user using:
Long n;
n = GetLongLong();
int sum;
then using sprintf
to convert my Long n
into a string, s
. After which, I can add the characters of the string (using s[i]
for some i < strlen(s)
) by using:
for (int i=0, i < strlen(s), i++)
{
for (i % 2 == 0)
sum = sum + s[i]
}
But I don't know how to program this into the actual C language, especially the conversion from Long to Strings. Is my design of the program actually correct?