Why is my code giving segmentation fault?

I modified the code and its working now, but can you tell me why its giving the wrong output? https://ide.codingblocks.com/s/633043

Modified Code

logic is correct but have some silly mistakes
i have commented the mistakes and also write correct statement
check above code

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.