Script to raise a integer by a exponent (while loop)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to raise a integer by a exponent (while loop)
# 1  
Old 10-19-2010
Script to raise a integer by a exponent (while loop)

I am trying to write a script that raises a integer (m) by a exponent (n) using a while loop

ex. 5 raised to the power of 2 ..

I am a beginner and i dont know what is the opperand or command i have to use to make this happen..this is what i have so far...

Code:
echo "Enter a integer for the base"
read m
echo "Enter a positve integer for the power"
read n
while [ $m -gt 0 ]
do
  exp=$(( m ^ n ))
echo $exp
done

This just gives a infinite loop of the sum...can someone please help?

---------- Post updated 10-19-10 at 01:52 AM ---------- Previous update was 10-18-10 at 09:32 PM ----------

Ok .. i got this far but i cant make it display the output without it going to an infinite loopSmilie! Please help! Smilie

Code:
c=0
i=1
echo "Enter an integer for the base"
read m
echo "Enter Enter a positive integer for the power"
read n
while (( i > 0 ))
do
  c=$(($m**$n))
done
echo "The product of the numbers is: $c"

if i insert
Code:
echo "echo "The product of the numbers is: $c"

it goes into an infinite loop giving me the answer...if i don't...it just keeps asking me for the second integer!
# 2  
Old 10-19-2010
The problem is with var i whose value is 1 which is always greater than Zero according to your while condition.. Hence you get a infinite loop.
Code:
while (( i > 0 ))

This User Gave Thanks to michaelrozar17 For This Post:
# 3  
Old 10-19-2010
Smilie you're not doing anything that requires a loop! Remove the while loop and your problems are solved!
This User Gave Thanks to umar.shaikh For This Post:
# 4  
Old 10-19-2010
@ umer -- salam .. you are absolutely right! i was just about to reply that....i was trying different things then i realized...i dont even need a loopSmilie lol ... so this is what i got and it works!

Code:
echo "Enter an integer for the base"
read m
echo "Enter Enter a positive integer for the power"
read n
c=$(($m**$n))
echo " "
echo "$m raised to the power of $n is: $c"

thanks for the replies Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Calculate e raise to the power in awk | UNIX

Input data: I'd like to calculate using value in second column. The value needs to be created using a formula, that employs exp (that is e raise to the power). awk '{ if(FNR==1){ ##if first line than print as is and add third column print $0,"weight" } else{ if($2<=0.01){... (2 Replies)
Discussion started by: genome
2 Replies

2. Shell Programming and Scripting

Shell script to find the wrong filename in a path and raise a trap for it

Example: I have server name A with an IP : 125.252.235.455 I have an username /password to login into this server under SSH connection In this server i have a path /apps/user/filename(Big.txt) Everyday we used to get the filename as Big.txt. I want a shell script to monitor this path... (4 Replies)
Discussion started by: ChandruBala73
4 Replies

3. Shell Programming and Scripting

While Loop to Check for Integer

Hello All, In ksh I am trying to ensure that user inputs are integers otherwise redisplay prompt with the following code; a=0 b=0 c=0 read b?"Please enter a number: " read c?"Please enter another number: " while ] && ]; do read b?"Not a number. Please enter a number: " read c?"Not... (4 Replies)
Discussion started by: techieg
4 Replies

4. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

5. Shell Programming and Scripting

Displaying exponent value as string in PERL script

Hello All, I am currently having an issue displaying a exponent value using perl, I have a perl program which generates an xls file. This xls is populated with values from a database. But for a certain column which I have made explicitely text and also implemented keep_leading_zeroes()... (3 Replies)
Discussion started by: sbasetty
3 Replies

6. Shell Programming and Scripting

PERL- converting exponent value to floating point

Hi Friends, I've an exponent value like, $val="9.57669e-05"; I want to convert this value to floating point value in PERL scripting. I tried googling for the solution, and also asked many perl friends. Unfortunately, I didn't get answer. Could you please help me? Thanks in advance... (4 Replies)
Discussion started by: ganapati
4 Replies

7. Programming

How to deal with this error: floating constant exponent has no digits

A conditional statement cause it: if(strlen(str) < n1+1) { ------- } (7 Replies)
Discussion started by: cdbug
7 Replies

8. Shell Programming and Scripting

raise an alarm in Unix

Hi members, I am working in WebSphere in Unix environment. we are working with 500 odd servers and most of the times processes got down. Can i have any shell script through whih some popup with alarm get raised whenever some server get down. kindly help.. Thanks Rishi (1 Reply)
Discussion started by: rishi.madan
1 Replies

9. Shell Programming and Scripting

Script to raise the alarm in the log File

Hi All, Please help to write the script that should raise an alarm if the new logs will not come in the log file. In other words i want to write a script which will monitor the log file continuously and will raise an alarm if the logs will not come after some time suppose 5... (5 Replies)
Discussion started by: akhtar.bhat
5 Replies

10. Programming

[Problem] raise a signal in FreeBSD

I am trying to send a SIGUSR1 to a set of process. Please tell me how to do. I've tried the system call raise(int sig) but it just raise a signal of to the 'current process.' My program is about a network chat server. When a client connects in, The main process will fork a new process... (1 Reply)
Discussion started by: Namely
1 Replies
Login or Register to Ask a Question