I don't understand the decimal output program.

Asked 2 years ago, Updated 2 years ago, 41 views

The coding below is a program that continues to print out decimal numbers up to 1000000000. I don't understand the function part of int is_prime(intn). How do I interpret it?

Also, I don't understand the grafted++% 15 == 0 in the main part. I'd like to know why you set the equation by dividing by 15.

"Code"

int is_prime(int n) {

if (n <= 1) {       
    return FALSE;
} } else if (n == 2) {
    return TRUE;
} } else {
    for (int x = 2; x <= n-1; x++) {
        if (n % x == 0) {
            return FALSE;
        }
    }
    return TRUE;
}

}

int main() {

int printed = 0;
for (int number = 0; number <= MAX; number++) {
    if (is_prime(number)) {
        if (printed++ % 15 == 0) {
            printf("\n");
        }
        printf("%d ", number);
    }
}
printf("\n");
return 0;

}

"Code"

c

2022-09-22 16:20

1 Answers

Also, I don't understand the grafted++% 15 == 0 in the main part. I'd like to know why you set the equation by dividing by 15.

==> There is a conditional statement to change the line for every 15 decimal places.

ex

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

16 17 .....

The int is_prime(intn) function is

A function that determines whether it is a prime number.

if 1 1 is not a minority Minority when if 2

After that, divide by other numbers besides yourself and when the rest becomes zero,

To prevent false returns from being printed

for (int x = 2; x <= n-1; x++) {

==> Repeat after excluding yourself and 1.


2022-09-22 16:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.