Sicily 6374

 
 

 素数

 
 
时间限制:1秒    内存限制:256兆

题目描述

数学上,素数(又称质数)指的是一个大于1的自然数,除了1和此整数自身外,没法被其他自然数整除的数。即素数只有两个正因数:1和自身。2是第一个素数。

设计一个判别素数的函数,在主函数中输入一个整数n(n>0),调用这个判别函数,并输出是否为素数的信息(“** is a prime.”, or “** is not a prime.”)。

输入格式

每个case输入一个数n,当输入为-1时,结束。

输出格式

每次case输出一句话:

当n为素数是,输出n is a prime.

反之,输出:n is not a prime.

样例输入

4
7
-1

样例输出

4 is not a prime.
7 is a prime.
 1 // Problem#: 6374
 2 // Submission#: 1632165
 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
 6 #include <stdio.h>
 7 
 8 
 9 int Prime ( int n ) ;
10 
11 int main()
12 {
13     int n ;
14     
15     while ( scanf("%d",&n),n != -1) 
16      { 
17      Prime ( n );
18      }
19     
20     return 0;
21 }    
22 int Prime ( int n ) 
23 {  
24     int i , temp ;
25      if ( n == 2 ) 
26      printf ("%d is a prime.\n",n);
27      else if ( n==1 )
28      printf ("%d is not a prime.\n",n);   
29     else 
30      {
31    for ( i = 2 ; i <= (n-1) ; i++ )
32          {
33               temp = n % i ;
34               
35               if ( temp != 0 )
36               {
37                    continue;
38               }
39               else 
40                    {
41                return  printf ("%d is not a prime.\n",n);
42                    } 
43          }     
44 
45        if ( temp != 0)
46          {
47          return printf ("%d is a prime.\n",n);
48          } 
49      }
50 }                                 

 

转载于:https://www.cnblogs.com/Gawaine/archive/2012/11/07/2759630.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值