gohanhango
New Coder
I'm tackling the credit problem from cs50 and I'm baffled by the for loop on one section because I'm dividing a long integer 4003600000000014 by 100 and it returns a large negative number -1685133312.
Here is the actual code:
I'm tackling the credit problem from cs50 and I'm baffled by the for loop on one section because I'm dividing a long integer 4003600000000014 by 100 and it returns a large negative number -1685133312.
Here is the actual code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
long int number;
long int temp;
do
{
number = get_long("Number: ");
} while (number < 0);
temp = number;
int counter = 1;
for (int i = 10; i <= number; i = 10)
{
number /= i;
counter += 1;
}
printf("%i\n", counter);
int product = 0;
int divisor = 100;
int modulo = 0;
//printf("%li\n", (temp % 100) / 10);
for (int i = 0; i < counter / 2; i++)
{
modulo = temp / divisor;
divisor *= 100;
product += (2 * (modulo % 10));
}
printf("%i\n", product);
}
for (int i = 0; i < counter / 2; i++)
{
modulo = temp / divisor;
divisor *= 100;
product += (2 * (modulo % 10));
}
Here is the portion where the division occurs.
What could be the problem?
Here is the actual code:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
long int number;
long int temp;
do
{
number = get_long("Number: ");
} while (number < 0);
temp = number;
int counter = 1;
for (int i = 10; i <= number; i = 10)
{
number /= i;
counter += 1;
}
printf("%i\n", counter);
int product = 0;
int divisor = 100;
int modulo = 0;
//printf("%li\n", (temp % 100) / 10);
for (int i = 0; i < counter / 2; i++)
{
modulo = temp / divisor;
divisor *= 100;
product += (2 * (modulo % 10));
}
printf("%i\n", product);
}
Here is the actual code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
long int number;
long int temp;
do
{
number = get_long("Number: ");
} while (number < 0);
temp = number;
int counter = 1;
for (int i = 10; i <= number; i = 10)
{
number /= i;
counter += 1;
}
printf("%i\n", counter);
int product = 0;
int divisor = 100;
int modulo = 0;
//printf("%li\n", (temp % 100) / 10);
for (int i = 0; i < counter / 2; i++)
{
modulo = temp / divisor;
divisor *= 100;
product += (2 * (modulo % 10));
}
printf("%i\n", product);
}
for (int i = 0; i < counter / 2; i++)
{
modulo = temp / divisor;
divisor *= 100;
product += (2 * (modulo % 10));
}
Here is the portion where the division occurs.
Code:
for (int i = 0; i < counter / 2; i++)
{
modulo = temp / divisor;
divisor *= 100;
product += (2 * (modulo % 10));
}