Find the factorial of six (6) using Visual Basic, and C++.
I have found the answer using C++ but not Visual Basic.
Quote from: ChroniX on May 28, 2006, 12:34 AM
Find the factorial of six (6) using Visual Basic, and C++.
I have found the answer using C++ but not Visual Basic.
...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
#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);
}
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?
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 + "."
Error: type mismatch on print statement.
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&; "."
Output:
The factorial of 8 is 40320 .
Used: MS QBasic 4.5.
I happen to of found QBasic on the Win95 installation CD.
Quote from: ChroniX on May 28, 2006, 01:18 PM
I happen to of found QBasic on the Win95 installation CD.
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.
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
This works to.
That's nasty.
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&; "."
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.
Quote from: ChroniX on May 29, 2006, 11:11 AM
I just see it that as long as the program get's done what's needed, then hey! it's all good.
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 :).
(define (fac n)
(if (zero? n)
1
(* n (fac (1- n)))))
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.
Brainfuck factorial code - written by me @ 2004-07-12
+>,[[->+>+<<]>>[-<<+>>]<[-<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<]>[-<<<+>>>]<<-]<.
Quote from: Yoni on May 29, 2006, 02:16 PM
Brainfuck factorial code - written by me @ 2004-07-12
+>,[[->+>+<<]>>[-<<+>>]<[-<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<]>[-<<<+>>>]<<-]<.
<3 BrainFuck!
Quote from: Mad_DadD on May 29, 2006, 11:53 AM
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.
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.
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 from: MyndFyre[vL] on May 30, 2006, 03:40 PM
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.
QBASIC tradition.