Bash shell script to find if a number is perfect or not Last Updated : 05 Sep, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss how to write a bash script to find if a number is perfect or not. A perfect number is defined as, a positive number that is equal to the sum of its proper divisors. Smallest no is 6 ex= 1,2,3 are divisor of 6 and 1+2+3=6 Method 1: Using While LoopRead the input using the read command.Then run while loop with condition i <= no/2 .Check if no%i is equals to 0 or not if true then add i to ans .After getting out of the while loop checks if the obtained value of variable sum is equal to no variable or not.If equals then return "$no is a perfect number " else "$no is not a perfect number " using echo command.# !/bin/bash echo "Enter a number" # reading input from user read no # initializing the value of i i=1 ans=0 # check if the value of left operand is less # than or equal to the value of right operand # if yes, then the condition becomes true while [ $i -le $((no / 2)) ] do # Checks if the value of two operands are # equal or not; if yes, then the condition # becomes true if [[ $((no%i)) -eq 0 ]] then ans=$((ans + i)) fi i = $((i + 1)) done # Checks if the value of two operands are equal # or not; if yes, then the condition becomes true if [ $no -eq $ans ] then # printing output echo "$no is perfect" else # printing output echo "$no is NOT perfect" fi Output: 1) perfect number Fig= output 1 2) not a perfect number Fig= output 2Method 2: Using For LoopRead the input using the read command.Then use for loop and iterate until no(input).Check if no%i is equaled to 0 or not if true then add i to ans .After getting out of for loop check if the obtained value of the variable sum is equal to no variable or not .If equals then return "$no is a perfect number " else "$no is not a perfect number " using echo command.# !/bin/bash echo "Enter a number" read no i=1 ans=0 for i in 1 2 3 4 5 .. no do if [[ $((no%i)) -eq 0 ]] then ans=$((ans + i)) fi i=`expr $i + 1` done if [ $no -eq $ans ] then echo "$no is perfect" else echo "$no is NOT perfect" fi Output: 1) perfect number Fig= perfect 2) not perfect number Fig=not perfect Time complexity: O(n) as using a loop Auxiliary space: O(1) Comment More infoAdvertise with us Next Article Bash shell script to find if a number is perfect or not K kamal251199 Follow Improve Article Tags : Linux-Unix Shell Script Linux-Shell-Commands Similar Reads Shell Script to print odd and even numbers In this article, we will discuss how to print even and odd numbers. We will take a number as input and print all even and odd numbers from 1 to the number. Input : Enter the Number - 10 Output : Even Numbers - 2, 4, 6, 8, 10 & Odd Numbers - 1, 3, 5, 7, 9 Input : Enter the Number - 5 Output : Eve 2 min read Shell script to print table of a given number In this article, we will explore the process of creating a simple yet powerful shell script that takes user input and generates number tables. Number tables are a common mathematical concept used to display the multiples of a given number up to a specified limit. We will illustrate this process with 2 min read Bash program to check if the Number is a Palindrome Given a number num, find whether the given number is palindrome or not using Bash Scripting. Examples: Input : 666 Output : Number is palindrome Input : 45667 Output : Number is NOT palindrome Approach To find the given number is palindrome just check if the number is same from beginning and the end 1 min read Shell Script To Check For a Valid Floating-Point Value User Input can be hectic to manage especially if it's about numbers. Let's say you wanted to perform mathematical calculations in your app or script. It's not hard to say that it will have to deal with floating-point numbers. It's easy in many statically typed programming languages but if it has to 4 min read Shell Script to Validate Integer Input Here we are going to see a shell script which validates an integer. We are going to display whether the input entered is an integer or an invalid input. Approach: Take input from the user.Store the input of any variableNow we are going to trim the input in such a way that all the -(minus) or +(plus) 1 min read Like