user57603
user57603

Reputation:

How to find the factors of a 3 digit number

I'm writing a mathematical toolkit consisting of various commands. One of the commands I would like to write is for finding the factors of a 3 digit number. Please name the command as “myfactors”. Here’s an example transcript:

$ myfactors abc
abc is not a number. Please enter a number

$ myfactor 72
72 is not a 3 digit number

$ myfactor 105
The factors are: 1 3 5 7 15 21 35 105

Upvotes: 1

Views: 786

Answers (1)

Nischay
Nischay

Reputation: 105

Please check this, I used factor GNU tool available In Ubuntu.

#!/bin/bash
num=$1
if [ "$num" -ge 100 ]
then
factor="`factor $num`"
echo "Factor of number $num is $factor"
else
echo "Enter number is not a 3 digit number"
fi

Or you we make it more restrictive to accept only 3 digit numbers

#!/bin/bash
num=$1
if [ "$num" -ge 100 ] && [ "$num" -lt 1000 ]
then
factor="`factor $num`"
echo "Factor of number $num is $factor"
else
echo "Enter number is not a 3 digit number"
fi

Upvotes: 1

Related Questions