我想为每10个输入分离程序的输出(打印)。代码是找到从1到n的素数和它们的中位数。
我只想将每10个n的打印分开:
primes from 1 to 10:
from 10 to 20:
from 20 to 30:
你能帮帮我吗?
#include <stdio.h>
int main()
{
int n, x, y, p, sum, sumx;
float arm;
sum = 0, sumx = 0;
printf("\n Enter the maximum value of n =");
scanf("%d", &n);
printf("\n All prime numbers from 1 to %d are: ", n);
for (x = 2; x <= n; x++)
{
p = 1;
for (y = 2; y <= x / 2; y++)
{
if (x % y == 0)
{
p = 0;
break;
}
}
if (p == 1)
{
sum += x;
}
if (p == 1 && x != 1)
{
printf("%d ", x);
sumx++;
arm = (sum * 1.0) / sumx;
}
}
printf("\nThe sum of all Prime numbers from 1 to %d =%d", n, sum);
printf("\nTotal prime numbers is %d", sumx);
printf("\nThe median of all Prime numbers from 1 to %d is %.2f", n, arm);
return 0;
}
答案 0 :(得分:1)
你可以打印出#34;从_到_:&#34;每当你达到x可被10整除的值时:
#include <stdio.h>
int main()
{
int n, x, y, p, sum, sumx;
float arm;
sum = 0, sumx = 0;
printf("\n Enter the maximum value of n =");
scanf("%d", &n);
printf("\n All prime numbers from 1 to %d are: ", n);
int upper = n < 10? n : 10;
printf("\nFrom 1 to %d: ", upper);
for (x = 2; x <= n; x++)
{
p = 1;
for (y = 2; y <= x / 2; y++)
{
if (x % y == 0)
{
p = 0;
break;
}
}
if (p == 1)
{
sum += x;
}
if (p == 1 && x != 1)
{
printf("%d ", x);
sumx++;
arm = (sum * 1.0) / sumx;
}
if (x % 10 == 0 && x != n) {
int upper = (x + 10) > n? n : (x + 10);
printf("\nFrom %d to %d: ", x + 1, upper);
}
}
printf("\nThe sum of all Prime numbers from 1 to %d =%d", n, sum);
printf("\nTotal prime numbers is %d", sumx);
printf("\nThe median of all Prime numbers from 1 to %d is %.2f", n, arm);
return 0;
}
这会产生如下输出:
输入最大值n = 29
从1到29的所有素数都是:
从1到10:2 3 5 7
从11到20:11 13 17 19
从21到29:23 29
所有素数的总和从1到29 = 129
总素数为10
从1到29的所有Prime数的中值是12.90
答案 1 :(得分:0)
我无法清楚地理解你的问题,但是如果你想分开10到20或20到30之间的素数的行,那么你可以在你的循环中添加它
if(x%10 == 0)
printf("\n");
或者,如果你想在一行中想要10个素数,那么
if(sumx%10 == 0)
printf("\n");