In: Computer Science
VB console .net framework
I am missing something in my code below which will calculate factorials individually into the file to be read by a sub using theis outdated language please Not Sreamwriter or Reader
fact*0, 2fact*0
Fact*1 , 2fact*1
Fact*2, 2fact*2
Fact*3, 2fact*3 (To 9)
Only the first 9 (0-8) but what I have doe calculates only the 9 factorial giving a single value
Public Function Calculate_fact(n_values As Integer) As
Long
'calc local variables
Dim fact As Long = 1
Dim fact2n As Long
Dim i As Integer 'loop index
Dim fid1 As Integer = FreeFile()
FileOpen(fid1, "Newfile.dat", OpenMode.Output, OpenAccess.Write) '
open output file
'Calculaions
If n_values = 0 Then
Return fact
Else For i = 1 To n_values
Return fact = fact * i
Return fact2n = (2 * fact) * i
Next i
End If
MsgBox(fact, fact2n)
WriteLine(fid1, n_values, fact, fact2n)
FileClose(fid1)
End Function
#include <stdio.h>
int
main()
{
int
I ,
num , fact =
1
;
//defining
factorial as 1 since least value is 1
printf (“Enter a number to
calculate its factorial”);
scanf (“%d”,
&num);
if
(num<
0
)
//if the
input is a negative integer
{
printf (“Factorial is not
defined
for
negative
numbers.”);
}
else
{
for
(i=
1
;i<=
num;i++)
//for loop doesn’t gets executed for input 0
as 1>0, therefore fact value remains 1
{
fact = fact * i;
// keeps on multiplying and
storing in the value of factorial till the input integer is
reached
}
printf(“Factorial of %d =
%dn”, num, fact);
}
return
0
;
//since we have defined the
main() method with a return value of integer type
}