In the first for loop you insert value in a[i] but you didn’t check the value of t before implementing it .Suppose the user enter t = (10^6+1) , but your array a can store only 10^6 values , so this leads to overflow hence the compiler shows segmentation error. First check the values of all inputs only then execute other things. C family is prone to such memory errors because it does not do bound checking , hence buffer overflows occur.