目录
Java输出n以内的素数判断素数C语言输出n以内的素数判断素数Java
输出n以内的素数
//判断素数方法public static void isPrime(int n){int i,j;for(i = 2; i <= n; i++){boolean flag = true;for(j = 2; j < i; j++){if(i % j == 0){flag = false;break;}}if(flag){System.out.print(i + " ");}}}public static void main(String[] args){Scanner sc = new Scanner(System.in);int n = sc.nextInt();//输入一个数isPrime(n);}
结果截图
判断素数
方法一:
public static void isPrime(int n){boolean flag = false;for(int i = 2; i < n; i++){flag = true;if(n % i == 0){flag = false;break;}}if(flag){System.out.println("是素数!");}else{System.out.println("不是素数!");}}public static void main(String[] args){Scanner sc = new Scanner(System.in);int n = sc.nextInt();//输入一个数isPrime(n);}
方法二:
public static void isPrime(int n){boolean flag = false;for(int i = 2; i < Math.sqrt(n); i++){//平方n ,优化代码flag = true;if(n % i == 0){flag = false;break;}}if(flag){System.out.println("是素数!");}else{System.out.println("不是素数!");}}public static void main(String[] args){Scanner sc = new Scanner(System.in);int n = sc.nextInt();//输入一个数isPrime(n);}
运行截图:
C语言
输出n以内的素数
方法一:
//判断素数函数#include<stdio.h>void isprime(int n){int i, j;for (i = 2; i < n; i++){for (j = 2; j < i; j++){if (i % j == 0){j = 0;break;}}if (j != 0){printf("%d ", i);}}}int main(){int n = 0;scanf("%d", &n);isprime(n);return 0;}
方法二:
#include<stdio.h>void isprime(int n){int i, j;int flag = 0;for (i = 2; i < n; i++){flag = 1;for (j = 2; j < i; j++){if (i % j == 0){flag = 0;break;}}if (flag){printf("%d ", i);}}}int main(){int n = 0;scanf("%d", &n);//输入一个数isprime(n);return 0;}
运行截图:
判断素数
方法一:
#include<stdio.h>//判断素数函数void isprime(int n){int i;int flag = 0;for (i = 2; i < n; i++){flag = 1;if (n % i == 0){flag = 0;break;}}if (flag == 1){printf("是素数!\n");}else{printf("不是素数!\n");}}int main(){int n = 0;scanf("%d", &n);isprime(n);return 0;}
方法二:
#include<stdio.h>#include<math.h>void isprime(int n){int i;int flag = 0;for (i = 2; i < sqrt(n); i++)//平方n,优化代码{flag = 1;if (n % i == 0){flag = 0;break;}}if (flag == 1){printf("是素数!\n");}else{printf("不是素数!\n");}}int main(){int n = 0;scanf("%d", &n);isprime(n);return 0;}
运行截图