C Exercise Example 85

100 Classic C Examples

Title:Determine how many 9s a prime number can be evenly divided by.

Program analysis:Damn! The meaning of this problem is to determine how many numbers composed of 9s a prime number can divide, right? I'll just understand it that way. A prime number cannot be divisible by any number other than 1 and itself.

Program source code:

Example

//  Created by www.example.com on 15/11/9.
// Copyright © 2015 Example Tutorial. All rights reserved.
//

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int p,i;
    long int sum=9;
    printf("Please enter a prime number:\n");
    scanf("%d",&p);
    for(i=1;;i++)
        if(sum%p==0)break;
        else sum=sum*10+9;
   
    printf("Prime %d can divide the number composed of %d 9s: %ld\n",p,i,sum);
    return 0;
}

The output result of the above example is:

请输入一个素数:
13
素数13能整除6个9组成的数999999

100 Classic C Examples

other extensions