can anyone suggest changes in my code it is giving run time error,i beleive the the logic is correct but it gives sigsegv.
Your solution is incorrect. You are calling the recursion again and again and in each recursive step of your method ansh1() you are finding the sum of all values upto p where p varies from 0 to n-1. Hence you are using O(n^2) space in total i.e. 10 power 10 space which is not possible hence there would be a segmentation fault.