Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<stdio.h>
int main(){
    int x,y=0,z=1;
    scanf("%d",&y);
    for (x=1;x<=y;x++)
    z = z*x;
    printf("%d",z);
    return 0;
}
This code is working fine for less value like 4 or 5 but when I enter something bigger like 45881 This returns 0. Please help me

What I have tried:

Used diffferent complier but same issue.
Posted
Updated 20-Sep-20 7:32am
v3
Comments
Richard MacCutchan 20-Sep-20 7:41am    
You really need to get two books: one on computer architecture, and another on the C Programming Language. At the moment the reason you are struggling so much is that you lack the knowledge of the basics.
Siddharth Saurav - IIT [BHU] 20-Sep-20 8:19am    
Thanks for your views tbh I'm just a beginner so If you could suggest some books on Computer Architecture I'll be really thankful. :)
Richard MacCutchan 20-Sep-20 9:11am    
There are thousands of books published on the subject. Go to Amazon and read some of the reviews. And for C the best one ever written is "The C Programming Language" by Kernighan and Ritchie.

C++
for (x=1;x<=y;x++)
    z = z*x;

This code multiplies all numbers from 1 to y which is called the factorial of a number. An integer's max size is 2,147,483,647 [^]. 13! = 6,227,020,800 which is already out of range. I can't even compute 45881! but I can tell you it has 193,959 digits [1].

[1]: You can run this in your browser console.
JavaScript
(function () {
  let val = 1;
  for (let i = 2; i <= 45881; i++)
    val += Math.log10(i);
  val = Math.floor(val) + 1;
  alert("Digits: " + val);
})();
 
Share this answer
 
v5
To add to what Jon correctly says, integers have a fixed size, which these days is 32 bits or 4 bytes, giving a range of possible values between -2,147,483,648 to 2,147,483,647 because computers work with fixed length binary numbers.

If you want to calculate really big numbers, you will need to do all the arithmetic yourself, by creating numbers that can hold a ridiculous number of digits.
On of the ways is to use a byte per digit, and store them in an array. even then, you run into problems as the largest number of bytes you can dynamically allocate in C is 16711568 bytes while you could fit 45881! in, you will still run out of digits pretty soon.
If you do decide to go that way, you will have to implement multiplication for yourself, using long multiplication:
123 * 23 =

  123
 * 23
 ----
 2460
+ 369
 ----
=2829
 ----
That's not impossible, but it's going to get quite involved, and to be honest C isn't the best language to start doing it with (though it can be done using a struct
Be a nice little project though...
 
Share this answer
 
Quote:
This returns 0.

If you mean that the code exit with value 0, it is perfectly normal.
C++
return 0;

If you mean z is wrong after x=12 or so, it is normal too, it is an overflow, as stated in S1.
To compute z with bigger values like 45881, you will need to use BigInts.
 
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