C 程序在两个区间显示质数
在运行时在控制台输入两个数字。然后声明标志变量,用于借助 for 循环条件检查该数字是否为质数。
每当标志为零时,它将打印质数,而如果标志为 1,则跳出循环。
程序
以下是 C 程序,可在两个区间显示质数−
#include <stdio.h> int main(){ int number1,number2,i,j,flag; printf("enter the two intervals:"); scanf("%d %d",&number1,&number2); printf("prime no’s present in between %d and %d:",number1,number2); for(i=number1+1;i<number2;i++){// interval between two numbers flag=0; for(j=2;j<=i/2;++j){ //checking number is prime or not if(i%j==0){ flag=1; break; } } if(flag==0) printf("%d
",i); } return 0; }
输出
您将看到以下输出 −
enter the two intervals:10 50 the number of prime numbers present in between 10 and 50:11 13 17 19 23 29 31 37 41 43 47
考虑另一个示例,其中,我们尝试去除两个数字之间的质数。
示例
以下是 C 程序,可在两个区间显示排除质数的数字−
#include <stdio.h> int main(){ int number1,number2,i,j,flag; printf("enter the two intervals:"); scanf("%d %d",&number1,&number2); printf("the numbers that are present after removing prime numbers in between %d and %d:
",number1,number2); for(i=number1+1;i<number2;i++){// interval between two numbers flag=1; for(j=2;j<=i/2;++j){ //checking number is prime or not if(i%j==0){ flag=0; break; } } if(flag==0) printf("%d
",i); } return 0; }
输出
您将看到以下输出 −
enter the two intervals:10 20 the numbers that are present after removing prime numbers in between 10 and 20: 12 14 15 16 18
广告