How to find the factors of a 3 digit number - ubuntu-unity

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

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

Related

Trying to create a bash shell craps game. Output loops a particular portion of it and I can't seem to figure out why

Intended Output:
I'm going to pick two numbers, if the sum of those numbers equals 2, 3, or 12...you lose
hmm, I think I'll go with 5 and 4
ya lost, bud
want to try again?
Actual Output:
I'm going to pick two numbers, if the sum of those numbers equals 2, 3, or 12...you lose
hmm, I think I'll go with 5 and 4
So that brings us to a total of: 9
hmm, I think I'll go with 4 and 4
So that brings us to a total of: 8
hmm, I think I'll go with 3 and 1
So that brings us to a total of: 4
hmm, I think I'll go with 1 and 1
So that brings us to a total of: 2
ya lost, bud
want to try again?
Bash Script:
echo "Welcome to the craps machine"
echo "I'm going to pick two numbers, if the sum of those numbers equals 2, 3, or 12...you lose"
flag=1
temp=1
while [ $flag -le 1 ]
do
number_1=$((RANDOM % 6+1))
number_2=$((RANDOM % 6+1))
echo "hmm, I think I'll go with $number_1 and $number_2"
let sum=$number_1+$number_2
echo "So that brings us to a total of: $sum"
if [ "$sum" -eq 2 ] || [ "$sum" -eq 3 ] || [ "$sum" -eq 12 ];then
echo ya lost, bud
echo want to try again?
read flag
fi
if [ "$sum" -eq 7 ] || [ "$sum" -eq 11 ];then
echo Youre a winner, champ
echo you want to play again?
read flag
fi
flag=$temp
done

How to print odd numbers in bash?

for i in {1..100}
do
if [ ($i % 2) -ne 0 ]
then
echo $i
fi
done
Hi! I am learning Bash but I have some probelems with printing the odd numbers in the range 1 to 100, obviously I have some syntax error which I cannot find.
The {x..y} construct allows a 3rd argument to designate the increment value (default is 1), eg:
for i in {1..20..3} # start with 1 and increment by 3 until you reach/pass 20
do
echo $i
done
This generates:
1
4
7
10
13
16
19
For odd vs even you designate the starting number and increment by 2:
# odd numbers
for i in {1..10..2} # start with an odd number and increment by 2
do
echo $i
done
1
3
5
7
9
# even numbers
for i in {2..10..2} # start with an even number and increment by 2
do
echo $i
done
2
4
6
8
10
I think your if statement isn't correct. Here's a small rewrite with a working if:
#!/bin/bash
for i in {1..100}
do
isEvenIfZero=$i%2;
if [[ $isEvenIfZero -ne 0 ]];
then
#echo -n $i #single line
echo $i
fi
done

Bash Scripting, reading an indefinite number of integers and applying formulas

I just started learning bash. I know there are advanced ways of solving this problem but I can't use any advanced methods. I recently finished a lecture on loops and I'm expected to know how to solve this. But after 3+ hours of reading I can't find a solution.
Here's the prompt:
Create a script that will take in x amount of numbers from a user (minimum 5 numbers) via the use of positional variables. Then do the following:
Count how many numbers were inputted
Add up all the numbers
Multiply all the numbers
Find the average of all the numbers
read -p "Enter at least 5 integers"
#Check to make sure integers entered are indeed integers (how?)
#Check to make sure at least 5 integers were entered (how?)
#for total number of integers entered, add into a sum
#for total number of integers entered, find total product
#find average of all integers entered
My main issues are with the checks. Also, how do I assign an indefinite number of values to variables? Thanks for any help. I've looked around but have found no beginner solution to this.
#!/bin/bash
declare -i sum=0
declare -i counter=0
declare -i product=1
if [[ $# -lt 5 ]]
then
echo "Please enter atleast 5 positional parameters"
exit 1
fi
for num in "$#" #$# means all positional parameters, $1, $2, $3 and so on
do
#checking if the entered positional parameters are digits using regular expression, [0-9]+ means match any digit 1 or more times
if [[ "$num" =~ ^[0-9]+$ ]]
then
sum=$(($sum+$num))
product=$(($product*$num))
((counter++))
else
echo "$num is not a number"
fi
done
echo
echo "Result: "
echo "count of numbers: $counter"
echo "sum of numbers: $sum"
echo "average of numbers: "$(echo "scale=4;$sum/$counter" | bc)
echo "product of numbers: $product"
Output:
$ ./script.bash 1 2 3 4 5 abcd
abcd is not a number
Result:
count of numbers: 5
sum of numbers: 15
average of numbers: 3.0000
product of numbers: 120
$ ./script.bash 1 2 3
Please enter atleast 5 positional parameters
$ ./script.bash 56 78 235 67 466
Result:
count of numbers: 5
sum of numbers: 902
average of numbers: 180.4000
product of numbers: 32048758560

Bash: Formating numbers in a n digit string

I am a bash newbie. I would like to echo the numbers 1 to x in a n digits format. For example, let's consider n=3: 5 should become 005, 13 should become 013, 110 should remain 110.
One way to achieve this is with this kind of structure:
for i in $(seq 1 120)
do
if [ "$i" -lt "10" ]
then
echo "00$i"
elif [ "$i" -gt "99" ]
then
echo "$i"
else
echo "0$i"
fi
done
but it is quite ugly and is really not flexible to changing values of n (number of digits). I'd rather have a function that just do the formatting in n digits? Is there an already built in function for that? If not can you help me to create such function?
Use printf:
for i in {1..120} ; do
printf '%03d\n' $i
done
% starts the format string
d means integer
3 means length = 3
0 means zero padded
\n is a newline

Shell script I am trying to get the product of odd numbers

mul=1
i=0
while [ $i -ne 10 ]
do
echo "Enter Number"
read num
if [ `expr $num % 2` -ne 0 ]
then
mul=`expr $mul*$num`
fi
i=`expr $i + 1`
done
echo mul of odd numbers = $mul
this is what i tried...its showing output as 1*3*5*7*9
pls correct the error here
Thanks in advance
"*" has a special meaning, hence you need to escape it and need to have a space between the two variables like:
mul=`expr $mul \* $num`
Note aside- Use of back ticks are discouraged and you may want to use something instead like:
mul=$(expr $mul \* $num)
Since your don't provide some details (see my comment above) I can't guarantee this answers your question and produces the desired result. This assumes your shell is bash. Please inform me and I'll edit the answer accordingly.
Consider the changes below. The relevant part is the change from expr ... to $(( ... )), which is bash's built-in arithmetic expression evaluator.
#!env bash
MUL=1
I=0
while [ $I -ne 10 ]
do
echo "Enter Number"
read NUM
if [[ $(($NUM % 2)) -ne 0 ]] ; then
MUL=$(($MUL * $NUM))
fi
I=$(($I + 1))
done
echo MUL of odd numbers = $MUL
This produces the following output:
$ sh foo.sh
Enter Number
1
Enter Number
2
Enter Number
3
Enter Number
4
Enter Number
5
Enter Number
6
Enter Number
7
Enter Number
8
Enter Number
9
Enter Number
0
MUL of odd numbers = 945

Resources