Prime numbers between 1 to 100 in shell script
Prime numbers between 1 to 100 in shell script
In this article, I am going to guide you how to write a program to print Prime numbers between 1 to 100 in shell script
The code given is very simple to understand and easy to implement. You can copy the code from the below code and save in a file name with extension ".sh". Run the code to get the result.
# Write a program to print Prime numbers between 1 to 100 in shell script
Findprime ()
{
num=$1
for((i=2; i<=num/2; i++))
do
if [ $((num%i)) -eq 0 ]
then
return 10
fi
done
echo "$num"
}
for((x=2; x<=100; x++))
{
Findprime $x
}
Output:
For on demand articles for free join us here:
Telegram: https://t.me/@HintAbout
Facebook: https://m.facebook.com/HintAbout
Step1: create a function "Findprime()"
Step 2: Loop from 2 to half of 'n' where 'n' is the variable.
Algorithm to print Prime numbers between 1 to 100 in shell script
Step1: create a function "Findprime()"
Step 2: Loop from 2 to half of 'n' where 'n' is the variable.
Step 3: if number is divisible then return from the function
Else
print the number
Step 4: Loop from 1 to 100 where x is a variable.
Step 5: with in the loop call the function "findprime"
Step 6: Exit.
I hope this article will help you to understand and write the shell script program. You can also join our social media platform for more interesting articles.
0 Response to "Prime numbers between 1 to 100 in shell script"
Post a Comment