https://onlinegdb.com/lVBTfj1oM

why segmentation fault in this code?

Hi @hittasehgal,

I have corrected your code,

#include<bits/stdc++.h>
using namespace std;

int main() {
	int n;
	cin >> n;
	vector<int> result(1000);
	int size = 0; //size of result array
	result[0] = 1;
	
	//size increased because of 1 added at firt place
	size = size+1;
	
	
	int carry = 0;
	for(int i = 2; i <= n; i++){ //numbers you are going to multiply
		for(int j = size-1; j >= 0; j--){ //traversing result array from right to left
			int temp = result[j]*i+carry;
			result[j] = temp%10;
			carry = temp/10;
		}
		
		//insert carry in beginning
		while(carry!=0){
			for(int k = size-1; k >= 0; k--){
				result[k+1] = result[k];
			}
			result[0] = carry%10;
			carry /= 10;
			size++;
		}
	}
	
	for(int i = 0; i < size; i++){
		cout << result[i];
	}
	return 0;
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.