Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8467511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:40:03+00:00 2026-06-10T15:40:03+00:00

This is a program for calculating factorial of a number and I store it

  • 0

This is a program for calculating factorial of a number and I store it in a vector. The Program works fine for inputs upto 30, but for n=40 and greater, it produces a weird output.
eg.

input:

3

4

30

40

Output:

24

265252859812191058636308480000000

-190350521-236-6-6-5-745611269596115894272000000000

Where does this – sign come from?

#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
vector<int> solve(int n){
        if(n==1){
                vector<int> ans;
                ans.push_back(1);
                return ans; 
        }
        vector<int> b=solve(n-1);
        int temp=0,x=0;
        for(int i=0;i<b.size();i++){
                x=b[i]*n+temp;
                b[i]=x%10;
                temp=x/10;
        }
        if(temp!=0)
            b.push_back(temp);
        return b;
}   
    int main(){
            int t,n,i;
            scanf("%d",&t);
            while(t--){
                    scanf("%d",&n);
                    vector<int> ans=solve(n);
                    for(int j=ans.size()-1;j>=0;j--)
                             printf("%d",ans[j]);
                    printf("\n");
            }
    }           
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:40:04+00:00Added an answer on June 10, 2026 at 3:40 pm

    It’s integer overflow. A fixed-size integer can only hold so large a value and then it overflows. You probably want to use an arbitrary precision integer library like GMP.

    Run your code with these changes and it will become obvious:

    vector<int> solve(int n)
    {
        if(n==1){
                vector<int> ans; 
                ans.push_back(1);
                return ans;
        }
        vector<int> b=solve(n-1);
        int temp=0,x=0;
        cout << "b.size=" << b.size() << ", n=" << n << endl;
        for(int i=0;i<b.size();i++){
                x=b[i]*n+temp;
                cout << "b[ " << i << "]=" << b[i] << ", temp= " << temp << ", x=" << x << endl;
                b[i]=x%10;
                temp=x/10;
        }
        if(temp!=0)
        {
            cout << "push_back(" << temp << ")" << endl;
            b.push_back(temp);
        }
        return b;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program runs just fine, but for some reason when I ask for an
This program is calculating average in whole number without printing decimal values. It prints
This program stores pairs in a map, counting the number of times a word
I'm very new to programming and for some reason this program is not calculating
My code is pasted below.When I run this program,it keeps on calculating.I am using
Actually I wrote a Java program for calculating a particular number on the Fibonacci
I wrote a simple program(calculating the number of steps for the numbers 1-10^10 using
I'm using Task Parallel Library (TPL ) for calculating Fibonacci number. Program is given
This program builds a dictionary out of a list based on what two index
This program returns: package main import ( flag fmt ) func main() { num_agents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.