[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedAugust 14, 2023

How to create a bash shell script to find if a number is perfect or not.

How to create a bash shell script to find if a number is perfect or not.

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 Loop

  • Read 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:
  1. not a perfect number

Method 2: Using For Loop

  • Read 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

ans=0

for ((i = 1; i < no; i++))
do
    if [ $((no % i)) -eq 0 ]
    then
        ans=$((ans + i))
    fi
done

if [ $no -eq $ans ]
then
    echo "$no is perfect"
else
    echo "$no is NOT perfect"
fi

Output:

  1. perfect number
  1. not perfect number

Keep reading
How to Increment and Decrement Variable in Bash
April 10, 2023

How to Increment and Decrement Variable in Bash

How To Increment And Decrement Variable In Bash One of the most common arithmetic operations when writing Bash scripts is incrementing and decrementing variables. This is most often used in loops as a counter, but it can occur elsewhere in the script as well. Incrementing and Decrementing means…

Read article
How to Install Ubuntu 20.04 on Windows 10.
March 23, 2023

How to Install Ubuntu 20.04 on Windows 10.

NOTE: If you want to learn Linux (ubuntu), this is a very good and simple way to learn, I hope you like this article. Ubuntu is a very stable and flexible operating system and a Debian-based Linux distribution consisting mainly of free and open-source software. There are different versions of…

Read article
How to Find/Get your IP Address
March 23, 2023

How to Find/Get your IP Address

How To Find/Get Your IP Address This article explains several different methods of determining the public and private IP Addresses of a Linux system. Knowing the IP address of your device is important when troubleshooting network issues, setting up a new connection, or configuring a firewall. IP…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00