Yes, you can, but there are some important things to keep in mind.
First off, I'll note that I'm not a lawyer or intellectual property expert. This is just my opinion, as informed as I can make it.
To my knowledge there's no prohibition in the App Store rules about what you do with your source code. The main issue arises when there's a conflict between the licence of the source code (i.e. the GPL) and the App Store. This was the problem with the VLC app that Ian C referenced in the comments, particularly with regards to other open source libraries used in an app.
Another issue is when not all the code belongs to you - i.e. others have contributed and hold copyright on parts of the code. You can't submit something to the App Store that you don't have the rights to, so everyone who owns the code in the project must agree.
That said, if we're talking about a program that you've developed entirely on your own, without code from other projects or people, then you're free to do with it whatever you feel like - you can release it on the App Store, licence it under anything you want (the licence applies to what others do with it, not you).
There are some open sourced apps on the app store, you can find a partial listing here
Best Answer
Without jailbreaking you can't really. iOS is designed not to allow executables to be created or even files to be moved between apps. Also there is no terminal.
If you want to build apps for iOS you generally use code on a mac.
However there are some apps that allow programming on the iPad but all run inside the app. Examples of this include Swift Playgrounds from Apple and various interpreters for Python, Javascript and Lua. (I have not provided links as I have not used them and my quick search found low rated or old apps) there are also interpreters that you can build on the Mac and install like Pharo ( a version of smalltalk).