A. C語言 在1~n個整數求迴文數 個數
#include <stdio.h>
int main(void)
{
int n, x, temp, t, sum = 0, i;
scanf("%d", &i);
//for(n=1;n<i;n++)
for (n = 0;n <= i;n++) //因為0也是迴文數..所以應該從零開始, n<=i,包括自己之內的迴文數
{
temp = 0;
x = n;
do
{
t = x % 10;
temp = temp * 10 + t;
x = x / 10;
}
while (x > 0);
if (temp == n)
sum++;
}
printf("\n sum=%d\n", sum);
}
1000以內的迴文數
在自然數中,最小的迴文數是0,其次是1,2,3,4,5,6,7,8,9,11,22,33,44,55,66,77,88,99,101,111,121,131,141,151,161,171,181,191,202,212,222,232,242,252,262,272,282,292,303,313,323,333,343,353,363,373,383,393,404,414,424,434,444,454,464,474,484,494,505,515,525,535,545,555,565,575,585,595,606,616,626,636,646,656,666,676,686,696,707,717,727,737,747,757,767,777,787,797,808,818,828,838,848,858,868,878,888,898,909,919,929,939,949,959,969,979,989,999.
B. C語言輸入正整數n,輸出1~n,每行一個
#include<stdio.h>
intmain()
{
inti,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d ",i);
}
C. C語言求1到n所有質數
按照題目要求編寫的程序如下(見圖)