Sei sulla pagina 1di 11

5/21/12

c programming examples

C, C++, C# courses Aptech


50 lakh students trained already 25 years of expertise
www.Aptech-Education.com

c programming examples
C programming examples :- These programs illustrate various programming elements, concepts such as using operators, loops, functions, single and double dimensional arrays, performing operations on strings, files, pointers etc. Browse the code from simple c program to complicated ones you are looking for, every one of them is provided with output. C program download with executable files, so that you save on your computer and run programs without compiling the source code. All programs are made using c programming language and Turbo C compiler, most of these will work under Dev c++ compiler also. The first program prints "Hello World" on screen.

C programming codes
Hello world Print Integer Addition Odd or Even Add, subtract, multiply and divide Check vowel Leap year Add digits Factorial HCF and LCM Decimal to binary conversion ncR and nPr Add n numbers Swapping Reverse number Palindrome number Print Pattern Diamond Prime numbers Find armstrong number Generate armstrong number Fibonacci series Print floyd's triangle Print pascal triangle Addition using pointers Maximum element in array Minimum element in array Linear search Binary search Reverse array
www.programmingsimplified.com/c-program-examples 1/11

5/21/12

c programming examples

Insert element in array Delete element from array Merge arrays Bubble sort Insertion sort Selection sort Add matrices Subtract matrices Transpose matrix Multiply two matrices Print string String length Compare strings Copy string Concatenate strings Reverse string Find palindrome Delete vowels C substring Sort a string Remove spaces Change case Swap strings Character's frequency Anagrams Read file Copy files Merge two files List files in a directory Delete file Random numbers Add complex numbers Print date Get IP address Shutdown computer

c program examples
Example 1 - C hello world program /* A very simple c program printing a string on screen*/
#nld <ti.> icue sdoh mi( an) { pit(HloWrdn) rnf"el ol\"; rtr 0 eun ; }

www.programmingsimplified.com/c-program-examples

2/11

5/21/12

c programming examples

Output of above program: "Hello World" Example 2 - c program to take input from user using scanf
#nld <ti.> icue sdoh mi( an) { itnme; n ubr pit(Etra itgrn) rnf"ne n nee\"; saf"d,nme) cn(%"&ubr; pit(Itgretrdb yui %\" nme) rnf"nee nee y o s dn, ubr; rtr 0 eun ; }

Output: Enter a number 5 Number entered by you is 5 Example 3 - using if else control instructions
#nld <ti.> icue sdoh mi( an) { itx=1 n ; i (x= 1) f = pit(xi eult oe\"; rnf" s qa o n.n) es le pit(Frcmaio ue= a =i teasgmn oeao.n) rnf"o oprsn s = s s h sinet prtr\"; rtr 0 eun ; }

Output: x is equal to one. Example 4 - loop example


#nld <ti.> icue sdoh mi( an) { itvle=1 n au ; wievle=) hl(au<3 { pit(Vlei %\" vle; rnf"au s dn, au) vle+ au+; } rtr 0 eun ; }

Output: Value is 1 Value is 2 Value is 3 Example 5 - c program for prime number


www.programmingsimplified.com/c-program-examples 3/11

5/21/12
#nld <ti.> icue sdoh mi( an) { itn c n , ; pit(Etranme\"; rnf"ne ubrn) saf"d,&) cn(%" n;

c programming examples

i (n= 2) f = pit(Pienme.n) rnf"rm ubr\"; es le { fr(c=2;c< n-1;c+) o = + { i (n%c= 0) f = bek ra; } i (c! n) f = pit(Ntpie\"; rnf"o rm.n) es le pit(Pienme.n) rnf"rm ubr\"; } rtr 0 eun ; }

Example 6 - command line arguments


#nld <ti.> icue sdoh mi(n ag,ca *rv] anit rc hr ag[) { itc n ; pit(Nme o cmadln agmnspse:%\" ag) rnf"ubr f omn ie ruet asd dn, rc; fr(c=0;c<ag ;c+ o rc +) pit(%.Cmadln agmn pse i %\" c1 ag[]; rnf"d omn ie ruet asd s sn, +, rvc) rtr 0 eun ; }

Above c program prints the number and all arguments which are passed to it. Example 7 - Array program
#nld <ti.> icue sdoh mi( an) { itary10,n c n ra[0] , ; pit(Etrtenme o eeet i aryn) rnf"ne h ubr f lmns n ra\"; saf"d,&) cn(%" n; pit(Etr% eeet\" n; rnf"ne d lmnsn, ) fr(c=0;c<n;c+) o + saf"d,&ra[]; cn(%" aryc) pit(Aryeeet etrdb yuae\"; rnf"ra lmns nee y o r:n) fr(c=0;c<n;c+) o + pit(ary%]=%\" c aryc) rnf"ra[d dn, , ra[]; rtr 0 eun ; }

