I have this concern, when getting the factors of a number. My code gives me the total numbers of all factors of a number.What I want is it should only display the factors of that number, not the total. For example I input 9 the output should be 1,3 and 9.
My code
echo -n " enter a no. "
read n
i=1
mul=1
until [ $i -gt $n ]
do
mul=`expr $mul \* $i `
i=`expr $i + 1 `
done
echo " factorial of $n is $mul "
Assuming that the user enters the number in its simplest form, e.g. 9 instead of 09.0, this works:
echo -n " enter a no. "
read n
for i in $(seq 1 $n)
do
[ $(expr $n / $i \* $i) == $n ] && echo $i
done
though a bit slowly for large numbers.
Related
the code below cannot make the fibonacci sequence more than 93 sequences, how can i solve this? I would like you to do with any number
#!/bin/bash
clear
echo "Program to Find Fibonacci Series"
echo "How many number of terms to be generated ?"
read n
x=0
y=1
i=2
echo "Fibonacci Series up to $n terms :"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done
You can use the "bc" command (an interactive algebraic language with arbitrary precision) to get past numeric limits of the shell. Here is the re-write of your while loop:
while [[ $i -lt $n ]]
do
i=$(( $i + 1 ))
z=$( bc <<< "$x + $y" )
echo "$z"
x=$y
y=$z
done
On Debian/Ubuntu/RHEL/CentOS systems, install the optional "bc" package.
I want to print prime numbers using for loop in shell script. Please provide some suggestions.
actually I could able to print odd numbers from range of 1 to 100, but now I am unable to print prime numbers. Provide some script to print prime numbers
# cat prime.sh
#!/bin/bash
for ((i=1; i<=100; i++))
do
output=$(( $i % 2))
if [ $output -ne 0 ]
then
echo "We got odd numbers: $i"
fi
done
The below code returns the list of prime numbers within a specified range.
the below command returns the prime numbers between 10 to 30
./filename.sh 10 30
#!/bin/bash
# Check if a number is prime
# function to return whether prime or not
check_prime() {
current_number=$1
flag=0
i=2
while test $i -le `expr $current_number / 2`
do
if test `expr $current_number % $i` -eq 0
then
flag=1
fi
i=`expr $i + 1`
done
if test $flag -eq 0
then echo $current_number
fi
}
# Assign From and to number
from_number=$1
to_number=$2
for (( number=$from_number; number<=$to_number; number++ ))
do
check_prime $number
done
This code will Print all prime numbers upto given number & also print count of it
hope it helps !!
#!/bin/bash
echo "enter a number upto which you want the prime numbers"
read num
count=0
for (( n=2; n<=$num; n++ ))
do
t=1
if [ $n -lt 2 ]
then echo "Please give other numbers than 0 and 1"
else
#echo $n
for (( i=2; i<$n; i++ ))
do
#echo $n
if (($n%i==0))
then
#echo "$n is not prime number"
t=0
break;
fi
done
#echo $t
if [ $t == 1 ]
then
echo "$n is a prime number"
count=`expr $count + 1`
fi
fi
done
echo "Total prime numbers upto $num are $count "
#!/bin/sh
echo “Enter number:”
read n
num=$n
rev=0
while [ $num –ne 0 ]
do
rem= `expr $num % 10`
rev= `expr $rev \* 10 + $rem`
num= `expr $num / 10`
done
if [ $rev –eq $n ]
then
echo $n “is a palindrome”
else
echo $n “is not a palindrome”
fi
I am getting errors as::
unary operator expected
Please help me in fixing this.
What an unexpected problem! Your dash - character is not the right dash.
The hex representation of your dash – is 0xe28093
Unicode Character 'EN DASH' (U+2013).
But the right dash's hex representation is ASCII 0x2d.
echo -n '–' | xxd # 00000000: e280 93
echo -n '-' | xxd # 00000000: 2d
So you should change it to the correct dash.
#!/bin/bash
echo -n "Enter number : "
read n
# store single digit
sd=0
# store number in reverse order
rev=""
# store original number
on=$n
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 )) # get next digit
# store previous number and current digit in reverse
rev=$( echo ${rev}${sd} )
done
if [ $on -eq $rev ];
then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi
Source : https://bash.cyberciti.biz/academic/palindrome-shell-script/
I'm trying to SUM a sequence.
Asking for a first number and a second bigger number, the result would be like this:
1st number: 2
2nd bigger number: 6
2+3+4+5+6=20
I'm the whole evening stack, I have to use for and seq
#!/bin/sh
echo -n "Enter number 1: "
read A
echo -n "Enter a number greater o equal than $A: "
read B
J=0
if [ $B -ge $A ]
then
for i in `seq $A $B`
do
RES=$i=$i+$J
#I don't know what to do
done
exit 0
else
echo "wrong"
exit 1
fi
exit 0
This prints out that requested part (I think you already know how to add if/else).
read -p "Enter number 1: " a
read -p "Enter a number greater or equal than $a: " b
high=$(( b-1 ))
sum=0
str=""
for i in $(seq $a ${high}); do
str="${str}${i}+"
sum=$(( j+=i ))
done
sum=$(($sum+$b))
str="${str}${b}=${sum}"
echo $str
I am new to scripting. The script below checks if a number inputted is a prime number or divisible by 2. Why is the third line from the bottom needed (i=expr $i + 1). I commented that line out to see what the script would do and it hangs the script up. Please advise.
#! /bin/bash
echo -n "Enter a number: "
read num
i=2
while [ $i -lt $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
echo "$num is not a prime number"
echo "Since it is divisible by $i"
exit
fi
i=`expr $i + 1`
done
echo "$num is a prime number "
If you don't increment i, then the test [ $i -lt $num ] will never be false, assuming i starts out less than num.