Linux/Bash

bash 스크립트 (숫자의 배수 출력 하기)

워니주니구니 2024. 2. 19. 18:20
728x90
SMALL
  • 개요
Bash script를 사용해서 1에서 100까지 숫자중에 3, 5, 7의 배수를 출력하는 예제이다.

 

  • 예제 1
#!/bin/bash
for i in $(seq 1 100); do
    if (( i % 3 == 0 )); then
        echo "Multiple of 3: $i"
    fi
    if (( i % 5 == 0 )); then
        echo "Multiple of 5: $i"
    fi
    if (( i % 7 == 0 )); then
        echo "Multiple of 7: $i"
    fi
done

 

  • 예제 2
#!/bin/bash
for x in 3 5 7; do
for i in $(seq 1 100); do
    if (( i % $x == 0 )); then
        echo "Multiple of $x : $i"
    fi
done
done
728x90
LIST