Valhalla Legends Forums Archive | Yoni's Math Forum | Re: Factorial Program

AuthorMessageTime
Chronix
Find the factorial of six (6) using Visual Basic, and C++.

I have found the answer using C++ but not Visual Basic.
May 28, 2006, 5:34 AM
Rule
[quote author=ChroniX link=topic=15078.msg153308#msg153308 date=1148794462]
Find the factorial of six (6) using Visual Basic, and C++.

I have found the answer using C++ but not Visual Basic.
[/quote]

...the answer should essentially be the same in both languages...

have an integer variable theanswer = 6
start a loop with an index i=0. 
loop the expression below, incrementing the index on each loop.
{theanswer = theanswer*(5-i) }
and once i = 4 or 5 stop the loop before the expression is evaluated again.

..alternatively you could start the loop at index i=5 and decrement it until i=0 or 1.
and evaluate the expression theanswer=theanswer*i



May 28, 2006, 5:48 AM
Chronix
[code]#include <stdio.h>

#define VALUE 6

int i,j;
void main()
{
j=1;
for (i=1; i<=VALUE; i++)
j=j*i;
printf("The factorial of %d is %d\n",VALUE,j);
}[/code]

This is the code I used, and found '720' to be the factorial of 6.

Can anyone figure this problem out using the very old program BASIC?
May 28, 2006, 6:04 AM
JoeTheOdd
[pre]DIM I AS INTEGER
DIM J AS INTEGER
DIM VALUE AS INTEGER

J = 1
VALUE = 6

FOR I = 1 TO VALUE
    J = J * I
NEXT

PRINT "THE FACTORAL OF " + VALUE + " IS " + J + "."[/pre]
May 28, 2006, 6:14 AM
Myndfyr
Error: type mismatch on print statement.

[code]
CLS

DIM i&, j&, value&
j& = 1
value& = 8
FOR i& = 2 TO value&
        j& = j& * i&
NEXT

PRINT "The factorial of"; value&; "is"; j&; "."
[/code]
Output:
[pre]The factorial of 8 is 40320 .[/pre]

Used: MS QBasic 4.5.
May 28, 2006, 9:31 AM
Chronix
I happen to of found QBasic on the Win95 installation CD.
May 28, 2006, 6:18 PM
JoeTheOdd
[quote author=ChroniX link=topic=15078.msg153333#msg153333 date=1148840309]
I happen to of found QBasic on the Win95 installation CD.
[/quote]

Yeah. But you can't compile with QBasic, only with QuickBasic. You should be able to find floppy images somewhere - there's three disks, I believe.
May 28, 2006, 6:59 PM
Chronix
[code]10 Cls
20 J = 1
30 INPUT X
40 For I = 1 To X
50 J = J * I
60 Next
70 Print "THE FACTORAL OF"; X; "="; J[/code]

This works to.
May 29, 2006, 6:18 AM
JoeTheOdd
That's nasty.

[code]10  CLS
20
30  DIM I&  ' Loop counter
40  DIM J&  ' End factoral value
50  DIM K&  ' Number to find value of
60
70  PRINT "NUMBER TO FIND FACTORAL OF: ";
80  INPUT K&
90
100 J = 1
110
120 FOR I = 1 TO K
130    J = J * I
140 NEXT
150
160 PRINT "THE FACTORAL OF "; k&; " IS "; j&; "."[/code]
May 29, 2006, 3:54 PM
Chronix
But why use the Dim statements when not needed, it's just extra typing. Although it does look more organized.

I just see it that as long as the program get's done what's needed, then hey! it's all good.
May 29, 2006, 4:11 PM
Yegg
[quote author=ChroniX link=topic=15078.msg153381#msg153381 date=1148919119]
I just see it that as long as the program get's done what's needed, then hey! it's all good.
[/quote]

Thinking that way can conclude to terrible problems with code readability. You should always produce nice and clean looking code, in whatever language you prefer. I saw this post so decided to define a factorial in Scheme for fun :).

[code](define (fac n)
  (if (zero? n)
    1
    (* n (fac (1- n)))))[/code]
May 29, 2006, 4:27 PM
Mad_DadD
Code readability and cleanliness is in the eyes of the programmer.  Some don't need to extra lines, rem lines and blank lines to see "readability", some need only the basics and as few lines as possible.  It's always a good idea to be organized with your programs.
May 29, 2006, 4:53 PM
Yoni
Brainfuck factorial code - written by me @ 2004-07-12

+>,[[->+>+<<]>>[-<<+>>]<[-<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<]>[-<<<+>>>]<<-]<.
May 29, 2006, 7:16 PM
Explicit[nK]
[quote author=Yoni link=topic=15078.msg153394#msg153394 date=1148930219]
Brainfuck factorial code - written by me @ 2004-07-12

+>,[[->+>+<<]>>[-<<+>>]<[-<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<]>[-<<<+>>>]<<-]<.
[/quote]

<3 BrainFuck!
May 29, 2006, 7:22 PM
Yegg
[quote author=Mad_DadD link=topic=15078.msg153386#msg153386 date=1148921594]
Code readability and cleanliness is in the eyes of the programmer.  Some don't need to extra lines, rem lines and blank lines to see "readability", some need only the basics and as few lines as possible.  It's always a good idea to be organized with your programs.
[/quote]

Every programmer knows of this, but should we not write code that can be read by our fellow programmers? Code that can be read and understood by all is better code, in the future you will still understand it if you wrote nice, clean code. If you wrote sloppy code that you understand currently, you may not understand it at a later time especially if much time has passed since the last time you saw the code.

Believing that you can write code any way you feel as long as you alone can read it is not a good way to think. if your code is readable, you have a higher chance of receiving adequate help if required, you can also explain more easily to others what the code is doing, I would also assume that less comments would be necessary.
May 30, 2006, 7:10 PM
Myndfyr
Why are you guys numbering your lines?  It's pretty much entirely unnecessary.  You don't use gotos in your code anywhere.  It's entirely superfluous.
May 30, 2006, 8:40 PM
Yoni
[quote author=MyndFyre[vL] link=topic=15078.msg153455#msg153455 date=1149021613]
Why are you guys numbering your lines? It's pretty much entirely unnecessary. You don't use gotos in your code anywhere. It's entirely superfluous.
[/quote]
QBASIC tradition.
May 31, 2006, 7:30 PM

Search