LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-22-2012, 01:04 PM   #1
ajjc2005
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Rep: Reputation: Disabled
using vi for scripting


I need to write a program that takes a number and shows the result of n!

Example: 5!=1*2*3*4*5

Thank you in advance for your help.
 
Old 08-22-2012, 01:16 PM   #2
jkirchner
Member
 
Registered: Apr 2007
Location: West Virginia
Posts: 765

Rep: Reputation: 196Reputation: 196
Hmmm, is this homework? If so, please post what you have done so far.

Also, VI is an editor, what programming language are you supposed to be writing this program in?
 
Old 08-22-2012, 01:23 PM   #3
ajjc2005
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
I'm not sure if I should be using if or a while loop or something else


echo "Please enter a number"
read num
count=0
total=0
if test $count -lt $num
then
let count=$count+1
let total=$num*$count
fi
echo $total
 
Old 08-22-2012, 02:35 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,327

Rep: Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881
Quote:
Originally Posted by ajjc2005 View Post
I need to write a program that takes a number and shows the result of n!

Example: 5!=1*2*3*4*5

Thank you in advance for your help.
You don't have anything in your script to ask for a second (or subsequent) value. You should prompt the user for more input, and tell them something like "Press Z to multiply your values", so the user can decide when they're done. Something like this:
Code:
#!/bin/Bash
echo "Enter the numbers to be Multiplied:"
read n1
read n2
answer=`expr $n1 \* $n2`
echo $answer
...may give you an idea. It's only two variables now, but can be expanded.

Last edited by TB0ne; 08-22-2012 at 02:40 PM.
 
Old 08-22-2012, 02:37 PM   #5
WzJazz
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Rep: Reputation: 0
Also, do a google search on factorial, factorial bash or factorial programming.
 
Old 08-22-2012, 07:03 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Hi TB0ne,
the OP is asking about factorial calcs https://en.wikipedia.org/wiki/Factorial, not just any multiplication set.
 
Old 08-22-2012, 11:36 PM   #7
Vegan
Member
 
Registered: Aug 2010
Location: Idaho
Distribution: Arch, Ubuntu, Fedora
Posts: 114
Blog Entries: 2

Rep: Reputation: 10
Quote:
Originally Posted by ajjc2005 View Post
I'm not sure if I should be using if or a while loop or something else


echo "Please enter a number"
read num
count=0
total=0
if test $count -lt $num
then
let count=$count+1
let total=$num*$count
fi
echo $total
A while loop will be perfect for what you are trying to achieve
 
Old 08-23-2012, 12:40 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
I'm curious what you mean about "using vi", for scripting. As mentioned, vi is just a text editor. However it is possible to write scripted input for it to edit files automatically.

None of that has much to do with shell arithmetic, however. Probably the easiest way to do a factorial (assuming integer input, of course), is with a simple c-style for loop.
 
Old 08-23-2012, 09:13 AM   #9
clocker
Member
 
Registered: Sep 2011
Distribution: fedora, redhat, mint
Posts: 87

Rep: Reputation: Disabled
if you need help on using vi, i can help but otherwise you should put your thread on the programming forum.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Scripting the Linux desktop, Part 2: Scripting Nautilus LXer Syndicated Linux News 0 02-17-2011 05:02 AM
Firefox Scripting Add-on (Scripting HTML / Javascript inside Firefox) linuxbeatswindows Programming 1 09-18-2009 11:09 PM
Need scripting help!! stan0934 Programming 5 05-29-2006 02:29 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM
scripting help please deepsix Programming 10 09-08-2005 08:49 PM


All times are GMT -5. The time now is 01:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration