The revolutionary project management tool is here! Plan visually with a single glance and make sure your projects get done.

My program will crash and I can't seem to figure out what I did wrong. The code is attached. My goal here is to list all of those factors that I have added to the array I created. Then at the end of the program I need to print each factor, one by one.

I am using the Dev C++ compiler. Thank you very much

I am using the Dev C++ compiler. Thank you very much

```
/*
John Doe
COP 3223-0001 LEC (16050)
Assignment #2 Aid to an age calculator
Due: February 12, 2009
This program is designed to calculate how much you owe
on your student loan after each monthly payment.
*/
//load library that contains printf function
#include <stdio.h>
int main(void)
{
int posInt;
int i, j, k, sum, product;
//Gather user input
again:
printf("Enter a positive integer greater than 1. (Ex 1, 4, 100)\n");
scanf("%d", &posInt);
//conditional statement testing if positive integer
if(posInt > 0)
{
//run function to calc factors
printf("Good works\n");
}
else if(posInt <= 0)
{
printf("Please enter a valid positive integer!\n");
goto again;
}
int factors[posInt];
j = 0;
sum = 0;
product = 0;
for(i=0; i <= posInt; i++)
{
if(posInt % i == 0)
{
factors[j] = i;
sum += i;
product *= i;
j++;
}
}
printf("Here are a list of positive factors of %d:\n", posInt);
for(k=0; k <= j; k++)
{
printf("%d ", factors[k]);
}
//print sum and product of factors
printf("The number of positive factors of %d is %d.\n", posInt, j);
printf("Here is the sum of the factors of %d: %d\n", posInt, product);
printf("Here is the product of the factors of %d: %d\n", posInt, product);
system("pause");
return 0;
}
```

In your code you are using system("pause");

so that you need to add a header file,

```
#include <windows.h>
```

int factors[posInt];

insted you can declare int factors[100] ;

something like this,

and the problem in modulus operator this is your major probelm

and in for loop check that out find out the answer

hope this helps

{

printf("%d ", factors[k]);

}

when k==j, you print an element of factors that has never been set

when you start with product = 0, product *= i; will never change it

in

printf("Here is the sum of the factors of %d: %d\n", posInt, product);

the text does not describe what you are printing

if you enter too large a number,

int factors[posInt];

will fail

>>

>> so that you need to add a header file,

>>

>> #include <windows.h>

The system function is a standard C function and is in the standard <stdlib.h> header. No need to include <windows.h> or any other platform dependent headers.

It closes unexpectedly only when assigning the array values for valid factors. The % (modulus) is being used correctly.

second; the result of the % operator is the remainder. In both operations, if the value of

the second operand is zero, the behavior is undefined.

undefined behavior

behavior, upon use of a nonportable or erroneous program construct or of erroneous data,

for which this International Standard imposes no requirements

NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable

results, to behaving during translation or program execution in a documented manner characteristic of the

environment (with or without the issuance of a diagnostic message), to terminating a translation or

execution (with the issuance of a diagnostic message).

I changed you code in this way::

added #include <windows.h> in first

and changed

int factors[posInt];

to

int *factors = (int*)malloc(posInt);

and then added

free(factors);

before return.

now, your code works well :D

regards...

```
#include <stdio.h>
#include <windows.h>
int main(void)
{
int posInt;
int i, j, k, sum, product;
//Gather user input
again:
printf("Enter a positive integer greater than 1. (Ex 1, 4, 100)\n");
scanf("%d", &posInt);
//conditional statement testing if positive integer
if(posInt > 0)
{
//run function to calc factors
printf("Good works\n");
}
else if(posInt <= 0)
{
printf("Please enter a valid positive integer!\n");
goto again;
}
int *factors = (int*)malloc(posInt);
j = 0;
sum = 0;
product = 0;
for(i=0; i <= posInt; i++)
{
if(posInt % i == 0)
{
factors[j] = i;
sum += i;
product *= i;
j++;
}
}
printf("Here are a list of positive factors of %d:\n", posInt);
for(k=0; k <= j; k++)
{
printf("%d ", factors[k]);
}
//print sum and product of factors
printf("The number of positive factors of %d is %d.\n", posInt, j);
printf("Here is the sum of the factors of %d: %d\n", posInt, product);
printf("Here is the product of the factors of %d: %d\n", posInt, product);
system("pause");
free(factors);
return 0;
}
```

now your app works well

regards...

```
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(void)
{
int posInt;
int i, j, k, sum, product;
//Gather user input
again:
printf("Enter a positive integer greater than 1. (Ex 1, 4, 100)\n");
scanf("%d", &posInt);
//conditional statement testing if positive integer
if(posInt > 0)
{
//run function to calc factors
printf("Good works\n");
}
else if(posInt <= 0)
{
printf("Please enter a valid positive integer!\n");
goto again;
}
int *factors = (int*)malloc(posInt);
j = 0;
sum = 0;
product = 1;
for(i=1; i <= posInt; i++)
{
if(posInt % i == 0)
{
factors[j] = i;
sum += i;
product *= i;
j++;
}
}
printf("Here are a list of positive factors of %d:\n", j);
for(k=0; k < j; k++)
{
printf("%d ", factors[k]);
}
//print sum and product of factors
printf("\nThe number of positive factors of %d is %d.\n", posInt, j);
printf("Here is the sum of the factors of %d: %d\n", posInt, sum);
printf("Here is the product of the factors of %d: %d\n", posInt, product);
printf("Press any key to continue . . .");
getch();
free(factors);
return 0;
}
```

works.JPG
All Courses

From novice to tech pro — start learning today.