• Welcome to Valhalla Legends Archive.
 

Factorial Program

Started by ChroniX, May 28, 2006, 12:34 AM

Previous topic - Next topic

ChroniX

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

I have found the answer using C++ but not Visual Basic.

Rule

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




ChroniX

#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?

Joe[x86]

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 + "."
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

MyndFyre

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

ChroniX

I happen to of found QBasic on the Win95 installation CD.

Joe[x86]

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.
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

ChroniX

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.

Joe[x86]

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&; "."
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

ChroniX

#9
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.

Yegg

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)))))

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.

Yoni

Brainfuck factorial code - written by me @ 2004-07-12

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

Explicit

Quote from: Yoni on May 29, 2006, 02:16 PM
Brainfuck factorial code - written by me @ 2004-07-12

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

<3 BrainFuck!
I'm awake in the infinite cold.

[13:41:45]<@Fapiko> Why is TehUser asking for wang pictures?
[13:42:03]<@TehUser> I wasn't asking for wang pictures, I was looking at them.
[13:47:40]<@TehUser> Mine's fairly short.

Yegg

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.