Thursday, May 18, 2017

C Programming

C is a general-purpose programming language used for wide range of applications from Operating systems like Windows and iOS to software that is used for creating 3D movies.
C programming is highly efficient. That’s the main reason why it’s very popular despite being more than 40 years old.
Standard C programs are portable. The source code written in one system works in another operating system without any change.
As mentioned, it’s a good language to start learning programming. If you know C programming, you will not just understand how your program works, but will also be able to create a mental picture on how a computer works.
C is closely associated with Unix Operating system

Development of Unix System

The PDP-11 version of Unix system was written in assembly language. Assembly languages are low-level programming languages that are specific to a particular computer architecture. They are hard to write and understand.
The developers of Unix Operating system (including Dennis Ritchie and Stephen C. Johnson) decided to rewrite the system in B language. However, B couldn’t suffice some of the features of PDP-11, which led to the development of C.
In 1972, the development of C started on the PDP-11 Unix system. A large part of Unix was then rewritten in C. By 1973, C was powerful enough to be used in Unix Kernel. Dennis Ritchie and Stephen C. Johnson made further changes to the language for several years to make it portable in Unix Operating system.

First Book on C Programming

In 1978, the first book of C programming, The C Programming Language, was published. The first edition of the book provided programmers informal specification of the language. Written by Brian Kernighan and Dennis Ritchie, this book is popular among C programmers as "K&R".

ANSI C

With the rapid growth of C language for several years, it was time for language to get it standardized.
C89. The first standard of C was published by American National Standards Institute (ANSI) in 1989. This version is commonly popular as C89.
C99. In late 1990’s, several new features like inline functions, several new data types and flexible array-members were added to the C standard. This is commonly known as C99.
C11. The C11 standard has new features like type generic macros, atomic operations, anonymous structures that doesn’t exist in C99.
All these three standards are also known by the name of ANSI C.
“Standard C programs are portable”. This means, the programs that follow ANSI C standard are portable among operating systems.
If you are new to programming, it’s advisable to follow the standard (ANSI C in case of C programming) that is accepted everywhere. It will help you learn the language the way it was intended.
You will learn to write a “Hello, World!” program in this section.

Why “Hello, World!” program?

“Hello, World!” is a simple program that displays “Hello, World!” on the screen. Since, it’s a very simple program, it is used to illustrate the basic syntax of any programming language.
This program is often used to introduce programming language to a beginner. So, let’s get started.
#include <stdio.h>
int main()
{
    printf("Hello, World!\n");
    return 0;
}

How “Hello, World!” program works?

Include stdio.h header file in your program.
C programming is small and cannot do much by itself. You need to use libraries that are necessary to run the program. The stdio.h is a header file and C compiler knows the location of that file. To use the file, you need to include it in your program using #include preprocessor.
Why do you need stdio.h file in this program?
In this program, we have used printf() function which displays the text inside the quotation mark. Since printf() is defined in stdio.h, you need to include stdio.h.
The main() function
In C programming, the code execution begins from the start of main() function (doesn’t matter if main() isn’t located at the beginning).
The code inside the curly braces { } is the body of main() function. The main() function is mandatory in every C program.
int main() {
}
This program doesn’t do anything but, it’s a valid C program.
The printf() function
The printf() is a library function that sends formatted output to the screen (displays the string inside the quotation mark). Notice the semicolon at the end of the statement.
In our program, it displays Hello, World! on the screen.
Remember, you need to include stdio.h file in your program for this to work.
The return statement
The return statement return 0; inside the main() function ends the program. This statement isn’t mandatory. However, it’s considered good programming practice to use it.

Key notes to take away

  • All C program starts from the main() function and it’s mandatory.
  • You can use the required header file that’s necessary in the program. For example: To use sqrt() function to calculate square root and pow() function to find power of a number, you need to include math.h header file in your program.
  • C is case-sensitive; the use of uppercase letter and lowercase letter have different meanings.
  • The C program ends when the program encounters the return statement inside the main()function. However, return statement inside the main function is not mandatory.
  • The statement in a C program ends with a semicolon.
INTRODUCTION
  • Keywords & Identifier
  • Variables & Constants
  • C Data Types
  • C Input/output
  • C Operators
  • Basic Examples
FLOW CONTROL
  • if...else Statement
  • C for Loop
  • C while Loop
  • break and continue
  • switch Statement
  • Decision Examples
FUNCTIONS
  • Functions Introduction
  • User-defined Function
  • Function Types
  • Recursion in C
  • Variable Scope
  • Function Examples
ARRAYS
  • C Arrays Introduction
  • Multidimensional Array
  • Arrays & Functions
  • Strings in C
  • String Functions
  • Array Examples
