Learn C Programming in Easy Way | codeforhunger

In this article I am going to share all C programming exercises and solutions. Here, you will find all C programming exercises and examples on this blog.

To start learning Programming first you need to know what is programming and why it is important to learn.

How to Write Correct Code in C Programming and Where to Begin

To start coding in C you need to start learning C programming first and most important thing is your interest should be in C programming.

C Program to Print 2 15 41 80 132 197 275 366 Numbers Series

Ex: Write a C program to print 2 15 41 80 132 197 275 366 numbers series. How to write a C program to print 2 15 41 80 132 197 275 366 numbers series. C program to print 2 15 41 80 132 197 275 366 numbers series.


Input from user:

Enter the number: 8


Expected output:

2 15 41 80 132 197 275 366

C Program to Print 1 3 8 15 27 50 92 Number Series

Ex: Write a C program to print 1 3 8 15 27 50 92 number series. How to write a C program to print 1 3 8 15 27 50 92 number series. C program to print 1 3 8 15 27 50 92 number series.


Input from user:
Enter the number: 8

Expected output:
1 3 8 15 27 50 92

C Program to Print Natural Numbers Series

Ex: Write a C program to print natural number series. How to write a C program to print natural number series. C program to print natural number series.


Input from user:

Enter the number: 8


Expected output:

1 2 3 4 5 6 7 8...

Series Programs in C Exercises and Solutions

In these post we will learn different mathematical series programs using C programming language. 

In this exercises you will learn about series programs. How to print series programs in C programming language. Below is shown some series programming exercises and examples or practice set to solve it your self.

Guess the Number Game in C (Project 2)

Guess the number game Program in C:

In this tutorial we will learn how to create Guess the number game project in C.


  Quick links:


In these program we used two extra header files one is <stdlib.h> and second is <time.h>. <stdlib.h> header file  used because,

Rock-Paper-Scissor Game In C (Project-1)

Rock-Paper-Scissor Game Project using C Language:


In this tutorial we will learn how to create Rock-Paper-Scissor game project using C language.


  Quick links:




In these program we used two new header files one is <stdlib.h> and second is <time.h><stdlib.h> header file  used because

Data Input and Output Functions in C Programming

Data Input and output functions in C:

