codescracker
c

C Programming Examples



« C Tutorial Next Program »

Programming is like Mathematics. As in Mathematics, theory is not enough. Because to understand the topic clearly, you must have to practice solving the problem related to the topic. Just like this, in programming also, to learn any topic, you must have to practice coding (with yourself) related to the topic.

So here we have listed more than 200 programs with its output and step by step explanation. You can learn here, from simplest C programs to shutdown your computer using C program.

But before going through all those programs. Let's first take a look at some of the interesting programs given here. I'm sure that, after having a look at the output of these programs listed here, your interest in programming gets increased. Don't be panic while looking at the code (the code, you are not getting an idea that what is going inside). Because you will learn more than you think about C programming over here.

The step by step explanation of all the programs starts from next page. But here, these programs are just a demo, you will learn much more about programs starts from next page.

I'm sure that, after learning all the programs (starts from next page), you will be able to understand the programs given below and can also modify these with yourself. And may be you can create some programs that are beyond my thinking.

And I'm also sure, you can create a lot of programs that are not listed here. But before going to the demo programs listed in this article. Let's have a look at some popular programs available in C language:

  1. Add two Numbers in C
  2. Leap Year Program in C
  3. Student Grade Program in C
  4. Greatest of Three Numbers in C
  5. Factorial of Number in C
  6. Fahrenheit to Centigrade in C
  7. Decimal to Binary Conversion in C
  8. Hexadecimal to Octal in C
  9. Pattern Printing Programs in C
  10. Binary Search Program in C
  11. Bubble Sort Program in C
  12. Matrix Multiplication Program in C
  13. Compare two String Program in C
  14. Delete Vowels from String in C
  15. Remove Spaces from String in C
  16. Convert Uppercase to Lowercase in C
  17. Copy File in C
  18. Shutdown Computer in C

C Examples - Demo Programs

As I've already told at the early of this article, after having a look at the output of these programs, you will get more interest to learn C. Let's get started with one of the simplest program available in C.

C Program Example No.1

Following is the simplest C program which will print "Hello Compiler, I am C" on the screen :

#include<stdio.h>
#include<conio.h>
int main()
{
    printf("Hello Compiler, I am C");
    getch();  // holds output screen, until user press a key
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is the output produced by this program:

simplest c program example

C Program Example No.2

Here is the second program, where user allows to enter any input.

#include<stdio.h>
#include<conio.h>
int main()
{
    char name[20];
    printf("Enter your Name: ");
    scanf("%s", name);
    printf("Hello, %s\nWelcome to codescracker.com", name);
    getch();
    return 0;
}

Here is the sample run of previous (above) program:

c programming examples with output

C Program Example No.3

This is password protected program. That is, user allows to create a password and then to enter the same to see the result.

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char pass[20], e_pass[20];
    int num1, num2, sum;
    printf("Create a Password: ");
    scanf("%s", pass);
    printf("\nEnter any two number to Add: ");
    scanf("%d %d", &num1, &num2);
    printf("\nEnter password to see the Result: ");
    scanf("%s", e_pass);
    if(!strcmp(pass, e_pass))
    {
        sum = num1 + num2;
        printf("\n%d + %d = %d", num1, num2, sum);
    }
    else
        printf("\nSorry! You entered a Wrong Password");
    getch();
    return 0;
}

The sample run of the above C program in both cases (if user enters either correct or wrong password) is shown here. This is first output, if user enters wrong password:

c programs list

This is second one, if user enters correct password:

c all programs

C Program Example No.4

This program receives a string input from user and compares the string with yes to print the message according to the input. Don't worry, you'll get to learn each and every thing one by one, starting from next page.

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str[10];
    printf("Are you a Programmer ? ");
    scanf("%s", str);
    if(!strcmp("yes", str))
        printf("\nWhat a True Mind, You have!");
    else
        printf("\nSorry! You are telling a lie.");
    getch();
    return 0;
}

This is the first output:

c programs

If you are here, then you are interested to learn programming. So I'm sure that you're a programmer, then why you entered No. Try to go for yes. This is second output:

c example programs

C Program Example No.5

Here is another program in C that checks with invited members and prints whether the current user is invited or not based on user input.

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char arr[6][20] = {"programmer", "developer", "engineer",
                        "student", "teacher", "professor"};
    char name[25];
    int found=0;
    printf("Who are You ? ");
    scanf("%s", name);
    for(int i=0; i<6; i++)
    {
        if(!strcmp(name, arr[i]))
        {
            printf("\nCongratulation!!");
            printf("\nYou're invited from codescracker.com.");
            found=1;
            break;
        }
        else
            found++;
    }
    if(found != 1)
        printf("\nSorry!\nYou're not invited.");
    getch();
    return 0;
}

The sample run of the above C program in both cases is shown here. This is first output:

c programs with output

This is second one:

c examples with output

C Program Example No.6

This is the last demo program. That is, from next page, you will see programs with its output and step by step explanation of the code.

#include<stdio.h>
#include<conio.h>
int main()
{
    int starTot, rowTot, i, j, starInOneRow, rem;
    printf("How many stars you want to print ? ");
    scanf("%d", &starTot);
    printf("Upto how many rows ? ");
    scanf("%d", &rowTot);
    starInOneRow = starTot/rowTot;
    rem = starTot%rowTot;
    printf("\n");
    i=0;
    while(i<rowTot)
    {
        if(i!=0)
            printf("\n");
        for(j=0; j<starInOneRow; j++)
            printf("* ");
        i++;
    }
    if(rem!=0)
    {
        for(i=0; i<rem; i++)
            printf("* ");
    }
    getch();
    return 0;
}

The snapshot given below is its sample run:

c programming examples

This is the second sample run:

c examples

Now from next, you will get started to enter into C programming world, from very basic program to an advance level. Step by step explanation of all the programs are given.

C Online Test


« C Tutorial Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker