输出100以内奇数,偶数,质数,合数的脚本

奇数
#!/bin/bash

echo " ( 1..100 )内的奇数:"
for i in {1..100};do
A=$(expr $i % 2)
        if [ $A -ne 0 ];then
                echo $i
        fi
done

偶数
#!/bin/bash

echo " (1-100) 内的偶数:"
for i in {1..100};do
A=$(expr $i % 2)
        if [ $A -eq 0 ]
        then
                echo $i
        fi
done

质数
#!/bin/bash
echo " 显示{1.100}质数:"
for i in {1..100};do
        A=$(factor $i |awk '{print NF}')
        if [ $A -eq 1 ] || [ $A -eq 2 ];then
                echo "$i是质数"
        fi
done

合数
#!/bin/bash

for i in {1..100};do
        A=$(factor $i |awk '{print NF}')
        if [ $A -ne 1 ] && [ $A -ne 2 ];then

                echo "$i是合数"
        fi

done
~       


posted @ 2019-05-19 12:26  #赵程#  阅读(917)  评论(0编辑  收藏  举报