0

I wrote a code to get factorial for a number uptil 100. The problem i have is that i have to display the array it is sstored in a single output. So i cant use for statement and print each element.

#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
  int array[200];
  int count;//keepas count of the number of digits.
  int temp;//store the carry over.
  int t; // no of cases
  int num; //stores the fact.
  int i,j,k;
  int x;
  cin>>t;
  while(t--)
    {
      cin>>num;      
array[0]=1;
      count= 1;
      temp=0;
      
      for(i=2;i<=num;i++)
	{
	  for(j=0;j<count;j++)
	    {
	      x=(array[j]*i)+temp;
	      array[j]=x%10;
	      temp=x/10;
	    }
	  
	  while(temp!=0)
	    {
	      array[j]=temp%10;
	      temp/=10;
	      j++;
	      count++;
	    }
	}
      
      
      for(i=count-1;i>=0;i--)
	{
	  cout<<array[i];
	}
      
    }
}
5
Contributors
11
Replies
12
Views
7 Years
Discussion Span
Last Post by siddhant3s
Featured Replies
  • [QUOTE=tux4life;913077]Oh sorry, I guess I misunderstood your question. I understand it now, you could for example use a [B]string[/B] and a [B]stringstream[/B] to achieve that.[/QUOTE] No matter what, you're going to need a loop. You can use a loop to CREATE the string, then output the string (no loop), or … Read More

0

>I wrote a code to get factorial for a number uptil 100.
If you even can't print out the result, how can you be sure then that your code is working?

0

i just edited my post. The program is working properly but i want to output all the values together and not using for statement.
I am submitting to a online judge so should ouput it in one go

0

i just edited my post. The program is working properly but i want to output all the values together and not using for statement.
I am submitting to a online judge so should ouput it in one go

Well, use a while loop then :)
You can let your while loop do the same as a for loop:

A for-loop in general looks like this:

for( [B]PART A[/B]; [B]PART B[/B]; [B]PART C[/B] )
{
    // Your code
}

You can easily make your while loop behaving in the same way by putting the parts in another order:

[B]PART A[/B]
while( [B]PART B[/B] )
{
    // Your code
    [B]PART C[/B]
}

:)

0

Since you don't know how many factorials there are then use an empty array of N / 2, thus 100 / 2 = 50. Most of it will be wasted but that's fine. Alternatively you'd have to use a dynamic array like an inch worm. Preset a size, fill one cell at a time, when full, allocate a new array with a size increase, copy the contents over, destroy the old array, continue. Just N/2 is much easier.

FactAry[ 100/2 + 1 ]
nFactIndex = 0;

FactAry[ nFactIndex++ ] = 1; // Seed the 1st factorial

Then work your way up! As you find your factorial...
FactAry[ nFactIndex++ ] = MyNewFactorial;

If you're given 100 as the number, why are you taking input? Is the 100 suppose to be manually entered? If so then dynamically allocate the array instead of hard coding N/2 like I mentioned earlier in this post!

I'm guessing your assignment was to collect all the factorials in an array, and then call a function passing the array and number of factorials in it, and print them out!

0

My problem is to output the array single time. If i use a for or a while loop ,each array element is outputted sperately. I want to output all the necessary elements together.

0

My problem is to output the array single time. If i use a for or a while loop ,each array element is outputted sperately. I want to output all the necessary elements together.

Oh sorry, I guess I misunderstood your question.
I understand it now, you could for example use a string and a stringstream to achieve that.

0

You're doing factorials or you're factoring? I'm seeing you use the modulus operator on 10, which suggests you're trying to isolate digits? 200! is way too big for an integer, so I assume you're doing something with arrays to bump up your storage or something, and doing manual multiplication through arrays. Is that the idea? Each array value s a single digit?

1

Oh sorry, I guess I misunderstood your question.
I understand it now, you could for example use a string and a stringstream to achieve that.

No matter what, you're going to need a loop. You can use a loop to CREATE the string, then output the string (no loop), or you can output the data one digit at a time using a loop. Six of one, half dozen of the other as far as I can see. Both work just fine.

Votes + Comments
Yes, my intention was that he would use a loop :)
0

BTW, 100! is

93326215443944152681699238856266700490715968264381621468592963
89521759999322991560894146397615651828625369792082722375825118
5210916864000000000000000000000000

There is no fundamental integral datatype in C++ which can store this.

@Vernon
Good catch.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.