C language help?

Write an entire C program that reads a positive integer entered by an interactive user and then prints out all the positive divisors of that integer in a column and in decreasing order.

The program should allow the user to repeat this process as many times as the user likes.

Initially, the program should inform the user about how the program will behave. Then the program should prompt the user for each integer that the user wishes to enter. The program may be terminated in any of two ways. One way is to have the program halt if the user enters an integer that's negative or zero. In this case the user should be reminded with each prompt that the program can be terminated in that way. Alternatively, after an integer has been entered and the divisors have been printed, the program can ask the user whether he/she wishes to enter another integer. In this case, when the user accidentally enters a zero or negative integer to have its divisors calculated, the program should inform the user that the input is unacceptable and should allow the user to try again (and again!).

What I have so far :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <ctype.h>
int main(void) {
int number, i;
char yn;
do{
do{
printf("Please enter a positive integer: ");
scanf("%d",&number);
if(number<=0)
printf("not a positive number!\n");
}while(number<=0);

printf("Results: ", number);
for(i=1; i <= number; ++i) {
if (number%i == 0)
printf("%d ",i);
}
printf("\n");
printf ("Would you like to see the divisors of another integer (Y/N)? ")
scanf(" %c",&yn);
yn=toupper(yn);
}while(yn=='Y');
return 0;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <ctype.h>

int main(void)
{
    int number, i;
    char yn;
    do
    {
        do
        {
            printf("Please enter a positive integer: ");

            number = -5 ; // *** added to avoid undefined behaviour if scanf fails
            // scanf("%d",&number);
            if( scanf("%d",&number) != 1 ) // *** if scanf failed
            {
                puts( "you did not enter a number" ) ; // *** inform the user
                while( getchar() != '\n' ) ; // *** extract and discard the bad input
                // note: for now, we will ignore the possibility of EOF on stdin
            }
            // if(number<=0) // ***
            else if( number < 1 ) // *** zero is non-negative, but not positive
                printf("not a positive number!\n");
        }
        while(number<=0);

        // printf("Results: ", number); // ****
        printf("Results: %d\n", number); // *** %d
        for(i=1; i <= number; ++i)
        {
            if (number%i == 0)
                printf("%d ",i);
        }
        printf("\n");
        // printf ("Would you like to see the divisors of another integer (Y/N)? ")
        printf ("Would you like to see the divisors of another integer (Y/N)? ") ; // *** semicolon
        scanf(" %c",&yn);
        yn=toupper(yn);
    }
    while(yn=='Y');
    return 0;
} // ****  


Consider factoring the code into functions. For example,
a function to read a positive integer from stdin
a function to print the integral divisors of a positive integer
etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

int get_positive_int()
{
    printf("Please enter a positive integer: ");

    int number = -5 ;

    if( scanf( "%d", &number ) != 1 )
    {
        puts( "you did not enter a number" ) ;
        while( getchar() != '\n' ) ;
    }
    else if( number > 0 ) return number ;

    else puts( "not a positive number!" );

    return get_positive_int() ; // try again
}

void print_integral_divisors( int number )
{
    printf("positive integral divisors of: %d\n", number );
    for( int i=1; i <= number; ++i )
        if (number%i == 0) printf("%d ",i);
    puts("");
}

bool again()
{
    printf ("Would you like to see the divisors of another integer (Y/N)? ") ;
    char yn = 'n' ;
    scanf(" %c",&yn);
    return toupper(yn) == 'Y' ;
}

int main()
{
    do
    {
        const int number = get_positive_int() ;
        print_integral_divisors(number) ;

    } while( again() ) ;
}
Last edited on
Topic archived. No new replies allowed.