Example 8 - function program


#nld <ti.> icue sdoh

www.programmingsimplified.com/c-program-examples

4/11

5/21/12
vi m_ucin) od yfnto(; mi( an) { pit(Mi fnto.n) rnf"an ucin\"; m_ucin) yfnto(; pit(Bc i fnto mi.n) rnf"ak n ucin an\"; rtr 0 eun ; }

c programming examples

vi m_ucin) od yfnto( { pit(Wloet m fnto.Fe a hm.n) rnf"ecm o y ucin el t oe\"; }

Example 9 - Using comments in a program


#nld <ti.> icue sdoh mi( an) { / Snl ln cmeti csuc cd / ige ie omn n ore oe pit(Wiigcmet i vr ueu.n) rnf"rtn omns s ey sfl\"; / * *Mliiecmetsna utln omn ytx *Cmet hl u t udrtn cd ltresl. omns ep s o nesad oe ae aiy *Wl yuwiecmet wiedvlpn porm ? il o rt omns hl eeoig rgas * / pit(Go lc cporme.n) rnf"od uk rgamr\"; rtr 0 eun ; }

Example 10 - using structures in c programming


#nld <ti.> icue sdoh src pormig tut rgamn { fotcntn; la osat ca *one; hr pitr } ; mi( an) { src pormigvral; tut rgamn aibe ca srn[ ="rgamn i Sfwr Dvlpet" hr tig] Pormig n otae eeomn.; vral.osat=12; aibecntn .3 vral.one =srn; aibepitr tig pit(%\" vral.osat; rnf"fn, aibecntn) pit(%\" vral.one) rnf"sn, aibepitr; rtr 0 eun ; }

Example 11 - c program for Fibonacci series


#nld <ti.> icue sdoh mi( an) { itn frt=0 scn =1 nx,c n , is , eod , et ; pit(Etrtenme o trsn) rnf"ne h ubr f em\"; saf"d,n; cn(%"&)

www.programmingsimplified.com/c-program-examples

5/11

5/21/12

c programming examples
pit(Frt% trso Fbncisre ae:\"n; rnf"is d em f ioac eis r -n,) fr(c=0;c<n;c+) o + { i (c< 1) f = nx =c et ; es le { nx =frt+scn; et is eod frt=scn; is eod scn =nx; eod et } pit(%\"nx) rnf"dn,et; } rtr 0 eun ; }

Example 12 - c graphics programming


#nld <rpish icue gahc.> #nld <oi.> icue cnoh mi( an) { itg =DTC,g; n d EET m iigah&d &m":\C\G"; ntrp(g, g,C\T\BI) otety1,0 "rpissuc cd eape"; utxx(02, Gahc ore oe xml.) crl(0,20 5) ice20 0, 0; stoo(LE; eclrBU) ln(5,20 40 5) ie30 5, 5, 0; gth) ec(; coerp() lsgah ; rtr 0 eun ; }

For GCC compiler users


If you are using GCC compiler on Linux operating system then you need to modify programs. For example consider the following program which prints first ten natural numbers
#nld <ti.> icue sdoh #nld <oi.> icue cnoh mi( an) { itc n ; fr(c=1;c< 1 ;c+) o = 0 + pit(%\" c; rnf"dn, ) gth) ec(; rtr 0 eun ; }

Above source code includes a header file <conio.h> and uses function getch, but this file is Borland specific so it works in turbo c compiler but not in GCC. So the code for GCC should be like
#nld <ti.> icue sdoh mi( an) { itc n ; / frlo * * o op / fr(c=1;c< 1 ;c+) o = 0 +

www.programmingsimplified.com/c-program-examples

6/11

5/21/12
pit(%\" c; rnf"dn, ) rtr 0 eun ; }

c programming examples

If using GCC compiler save the code in a file say numbers.c, to compile the program open the terminal and enter command GCC numbers.c, this will compile the program and to execute the program enter command ./a.out .

C programming tutorial
C program consists of functions and declarations or instructions given to the computer to perform a particular task. The process of writing a program involves designing the algorithm, a flowchart may also be drawn and then writing the source code, after developing the program you need to test it and debug it if it doesn't meet the requirement. To make a program you need a text editor and a compiler. You can use any text editor of your choice and a compiler. C compiler converts source code into machine code which consists of zero and one only and can be directly executed on machine. An IDE or Integrated Development Environment provides a text editor, compiler, debugger etc. for developing programs or projects. Download codeblocks IDE which provides an ideal environment for development. It can import Microsoft Visual C++ projects, extendable as it uses plugins, open source and cross platform. A c program must have at least one function which is main. A function consists of declaration and statements, a statement is an expression followed by a semicolon, for example a+b, printf("c program examples") are expressions and a+b; and printf("C is an easy to learn computer programming language."); are statements. To use a variable we must indicate its type whether it's an integer, float , character. C language has many built in data types and we can make our own using structures and unions. Keywords such as switch, case, default, register etc. are special words with predefined meaning and can't be used for other purposes. Memory can be allocated during compile time or at run time using malloc or calloc. C language has many features such as recursion, preprocessor, conditional compilation, portability, pointers, multi threading by using external libraries, dynamic memory allocation due to which it is used for making portable software programs and applications. C standard library offers functions for mathematical operations, character strings and input/output and time. The process of making programs which is known as coding, requires knowledge of programming language and logic to achieve the desired output. So you should learn c programming basics and start making programs. Learning data structures such as stacks, queues, linked lists etc. using c programming provides you a greater understanding as you learn everything in detail. General belief is to go for other high level languages but it's a good idea to learn c before learning c++ or Java. C++ programming language is object oriented and it contains all the features of c language so learning c first will help you to easily learn c++ and then you can go for Java programming.

C programming books
If you are a beginner then buy anyone of first two books mentioned below and if you have previous programming experience or you know basics of c language then you can buy third one. Let Us C By Yashavant kanetkar PROGRAMMING WITH C By Byron Gottfried, Jitender Chhabra The C Programming By Brian Kernighan and Dennis Ritchie

C programming downloads(PDF and c compiler)


Dev c++ compiler C handbook Essential c To download files right click and select save link as.

www.programmingsimplified.com/c-program-examples

7/11

5/21/12

c programming examples

Comments
18 comments posted

hi
very very useful website for beginners of C language thank you a lot who had developed.
Posted by Guest on Sat, 06/08/2011 - 23:15

reply

About website
It's best for a zero level beginner. awesome helpful site. thanks a lot
Posted by Guest on Fri, 05/08/2011 - 16:42

reply

guest
good...............
Posted by Guest on Wed, 03/08/2011 - 15:46

reply

a gratitude
I am so much please with the site it helps me a lot in my programming class. Thank you very much. :)
Posted by Guest on Wed, 03/08/2011 - 06:50

reply

www.programmingsimplified.com/c-program-examples

8/11

5/21/12

c programming examples

Programs
I am very thankful to you thank you a lot
Posted by Guest on Wed, 03/08/2011 - 13:18

reply

Awesome site
Thanks for developing this site, as it is very helpful to the one who is not good in developing programs, like me. It include many languages as i always wanted to learn these, thanks for a great help...!!!
Posted by Guest on Mon, 01/08/2011 - 22:34

reply

hi really great job !


this website is really beneficial thanks a lot, and hope you will add some more simplified programs.
Posted by Guest on Mon, 25/07/2011 - 21:37

reply

thanks
through your good job i have been able to write my first program and i believe that i will be a good programmer. thanks
Posted by Guest on Mon, 25/07/2011 - 13:16

reply

wow
I'm really amazed to get so much help from this site. thanks a lot...
Posted by Guest on Mon, 25/07/2011 - 02:08

reply

thank you.........
we could find important codes for interview purpose also.
Posted by Guest on Sun, 24/07/2011 - 16:15

reply

Great job..........
it is so easy to learn through your codes... So thank you lot but if you can add more exercises it will be very useful.
www.programmingsimplified.com/c-program-examples 9/11

5/21/12

c programming examples

Posted by Guest on Fri, 22/07/2011 - 20:25

reply

great
you are doing a great job...nice
Posted by Guest on Fri, 22/07/2011 - 16:23

reply

hi
really its a good site for learning c. sir its my request to add some more programs like for recursion, to print stars in different shapes like diamond.
Posted by Guest on Thu, 21/07/2011 - 14:56

reply

summation of x
Hi do you have a C program for the summation of a number? (example summation of x)? thanks.
Posted by Guest on Wed, 20/07/2011 - 13:35

reply

hi
this is the unique website hope this may introduce more simplified programs
Posted by Guest on Sun, 17/07/2011 - 19:56

reply

sure
We are always willing to add more content and help you to improve your programming skills.
Posted by adminPs on Sun, 17/07/2011 - 20:02

reply

very helpful
good place to learn c language using sample programs.
Posted by Guest on Sat, 09/07/2011 - 16:11

reply

www.programmingsimplified.com/c-program-examples

10/11

5/21/12

c programming examples

hi........ to programming simplified member........


it is good site for new student for learning c language. but sir my request is that please add the some program related to star in different types.....please.
Posted by Guest on Sun, 19/06/2011 - 12:52

reply

Post new comment


Subje ct:

* Comme nt:

Save

Preview

Home | About Us | Contact Us | Feedback | Programmer Resources | Site Map | Privacy

Programming Simplified is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

www.programmingsimplified.com/c-program-examples

11/11

Potrebbero piacerti anche