At least show me the logic on how to solve the math problem parts. I will put it into program if you just explain to me how to get the factors, tell the computer how many factors, find the sum and print it to screen, and find the product and print it to screen. Then i will put it all together.

Hi, im kinda new to using loops in my programming. I'm still a little rusty. could someone hep me out a little on this problem?
I sure could use some quick help from you guys. You've always been there in the past. Thanks so much.

I need a program that will prompt the user for a single positive integer greater than 1. If the user does not enter a valid integer, then the program should continue to reprompt the user for a value until a valid integer is entered. Once the integer is read in, the program will print out the following information about the integer:

  1. A list of each of the positive factors of the given integer.
  2. The number of factors the given integer has.
  3. The sum of the factors of the integer.
  4. The product of the factors of the integer.

An example output would be :

Enter a positive integer greater than one.
12

Here is a list of the positive factors of 12:
1 2 3 4 6 12

The number of positive factors of 12 is 6.
The sum of the positive factors of 12 is 28.
The product of the positive factors of 12 is 1728.

thanks so much guys.

Heres what i got so far :

int main()

{

       int LoopCount = 0;
       int integer;


       printf("Enter a positive integer greater than one.\n";
       scanf("%d", integer);


    for (int j=1; j <= LoopCount; j++){
               for (int integer=1; integer <=LoopCount; integer++){

Recommended Answers

All 3 Replies

user enters NUMBER
for loop counting X from NUMBER to 1 counting backwards
if ( (NUMBER % X) == 0 ) then X is a factor
print X to the screen
continue looping


In the above algorithm, we use modular division. (e.g. number % x = number mod x) What modular division does, is it gives you the remainder when regular division is performed. Take the following example.

NUMBER = 12

x = 12
12/12=1
12%12=0
12 is a factor

x = 11
12/11=...
12%11=.....
11 is not a factor
.
.
.
x= 6
12/6=2
12%6=0
6 is a factor

so how would i put this into code? I also have to say how many factors there are, anf the sum and product of the factors.

why do i get this compiling error with the following code??

numbers.c: In function `main':
numbers.c:33: `for' loop initial declaration used outside C99 mode

int main()  {
   int oneNumber =0;            // hold the valid integer entered
   int countPositiveFactors =0; // accumulate a sum , so should be initialize to 0
   int sumPositiveFactors =0;    // accumulate a sum , so should be initialize to 0
   int prodPositiveFactors = 1; // accumulate a product so should be initialize to 1
  
   while (1)
   {
       printf("Enter an integer > 1 :");
       scanf("%d", &oneNumber);
       if (oneNumber > 1)
          break;
       else 
           printf("Error: Number should be > 1\n");
       _flushall(); // to flush all streams e.g. stdin 

   }
   
   printf("\n\nHere is a list of the positive factors of %d\n",oneNumber);
   for (int i=1;i<=oneNumber;i++)
   {
       if (oneNumber%i==0) // one factor found
       {
           countPositiveFactors++; // add 1 to count of positive factors
           printf("%d ",i);        // print this factor
           sumPositiveFactors+=i;  // add this factor e.g. i to the sum
           prodPositiveFactors*=i; //  multiply this factor with the previous factors

       }
   }
   
   printf ("\n\nThe number of positive factors of %d is %d.\n",oneNumber, countPositiveFactors);
   printf ("The sum of the positive factors of %d is %d.\n",oneNumber, sumPositiveFactors);
   printf ("The product of the positive factors of %d is %d.\n",oneNumber, prodPositiveFactors);
   
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.