LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 12: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, 12:16 PM   #2
jkirchner
Member
 
Registered: Apr 2007
Location: West Virginia
Distribution: Pop!_OS
Posts: 945

Rep: Reputation: 297Reputation: 297Reputation: 297
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, 12: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, 01:35 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
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 01:40 PM.
 
Old 08-22-2012, 01: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, 06:03 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Hi TB0ne,
the OP is asking about factorial calcs https://en.wikipedia.org/wiki/Factorial, not just any multiplication set.
 
Old 08-22-2012, 10: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-22-2012, 11:40 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 08: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



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 04:02 AM
Firefox Scripting Add-on (Scripting HTML / Javascript inside Firefox) linuxbeatswindows Programming 1 09-18-2009 10:09 PM
Need scripting help!! stan0934 Programming 5 05-29-2006 01:29 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
scripting help please deepsix Programming 10 09-08-2005 07:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:34 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
Open Source Consulting | Domain Registration