C POINTERS
  • C Pointers
  • Pointers & Arrays
  • Pointers & Functions
  • Memory Management
  • Pointer Examples
STRUCTURE & FILE
  • C Structure
  • Structure & Pointers
  • Structure & Functions
  • C Unions
  • Structure Examples
  • Files Handling

     

Programs to print triangles using *, numbers and characters


Example 1: Program to print half pyramid using *

*
* *
* * *
* * * *
* * * * *
Source Code
#include <stdio.h>
int main()
{
    int i, j, rows;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=1; i<=rows; ++i)
    {
        for(j=1; j<=i; ++j)
        {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}

Example 2: Program to print half pyramid a using numbers

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Source Code
#include <stdio.h>
int main()
{
    int i, j, rows;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=1; i<=rows; ++i)
    {
        for(j=1; j<=i; ++j)
        {
            printf("%d ",j);
        }
        printf("\n");
    }
    return 0;
}

Example 3: Program to print half pyramid using alphabets

A
B B
C C C
D D D D
E E E E E
Source Code
#include <stdio.h>
int main()
{
    int i, j;
    char input, alphabet = 'A';

    printf("Enter the uppercase character you want to print in last row: ");
    scanf("%c",&input);

    for(i=1; i <= (input-'A'+1); ++i)
    {
        for(j=1;j<=i;++j)
        {
            printf("%c", alphabet);
        }
        ++alphabet;

        printf("\n");
    }
    return 0;
}

Programs to print inverted half pyramid using * and numbers


Example 4: Inverted half pyramid using *

* * * * *
* * * *
* * * 
* *
*
Source Code
#include <stdio.h>
int main()
{
    int i, j, rows;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=rows; i>=1; --i)
    {
        for(j=1; j<=i; ++j)
        {
            printf("* ");
        }
        printf("\n");
    }
    
    return 0;
}

Example 5: Inverted half pyramid using numbers

1 2 3 4 5
1 2 3 4 
1 2 3
1 2
1
Source Code
#include <stdio.h>
int main()
{
    int i, j, rows;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=rows; i>=1; --i)
    {
        for(j=1; j<=i; ++j)
        {
            printf("%d ",j);
        }
        printf("\n");
    }

    return 0;
}

 Programs to display pyramid and inverted pyramid using * and digits


Example 6: Program to print full pyramid using *

        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
Source Code
#include <stdio.h>
int main()
{
    int i, space, rows, k=0;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=1; i<=rows; ++i, k=0)
    {
        for(space=1; space<=rows-i; ++space)
        {
            printf("  ");
        }

        while(k != 2*i-1)
        {
            printf("* ");
            ++k;
        }

        printf("\n");
    }
    
    return 0;
}

Source Code

Example 7: Program to print pyramid using numbers


        1
      2 3 2
    3 4 5 4 3
  4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
#include <stdio.h>
int main()
{
    int i, space, rows, k=0, count = 0, count1 = 0;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=1; i<=rows; ++i)
    {
        for(space=1; space <= rows-i; ++space)
        {
            printf("  ");
            ++count;
        }

        while(k != 2*i-1)
        {
            if (count <= rows-1)
            {
                printf("%d ", i+k);
                ++count;
            }
            else
            {
                ++count1;
                printf("%d ", (i+k-2*count1));
            }
            ++k;
        }
        count1 = count = k = 0;

        printf("\n");
    }
    return 0;
}

Example 8: Inverted full pyramid using *


* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *
Source Code
#include<stdio.h>
int main()
{
    int rows, i, j, space;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=rows; i>=1; --i)
    {
        for(space=0; space < rows-i; ++space)
            printf("  ");

        for(j=i; j <= 2*i-1; ++j)
            printf("* ");

        for(j=0; j < i-1; ++j)
            printf("* ");

        printf("\n");
    }

    return 0;
}

Example 9: Print Pascal's triangle


           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1 
Source Code
#include <stdio.h>
int main()
{
    int rows, coef = 1, space, i, j;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=0; i<rows; i++)
    {
        for(space=1; space <= rows-i; space++)
            printf("  ");

        for(j=0; j <= i; j++)
        {
            if (j==0 || i==0)
                coef = 1;
            else
                coef = coef*(i-j+1)/j;

            printf("%4d", coef);
        }
        printf("\n");
    }

    return 0;
}

Example 10: Print Floyd's Triangle.

1
2 3
4 5 6
7 8 9 10
Source Code
#include <stdio.h>
int main()
{
    int rows, i, j, number= 1;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=1; i <= rows; i++)
    {
        for(j=1; j <= i; ++j)
        {
            printf("%d ", number);
            ++number;
        }

        printf("\n");
    }

    return 0;
}


No comments:

Post a Comment

JDBC  Connectivity With MySQL