Click here to Skip to main content
15,918,049 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i got problems to convert it to console from c# design;

in dos mode its succesful to compute but in design mode its only calculating till 26!, i got a below zero (-) result in 26! ;

heres my code ? wheres the problem?

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace can
{
    class Program
    {
        static void Main(string[] args)
        {
	    int k;
	    Console.WriteLine("Enter a number whose factorial needs to be calculated: ");
	    k = Convert.ToInt32(Console.ReadLine());

    	int[] numArr = new int[10000];
	    int total,rem=0,count,i;
	    for(i=0;i<9999;i++)
		    numArr[i]=0; 
				
	    numArr[9999]=1; 
	    for(count = 2 ; count <= k ; count++)   
	    {
		    while(i>0)
		    {
			    total = numArr[i] * count + rem;  
			    rem=0;
			    if(total>9)
			    {
				    numArr[i]=total%10;
				    rem=total/10;
			    }
			    else
			    {
				    numArr[i]=total;
			    }
			    i--;             
		    }
		    rem=0;
		    total=0;
		    i=9999;
	    }
	    Console.WriteLine("The factorial of " + k + " is ");
	    for(i=0;i<10000;i++)            
	    {
		    if(numArr[i]!=0 || count==1)  
		    {
		    	Console.Write(numArr[i]);
			    count=1;
		    }
	    }
	    Console.ReadLine();
        }
    }
}
Posted
Comments
Pheonyx 6-Apr-13 13:08pm    
Are you getting any errors thrown?
Can GARİP 6-Apr-13 13:32pm    
code working as the base on the last digit everytime i run it. for example if i intup 5 ,programs output like0 instead of 120 or input 4 output 4 instead of 24
Pheonyx 6-Apr-13 13:36pm    
I believe your issue is with the line that has %10 which is a mod function. So it returns the remainder.
Can GARİP 6-Apr-13 13:37pm    
any advice for solution?
Philippe Mori 6-Apr-13 23:05pm    
Uses a debugger and it should be easy to figured out what is going wrong by trying it for a small number and inspecting variables.

By the way it is not a good idea to uses same variable for multiple purpose as it make the code harder to understand. For example, count in last loop is not very clear.

1 solution

I would advise looking at how you are calculating the factorial.

After a quick Google search this seems like a sensible approach.

http://guyellisrocks.com/algorithms/factorial-function-in-c/[^]

It is a much cleaner approach to what you are attempting and removes the need for the mod function you are applying.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900