The C programming language  supports many library functions which includes a number of input/output functions. These functions are used to transfer the information between the computer and device(user).


  Some basic input/output functions are: 


    • scanf() and printf(): These two functions are used to transfer the single characters, strings and numerical values.


    • getchar() and putchar(): These two functions are used to transfer single characters.

    • gets() and puts(): is used to input and output strings.



    1. How to use scanf and printf functions:


    scanf() function is used to take input from user(keyboard).

    printf() function is used to display output on the screen.


      Example Program:


    #include<stdio.h>

    void main()

    {

          int num;// variable definition

           /*Display message and ask user to enter some number... using printf()*/

          printf("Enter the number:");

          /*Read the number entered by user... using scanf()*/

          scanf("%d",&num);

         /*Display the number with message...using printf() */

          printf("You entered number is:  %d",num);

    }



    In above program %d is used to scan the intiger value.

    If value is char then we use %c.

    If value is float then we use %f and so on...


    Above program shows the following output:

    Data input and output functions in C programming language


    2. How to use getchar and putchar functions in our program: 


    getchar() function reads  a single character from the terminal and returns it as an intiger.

    putchar() function is used to display these single character.



      Example program:


    #include<stdio.h>

    void main()

    {

          int a;

          printf("Enter a character:");

          /*Here, getchar function is used to store the accepted character in variable a*/

         a = getchar();    

         /*Display the character stored in variable a using putchar function*/

         putchar(a);

    }


    Above program shows the following output:



    3. How to use gets() and puts() function in C:


    In simple words:

    gets() function is used to accept string from user.

    puts() function is used to display the string.


      Example program:

     

    #include<stdio.h>

    void main()

    {

          //Declare character array to store string

          char str[100];

          printf("Enter a string: ");

           /*store string in array str*/

          gets(str);

          /*Display string on the output screen*/

          puts(str);

    }


    Above program shows the following output:


    File Handling in C Exercises and Solutions


    File handling is used to create, delete, read and update the files. In simple words using File handling in C programming we can perform create, update, read and delete operations.

    There are many functions in C file handling(library) like fopen(), fprintf(), fscanf() and many more....

    There are many functions in c library. A list of some file handling functions are shown in below table.

      File handling functions in C:


    File handling funtions table in C, file handling functions in c



    Below table shows the file opening modes in C: 


    File opening modes in c programming, file handling file opening modes in c



    In this tutorial we will learn file handling with some file handling exercises and examples. So let's get started......😃



      Exercises and Examples of File Handling in C:










    If you like these file handling exercises, then please share them with your friends and colleagues.

    C Pattern Programming Exercises and Examples


    In this tutorial you will find lot of pattern programming exercises and examples. Pattern programming exercises manly used to enhance the logic of programmer, by solving this programs you can easily increase your logical thinking ability or also it will increase your loops and if-else statements knowledge.


      Pattern programming Exercises and there solutions:


    1. Star pattern programs.

    2. Number pattern programs.



    Thank you for visiting us....... keep visiting. Learn our more C programming exercises with their solutions.....😊

    C Number Pattern Programming Exercises & Examples

    Number patterns in C examples, Number pattern exercises in C

    In this tutorial you will learn number pattern programs with there solutions. You can practice it to enhance your programming logic. I hope this exercises will help you to understand working of looping statements, if-else statements and more.


      Quick links:




      Exercises and examples of number pattern's in C:


    1. Write a C program to print square number pattern 1.















                         


    Keep visiting, keep learning......😊
    Happy Coding... :)

    C Program to Print Number Heart Pattern

    Ex:  Write a C program to print number heart pattern, which shows the steps to print pattern. How to write a C program to print number heart pattern, which shows steps to print pattern . C program to print number heart pattern which shows steps to print pattern.



    Input from user:

    Enter the number: 5


    Expected output:

      11      22

    1111  2222

    333333333

      3333333

        33333

          333

            3



    This pattern is same as previous exercises star heart pattern.


    Only difference is:

    In this pattern we only need to replace the * with numbers 1, 2 and 3

    This number's also shows the steps to print this pattern.



      Program to print number heart pattern:


    #include<stdio.h>

    void main()

    {

    int no,z;

    int i,j,k;

    printf("Enter the number:\n");

    scanf("%d",&no);

    /*To print upper part*/

    z=no*2;

    for(i=no/2;i<=no;i=i+2)

    {

    /*To print left peak */

    for(j=1;j<no-i;j=j+2)

    {

                        printf(" ");

    }

    for(k=1;k<=i;k++)

    {

                       printf("1");

    }

    /*To print right peak*/

    for(j=1;j<=no-i;j++)

    {

        printf(" ");

    }

    for(k=1;k<=i;k++)

    {

    printf("2");

    }

    printf("\n");

    }

    /*To print lower part means inverted triangle*/

    for(i=1;i<=no;i++)

    {

    for(j=1;j<i;j++)

    {

    printf(" ");

    }

    for(k=1;k<z;k++)

    {

    printf("3");

    }

    z-=2;

    printf("\n");

    }

    }



    Above program shows the following output:


    C Program to Print Number Heart Pattern



    C Program to Print Diamond Number Pattern

    Ex: Write a C program to print diamond number pattern. How to write a C program to print diamond number pattern. C program to print diamond number pattern.


    Input from user:

    Enter the number: 5


    Expected output:

                  1

                1  2

              1  2  3

            1  2  3  4

          1  2  3  4  5

            1  2  3  4

              1  2  3

                1  2

                  1




      Step by step logic of the given:


    For better understanding i have devided this code in two parts.


    To print upper half part:

                  1

                1  2

              1  2  3

            1  2  3  4

          1  2  3  4  5


    1. Accept input number from user.


    2. Run outer for loop from  1 to no which shows rows in pattern:

    for(i=1;i<=no;i++)


    3. Run inner for loop from i to no-1 to print spaces:

    for(j=i;j<no;j++)

    {

    printf(" ");

    }


    4. Run another inner for loop from 1 to i which shows columns in pattern:

    for(k=1;k<=i;k++)


    5. Inside the second inner for loop print value of k with one space:

    printf("%d ",k);


    6. After that outside the block of  inner for loop print next line \n for print new row.



    To print lower half part:


            1  2  3  4

              1  2  3

                1  2

                  1


    1. Run outer loop from 1 to no-1 which shows rows in pattern:

    for(i=1;i<no;i++)


    2. Run inner for loop from 1 to i to print spaces:

    for(j=1;j<=i;j++)

    {

            printf(" ");

    }


    3. Run another one inner for loop from 1 to no-i and inside the loop print value of  k :

    for(k=1;k<=no-i;k++)

    {

            printf("%d ",k);

    }


    4. Last print \n for new row.




      Program to print diamond number pattern:


    #include<stdio.h>

    void main()

    {

    int no,i,j,k;

    printf("Enter the number:");

    scanf("%d",&no);

    /*To print upper part*/

    for(i=1;i<=no;i++)

    {

    for(j=i;j<no;j++)

    {

    printf(" ");

    }

    for(k=1;k<=i;k++)

    {

    printf("%d ",k);

    }

    /*For next row*/ printf("\n");

    }

         /*To print lower part*/

         for(i=1;i<no;i++)

         {

             for(j=1;j<=i;j++)

             {

             printf(" ");

             }

             for(k=1;k<=no-i;k++)

             {

             printf("%d ",k);

             }

         /*For next row*/

         printf("\n");

         }

    }


    Above program shows the following output:


    C program to print diamond number pattern, Star pattern programs in C




    C program to print full pyramid number pattern

    Ex: Write a C program to print full pyramid number pattern. How to write a C program to print full pyramid number pattern. C program to print full pyramid number pattern.


    Input from user:

    Enter the number: 5


    Expected output:

            1

          123

        12345

      1234567

    123456789


    C program to print inverted half right side of pyramid number pattern

    Ex: Write a C program to print inverted half right side of pyramid number pattern. How to write a C program to print inverted half right side of pyramid number pattern. C program to print inverted half right side of pyramid number pattern.


    Input from user:

    Enter the number:

    5


    Expected output:

    12345

    1234

    123

    12

    1



      Step by step logic of the given program:


    1. Accept input number from user declare variable say no.


    2. Run outer for loop from 1 to no which shows rows in pattern:

    for(i=1;i<=no;i++)


    3. Run inner for loop from 1 to (no+1)-i which shows columns in pattern:

    for(j=1;j<=(no+1)-i;j++)


    4. Inside the inner loop print value of j :

    printf("%d",j);


    5. For next row print \n.




      Program to print inverted half right side of pyramid number pattern:


    #include<stdio.h>

    void main()

    {

    int i,j,no;

    printf("Enter the number:\n");

    scanf("%d",&no);

    /*Run outer loop from 1 to no*/

    for(i=1;i<=no;i++)

    {   

      /*Run inner loop from 1 to (no+1)-i*/

    for(j=1;j<=(no+1)-i;j++)

    {

    /*Print value of j*/ printf("%d",j);

    }

    /*For next row*/ printf("\n");

    }


    }



    Above program shows the following output:


    C program to print inverted half right side of pyramid number pattern







    C program to print inverted half left side of the pyramid number pattern

    Ex: Write a C program to print inverted half left side of the pyramid number pattern. How to write a C program to print inverted half left side of the pyramid number pattern. C program to print inverted half left side of the pyramid number pattern.



    Input from user:

    Enter the number:

    5


    Expected output:

    12345

      1234

        123

          12

            1



      Step by step logic of the given program:


    1. Accept input number from user declare variable say no.


    2. Run outer for loop from 1 to no which shows number of rows:

    for(i=1;i<=no;i++)


    3. Run inner for loop from 1 to i-1 to print spaces: 

    for(j=1;j<i;j++)

    {

    printf(" ");

    }


    4. Run another one inner for loop from 1 to (no+1)-i to print star:

    for(k=1;k<=(no+1)-i;k++)

    {

    printf("%d",k);

    }


    5. For next row print \n.



      Program to print inverted half left side of the pyramid number pattern:


    #include<stdio.h>

    void main()

    {

    int i,j,k,no;

    printf("Enter the number:\n");

    scanf("%d",&no);

    /*Run loops*/

    for(i=1;i<=no;i++)

    {

         /* Print spaces from second row, in incremented manner*/

    for(j=1;j<i;j++)

    {

    printf(" ");

    }

         /* Print numbers in decremented manner*/

    for(k=1;k<=(no+1)-i;k++)

    {

    printf("%d",k);

    }


            /* For next row*/

    printf("\n");

    }


    }



    Above program shows the following output:


    C program to print inverted half left side of the pyramid number pattern



    C program to print half left side of pyramid number pattern

    Ex: Write a C program to print half left side of pyramid number pattern. How to write a C program to print half left side of pyramid number pattern. C program to print half left side of pyramid number pattern.


    Input from user: 

    Enter the number: 5


    Expected output:

             1

           12

         123

       1234

     12345



      Step by step logic of the given program:


    This pattern is same as previous exercises star pattern:

             *

           **

         ***

       ****

     *****


    Difference is:

    In this pattern we only need to replace the * with value of .


    1. Accept input number from user declare variable say no.


    2. Run outer loop from 1 to no which shows the number of rows in pattern:

    for(i=1;i<=no;i++)


    3. Run first inner loop from i to no-1 to print spaces:

    for(j=i;j<no;j++)

    {

    printf(" ");

    }


    4. Run second inner loop from 1 to i and print value of k:

    for(k=1;k<=i;k++)

    {

            printf("%d",k);

    }


    5. For next row print \n.



      Program to print half left side of pyramid number pattern:


    #include<stdio.h>

    void main()

    {

    int no,i,j,k;

    printf("Enter the number:\n");

    scanf("%d",&no);

    /*Run loops*/

    for(i=1;i<=no;i++)

    {

        for(j=i;j<no;j++)

    {

      /*Print spaces*/

        printf(" ");

    }

    for(k=1;k<=i;k++)

    {

    /*Print value of k*/    

                     printf("%d",k);

    }

    /*For new row*/ printf("\n");


    }


    }



    Above program shows the following output:


    C program to print half left side of pyramid number pattern



    C program to print half right side of pyramid number pattern

    Ex: Write a C program to print half right side of pyramid number pattern. How to write a C program to print half right side of pyramid number pattern. C program to print half right side of pyramid number pattern. 


    Input from user:

    Enter the number: 5


    Expected output:

    1

    12

    123

    1234

    12345



      Step by step logic of the given program:


    This pattern is same as previous exercises star pattern:

    *

    **

    ***

    ****

    *****


    Difference is:

    In this pattern we only need to replace the * with value of j .


    1. Accept input number from user declare variable say no.


    2. Run outer for loop from 1 to no which shows rows in pattern:

    for(i=1;i<=no;i++)


    3. Run inner for loop from 1 to  i which shows columns in pattern:

    for(j=1;j<=i;j++)


    4. Inside the inner loop print value of j :

    printf("%d",j);


    5. For next row print \n.




      Program to print half right side of pyramid number pattern:


    #include<stdio.h>

    void main()

    {

    int no,i,j;

    printf("Enter the number: ");

    scanf("%d",&no);

    /*Run outer loop from 1 to no*/

    for(i=1;i<=no;i++)

    {

    for(j=1;j<=i;j++)

    {

    /*Print value of j*/ printf("%d",j);

    }

    /*For new row*/ printf("\n");

    }

    }


    Above program shows the following output:


    C program to print half right side of pyramid number pattern