读书笔记——《C Primer Plus》: 一个例程divisors.c

来源:互联网 发布:怎么样建一个淘宝网店 编辑:程序博客网 时间:2024/05/22 01:54

这是一个求整数的约数的例程,觉得不错,做个记号

Listing 7.5. The divisors.c Program
// divisors.c -- nested ifs display divisors of a number#include <stdio.h>#include <stdbool.h>int main(void){    unsigned long num;          // number to be checked    unsigned long div;          // potential divisors    bool isPrime;               // prime flag    printf("Please enter an integer for analysis; ");    printf("Enter q to quit./n");    while (scanf("%lu", &num) == 1)    {        for (div = 2, isPrime= true; (div * div) <= num; div++)        {            if (num % div == 0)            {                if ((div * div) != num)                printf("%lu is divisible by %lu and %lu./n",                        num, div, num / div);                else                    printf("%lu is divisible by %lu./n",                           num, div);                isPrime= false; // number is not prime            }        }        if (isPrime)            printf("%lu is prime./n", num);        printf("Please enter another integer for analysis; ");        printf("Enter q to quit./n");    }    printf("Bye./n");    return 0;}