-1

DESIRED OUTPUT:
Input Value to find factors for? 10
The number 10 has 1,2,5 and 10 as its factors
The number 10 has 4 factors
Try again y/n ?

CURRENT OUTPUT:
Input value to find factors for ? 10
The number 10 has 1 and 10 as it factors
The number 10 has 2 and 10 as it factors
The number 10 has 5 and 10 as it factors
The number 10 has 10 and 10 as it factors
The number 10 has -1 and 10 as it factors
The number 10 has -2 and 10 as it factors
The number 10 has -5 and 10 as it factors
The number 10 has -10 and 10 as it factors

My Current Code

#include <stdio.h> // printf, scanf, getchar
#include <stdlib.h> // system

int list_mult(int value);
int main()
{
    int input_value;
    char again;
    do {
        printf("Input value to find factors for ? ");
        scanf("%d", &input_value);
        list_mult(input_value);
        printf("\nTry again y/n ? ");
        scanf(" %c", &again);
    } while (again == 'y');
}

int list_mult(value) {
    int i;
    int count = 0;
    printf("\nThe number %d has", value);
    for (i = 1; i <= (value / 2); i++)
    {
        if (value%i == 0)
        {
            printf(" %d,", i);
            count++;
        }
    }
    printf(" and %d as it factors", value);
    printf("\nThe number %d has %d factors", value, count);
    return(count + 1);
}

Problems:
1st : do-while loop (again) not work
2nd: "The number 10 has 1,2,5 and 10 as its factors" . Do not know how to show "i" separate by commas.
3rd: Last print statement (Count) does not work

Edit: all above Problem is solved. And the above code works perfectly on CodeBlock

The problem I got now is I use CodeBlock and my code works perfectly. But when I use Microsoft Studio at School (Iam only allowed to use MS at school), MS shows Error as below

C2065 'value': undeclared identifier - Line 29
C2365 'list_mult': redefinition; previous definition was 'function' - Line 29
C2448 'list_mult': function-style initializer appears to be a function definition

Edit2: Compiler Error Solved. Everything works greatly now. Thank you

  • 2
    Looks like an infinite loop to me. If `value` is 10 and never modified inside the loop, when will the expression `1 <= (value/2)` be false? Is that `1` a typo and supposed to be an `i` instead? – MFisherKDX Jan 25 '18 at 04:07
  • OMG! my bad! Thank a lot man. This solves 3rd problem. But the 1st and 2nd problem still remain. – James Macathy Jan 25 '18 at 04:11
  • 1
    For problem 2, what if you print "The number has" before the loop, print each factor you find inside the loop, and print "as its factors\n" after the loop? – MFisherKDX Jan 25 '18 at 04:11
  • 1
    `scanf(" %c",&again);` note the space.. – David C. Rankin Jan 25 '18 at 04:12
  • 1
    For problem 1) see https://stackoverflow.com/questions/13542055/how-to-do-scanf-for-single-char-in-c – MFisherKDX Jan 25 '18 at 04:15
  • @MFisherKDX u r genuine! to David thanks a lot man. Many thanks All, it costed me nearly 3 hours and you guys save my sleep tonight. – James Macathy Jan 25 '18 at 04:24
  • I'm voting to close this question as off-topic because OP deleted the code after getting an answer, to avoid having his professor find out that someone else solved the problem. This makes the question useless for future visitors. – Michael Geary Jan 25 '18 at 04:35
  • I dont think I've ever heard of a professor googling all of his students code. They've got better things to do and I doubt the TA's care. You're fine – Mitch Jan 25 '18 at 04:36
  • 3
    Just to be clear, I have no objection to anyone asking for help with a homework problem. It's the deleting of code that bothers me. What little is left of the question has no value to anyone else. @JamesMacathy if you will edit the question to put the code back in, I'll retract my close vote and remove my annoying comments. :-) – Michael Geary Jan 25 '18 at 04:38
  • 1
    Do not destroy a question after you've received help. Do not ask questions on Stack Overflow if you're not allowed to ask for help. – Jonathan Leffler Jan 25 '18 at 05:06
  • @All ok I got it. sorry abt that – James Macathy Jan 25 '18 at 17:18

1 Answers1

1

After your first scanf for &again, you can put another line in. I had to catch a carriage return on my system.

if (again == '\n') scanf("%c", &again);

For the second problem, you can display the commas like this:

printf("\nThe number %d has ", value);

for (i=1; i<=(value/2); i++) {

  if(value%i==0) {

   printf("%d", i);
   putchar(',');

   count++;
 }
}

printf(" and %d as its factors.", value);

Another way to do the comma formatting would be to make an array of values

int factors[1000];

and pre-fill everything, then iterate through the values to get the formatted output you want. For example you'll know not to print a final comma because you have the count of factors at that point, along with all the factors in the pre filled array.

The number 10 has 1,2,5, and 10 as its factors.
 The number 10 has 3 factors
Try again y/n ?
Snohdo
  • 156
  • 5
  • If you're going to output the comma after the number unconditionally, you can use `printf("%d,", i);` instead of using both `printf()` and `putchar()`. OTOH, it is often effective to use a technique like: `const char *pad = ""; for (…) { if (…) { printf("%s%d", pad, i); pad = ", "; count++; } }` to output an empty pad string before the first number, and a comma-space (or just comma) before every subsequent number. – Jonathan Leffler Jan 25 '18 at 05:12
  • @Snohdo and Jonathan Thank you for ur help. I followed MFisherKDX advice and my code now works greatly on CodeBlock. But it having problems with Microsoft Studio. (problems I wrote on Question in the end). Wonder if you can give me ideas to solve it. Thank you – James Macathy Jan 25 '18 at 17:29