Visit Our UNIX and Linux User Community


A perfect number shell program


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers A perfect number shell program
# 1  
Old 10-07-2009
A perfect number shell program

Here's my work of testing whether a number input is perfect or not..

Code:
echo Enter a number
read no
i=1
ans=0
while [ $i -le 'expr $no / 2' ]
   do
       if [ 'expr $no % $i' -eq 0 ]
       then
          ans='expr $ans + $i'
       fi
       i='expr $i + 1'
done
if [ $no -eq $ans ]
then
echo $no is perfect
else
echo $no is NOT perfect
fi

Something has to be done in place of 'expr $no / 2'.. but I don't know..

Any idea guys.. ?

PS> you can provide me with a whole new program of tesing whether a number input from the keyboard is perfect or not.. if you guys can't fix the problem of this program..

Thanks in advance.
# 2  
Old 10-07-2009
Code:

'expr $no / 2'

to
Code:
$(expr $no / 2)

# 3  
Old 10-08-2009
Ok..
I fixed it..
Also fixed in
Code:
if [ 'expr $no % $i' -eq 0 ]

But still some problem!! Smilie
# 4  
Old 10-08-2009
Quote:
Originally Posted by Cyansnow
Code:
if [ 'expr $no % $i' -eq 0 ]

Code:
if [ `expr $no % $i` -eq 0 ]

# 5  
Old 10-08-2009
Quote:
Originally Posted by danmero
Code:
if [ `expr $no % $i` -eq 0 ]

what??
excuse me.. I didn't get you..
# 6  
Old 10-08-2009
Backticks ` ` are a different character from single quotes ' ' and are treated quite differently in shell. The command between backticks is executed by the shell, the command between single quotes is protected from the shell.
# 7  
Old 10-08-2009
I see there has already been a reply but for the sake of education fire up a shell and do some testing.

Code:
$ echo `expr 4 / 2`
2
$ echo 'expr 4 / 2'
expr 5 / 2
$ echo $(expr 4 / 2)
2

As you can see here both the backticks `command`and $(command) give the desired result while 'command' just gives you text string "command" back.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #804
Difficulty: Medium
Statistical signal processing is an approach which processes signals as stochastic processes, utilizing their statistical properties to perform signal processing tasks.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I use grep to grab prime number output from my factor program?

I have a factor program that runs and outputs to stdout all the prime numbers that are specified in the given paramters, in this case 30000000-31000000. Command: factor/factor 30000000-31000000 Sample output: 30999979 = 30999979 30999980 = 2^2 5 11 140909 30999981 = 3 10333327... (6 Replies)
Discussion started by: steezuschrist96
6 Replies

2. Shell Programming and Scripting

count number of entries perl program or Unix script

Hi I have a file with number of entries name 1 123 name 1 345 name 1 65346 name2 3243 name2 24234 name 2 234234 so on ......... how to count total number of entries for name 1 and name2...and so on Please guide. (1 Reply)
Discussion started by: manigrover
1 Replies

3. UNIX for Dummies Questions & Answers

Can you perfect my sed ?

I want to print only the lines that meet the criteria : "worde:" and "wordo;" I got this far: sed -n '/\(*\)\1e:\1o;/p;' But it doesn't quite work. Can someone please perfect it and tell me exactly how its a fixed version/what was wrong with mine? Thanks heaps, (1 Reply)
Discussion started by: maximus73
1 Replies

4. Solaris

Resolving port number to program name

I was just checking to see if anyone had a script that would allow me to go from port number to program name. I tried to create my own script but it looks like it only works for IPv4 sockets and it looks like daemons such as sshd return as AF_INET6 (in pfiles) for some reason. I can fix my script... (0 Replies)
Discussion started by: thmnetwork
0 Replies

5. Shell Programming and Scripting

ksh program that finds the lowest number in a .txt file

i am having a problem finding the lowest number after punching in a bunch of numbers in the .txt file but its probably the way i have the code set up. help please! (4 Replies)
Discussion started by: tinsteer
4 Replies

6. AIX

I want the perfect user-interface

I've got an aix-box somewhere on the network and a PC on my desk. Nothing fancy so far. The PC is made dual-boot: - windowsXP with putty & winSCP or - slackware 13 with xfce4 installed. The aix-box runs DB2 v8.2 and I've installed db2top to monitor the database. db2top is a character... (0 Replies)
Discussion started by: dr_te_z
0 Replies

7. UNIX for Dummies Questions & Answers

Script producing error, Program to calculate maximum number

Hi folks, Here i have written a shell script to calculate a maximum number from 10 numbers entered on command line. max=0 echo Enter 10 numbers , one at a time for i in 1 2 3 4 5 6 7 8 9 10 do read n max=`expr $max + $n` if --- At this last step there is some problem, it gives error... (5 Replies)
Discussion started by: rits
5 Replies

8. Shell Programming and Scripting

extraction of perfect text from file.

Hi All, I have a file of the following format. <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="tomcat"/> <role rolename="role1"/> <role rolename="manager"/> <role rolename="admin"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user... (5 Replies)
Discussion started by: nua7
5 Replies

9. Shell Programming and Scripting

Prime Number Program (Fun)

Hi, I was just wondering if anyone has, or knows where to download a prime number finder program. I would like a fairly simple bash program, and also I would like one that could take advantage of multiple processors. I have 500 cores I can use, and would like to take advantage of them using a... (2 Replies)
Discussion started by: Kweekwom
2 Replies

10. Shell Programming and Scripting

Write a shell program to find the sum of alternate digits in a given 5-digit number

Hi Can any one please post the answer for the above program.................. (4 Replies)
Discussion started by: banta
4 Replies

Featured Tech Videos