Thursday, 19 May 2016

Introduction to 'C' Programming Language.


Programming in ANSI ‘C’.

ANSI: American National Standard Institute.
ISO: International Standards Organization.

History of ANSI C.




C: C is a programming language, it’s used to communication with computer.

Developer of C: C is developed by Danish Ritchie.

·         C   is portable, efficient and fast and flexible.
·         Stdio.h:  It look for the stdio.h file and efficiently  copy-pastes it in the place of this #include statements. This file contains so called functions proto types of functions such as printf(), scanf(),… so that compiler known’s what are their parameters and return value.
·         Stdio.h meaning: Standard Input/Output and is header.
·         Conio.h: It is a header file used mostly by MS-DOS compilers to provide console input/output. It is not part of standard library or ISO C, nor is it defined  by POSIX.
·         conio.h meaning:

Characteristics.
1)      Portable: C program written for one computer can run one any other computer with little modification.
2)      Structured programming language: includes functions & blocks modules structure.
3)      Efficient & Fast: This  is because of variety of data types & operators.
4)      Another Important Feature: is its quality to extend itself C Program is a collection of functions &we can add our functions as per-requirements.
·         Two types of functions.
i)                    User defined: created by uses.
ii)                   Library: Inbuilt in system.

·         Structure of C Programming with example.

i)                       Basic syntax for writing C Program.


Documentation section
Optional
Link section
Compulsory
Definition section
Optional
Global declaration section
Optional
Main() function section
Compulsory
{
          Declaration part
          Executable part
}
Subprogram Section
Optional

(Depends on program)
User define functions
Function-1
Function-2
……………..
Function N

Procedure to create C Program.
1)      Install  Turbo C
2)      Open Turbo C
3)      New File
4)      Write a program & press F2 or Save
5)      Compile to press Compiler or Alt+F9
6)      Run or press Ctrl+F9

Execution of C Program.

Steps of execution.

1)      Creating the program
2)      Compiling the program
3)      Linking the program
4)      Run(execute) the program

An operating executes the program it is a program that controls the entire operation of a computer system. All outputs are connected with OS.
A programming language is used to …

i)                    A programming language is used to help the processing of some types of data (character, numbers & strings) to generate output, which is known as program.
ii)                   The processing of data is done using certain instruction set which is known as program.
iii)                 These instruction are created using certain symbols & words according some rigid rules known as syntax rules.
Character Set.

1)      The characters can be used to generate numbers, words & expressions depend on the computer on which it Runs.
2)      The character set is different for computers, minicomps, microcomps, etc.
3)      The characters in are grouped of
§  Letters
§  Digits
§  Special Characters
§  White Space
4)      White space is ignored by compiler during execution.
5)      White space is allowed during use of string.
Trigraph Sequence .

i)                    Some non English keyboards do not support some characters.
ii)                   If we want to enter certain characters that can not available on keyboard they Trigraph Sequence is used.
iii)                 Syntax for Trigraph Sequence is
·         First two question mark
·         Third is character
iv)                 For Example if # is not available on keyboard, we can enter it using Trigraph of
           ??=        -# number sign
Other examples

??=
#  
Sign
??(
[
Brackets
??)
]
Right Bracket
??<
{
Left Brace
??>
}
Right Brace
??!
|
Vertical Bar
??/
\
Back Slash
??-
~
Tidle

Character Set and it’s Types.

    
        I.            Letters
      II.            Digits
    III.            Special Character
    IV.            White Space

1)      Letters
                                           I.            Upper Case A…..Z
                                          II.            Lower Case a……z
2)      Digits
   i)        All digits 0…….9

3)      Special character

,               Comma
&             Ampersand
.               Period
^              Caret
;               Semicolon
*        Asterisk
:               Colon
-         Minus Sign
?              Question Mark
+              Plus Sign
‘               Apostrophe
<             Opening Angle Bracket or Less Than Sign
>       Closing Angle Bracket or Greater Than Sign
“              Quotation Mark
!              Exclamation Mark
(              Left Parenthesis
)              Right Parenthesis
|              Vertical Bar
/              Slash
[              Left Bracket
]              Right Bracket
~             Tilde
_             Under Score
{              Left Brace
}              Right Brace
$              Dollar Sign
%            Percentage Sign
#             Number Sign





      4)      White Space                                                                  
·         Horizontal Tab
·         Carriage Return
·         New Line
·         Form Feed
·         Blank Space


 Variables.
1)      Variable is used to store data values.
2)      It’s value can change throughout program.

Rules for variable Names

1)      It begins with letter or underscore.
2)      Space is not allowed.
3)      It should not be keyword.
4)      Length is up to 31 characters.

     Syntax:
       Data type V1,V2,…Vn;

       Declaration of variable:
     Int a;

It tells compiles about
1)      Name of variable.
2)      Which type of data it holds.

Format of C Programs.



The main function.


     ·         Main()
     ·         Int main()
     ·         Void main()
     ·         Main(void)
     ·         Int main(void)

BASIC STRUCTURE OF C PROGRAM.

Documentation section
Link section
Definition section
Global declaration section
Main() function section
{
          Declaration part
          Executable part
}
Subprogram
Function-1
Function-2
……………..
Function N

Size and ranger of basic data types on 16-bit machines.

Data Type
Range of Values
Char
Int
Float
Double
-128 to 127
-32,768 to 32,767
3.4e-38 to 3.4e+38
1.7e-308 to 1.7e+308

1.     Integer types
·         Short int
·         Int
·         Long int

2.     Floating point type
·         Float
·         Double
·         Long double

Introduction to Flowchart.




‘C’ Program and C programs coding

·     Write a program to print  “Hello World”.
#include<stdio.h>
#include<conio.h>
Main()
{
                Clrscr();

                Printf(“Hello World”);

                Getch();
}


·     Write a program to demonstrate the use of printf and scanf statement to read and print values of variable of different data types.

#include<stdio.h>
#include<conio.h>
main();
{
                int a;
                float b;
                char c;
printf(“Enter the value of integer: “);
scanf(“%d”,&a);

printf(“Enter the value of character: “);
scanf(“%c”,&b);

printf(“Enter the value of float: “);
scanf(“%f”,&c);

                getch();
}


·     Write a program to read two numbers from key board and print the addition, subtraction and multiplication.

#include<stdio.h>
#include<conio.h>
Main();
{
                Int a,b,c;
                Clrscr();
                               
                                Printf(“Enter the value of  A: “);
                                Scanf(“%d”,&a);

                                Printf(“Enter the value of B: “);
                                Scanf(“%d”,&b);
                               
                                C=a+b;
                                Printf(“Addition of %d and %d is %d”,a,b,c);

                                C=a-b;
                                Printf(“Subtraction  of %d and %d is %d”,a,b,c);

                                C=a*b;
                                Printf(“Multiplication  of %d and %d is %d”,a,b,c);
                Getch();

}


·     Write a program to find area of circle.
#include<stdio.h>
#include<conio.h>
#define PI 3.14
Main();
{
                Int  r;
                Float area;
                Clrscr();

                                Printf(“Enter the radius of circle: “);
                                Scanf(“%d”,&r);

                                area=PI*r*r;

                                printf(“\nArea of circle is %f”,area);
                getch();
}


·     Write a program to swap two numbers without using third variable.

#include<stdio.h>
#include<conio.h>
main()
{
                int num1,num2;
                clrscr();

                printf("Enter the vale of First Number: ");
                scanf("%d",& num1);
                printf("Enter the vale of Second Number: ");
                scanf("%d",& num2);

                num1=num1+num2;
                num2=num1-num2;
                num1=num1-num2;

                printf("\n\nFirst Number After Swap: %d",num1);
                printf("\nSecond Number After Swap: %d",num2);

                getch();
}


·     Write a program to find the minimum out of 3 numbers.

#include<stdio.h>
#include<conio.h>
main()
{
                int a,b,c;
                clrscr();
  
                printf("Enter first number: ");
                scanf("%d",&a);
                printf("Enter second number: ");
                scanf("%d",&b);
                printf("Enter third number: ");
                scanf("%d",&c);

   if (a<b && a<c)
      {
                 printf("\nFirst number is minimum.",a);
      }
   else if (b<a && b<c)
      {
                 printf("\nSecond number is minimum.",b);
      }
   else
                 printf("\nThird number is minimum.");

                 getch();
}


·     Write a program to perform shift left and shift right, bitwise AND, OR and XOR operator on given 2 numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
                int a,b;
                clrscr();

                printf("Enter the value of Num1: ");
                scanf("%d",&a);
                printf("\nEnter the value of Num2: ");
                scanf("%d",&b);

                printf("\n Shift right by one: %d",a>>1);
                printf("\n Shift left by one; %d",a<<1);

                printf("\n Num1 AND Num2 is %d",a&b);
                printf("\n Num1 OR Num2 is %d",a|b);
                printf("\n Num1 XOR Num2 is %d",a^b);

                getch();
}


·     Write a program to demonstrate the use of assignment operator on given 2numbers. (+=, -=, /=, *=, %=).

#include<stdio.h>
#include<conio.h>
void main()
{
                int a,c;
                clrscr();

                printf("Enter the value of A: ");
                scanf("%d",&a);
                printf("\nEnter the vale of C: ");
                scanf("%d",&c);

                c+=a;
                printf("\nAddition is c=%d\n",c);

                c-=a;
                printf("Subtraction is c=%d\n",c);

                c*=a;
                printf("Multiplication is c=%d\n",c);

                c/=a;
                printf("Division is c=%d\n",c);

                c%=a;
                printf("Modulas is c=%d",c);

                getch();
}


·     Write a program to illustrate the declaration and the access of enumerated data type.

#include<stdio.h>
#include<conio.h>
void main()
{
                enum months {Jan=1,Feb,Mar,Apr,May,Jun,July,Aug,Sep,Oct,Nov,Dec};
                enum months month;
                clrscr();

                printf("Month March is ");
                printf("%d",month=Mar);

                getch();
}


·     Write a program to Determine whether the person is eligible for voting or not.

#include<stdio.h>
#include<conio.h>
void main()
{
                int age;
                clrscr();

                printf("Enter your age:");
                scanf("%d",&age);

                if(age>=18)
                                {
                                                printf("You are eligible to vot");
                                }
                else
                                {
                                                printf("You are not eligible to vot");
                                }
                getch();
}


·     Write a program to whether the given year is leap year or not.

#include<stdio.h>
#include<conio.h>
void main()
{
                int year;
                clrscr();

                printf("Enter a year: ");
                scanf("%d",&year);

    if(year%100==0)
                {
                   printf("%d is not leap year.",year);
                }
    else if(year%4==0)
                {
                   printf("%d is leap year.",year);
                }
    else if(year%400==0)
                {
                   printf("%d is leap year.",year);
                }
    else
                   printf("%d is not leap year.",year);

    getch();
}


·     Write a program to Check whether given number is positive, negative or zero.

#include<stdio.h>
#include<conio.h>
void main()
{
                int a;
                clrscr();

                printf("Enter the number: ");
                scanf("%d",&a);

                   if(a>0)
                      {
                                printf("%d number is positive.",a);
                      }
                   else if(a<0)
                      {
                                printf("%d number is negative.",a);
                      }
                   else
                                printf("%d number is zero.",a);

                   getch();
}


·     Write a program to find the largest number out of 3 without using nested if.

#include<stdio.h>
#include<conio.h>
Main()
{
                Int a,b,c;
                Clrscr();

                                Printf(“Enter the value of A: “);
                                Scanf(“%d”,&a);

                                Printf(“Enter the value of B: “);
                                Scanf(“%d”,&b);

                                Printf(“Enter the value of C: “);
                                Scanf(“%d”,&c);

                If (a>b  && a>c)
                                {
                                                Printf(“%d is Largest Value.”,a);
                                }
                Else if (b>a  && b>c)
                                {
                                                Printf(“%d is Largest Value.”,b);
                                }
                Else
                                                Printf(“%d is Largest Value.”,c);

                Getch();
}


·     Write a program to find whether the given number is prime or not.

#include<stdio.h>
#include<conio.h>
main()
{
                int n,i,c=0;
                clrscr();

                printf("Enter the number: ");
                scanf("%d",&n);

   for (i=1;i<=1;i++)
      {
                 if (n%2==0)
                    {
                      printf("\n%d is prime number.",n);
                      c++;
                      break;
                    }

                 else
                      printf("\n%d is not prime number.",n);
      }

   getch();
}


·     Write a program to demonstrate the use of switch statement without the break statement.

#include<stdio.h>
#include<conio.h>
void main()
{
                int i;
                clrscr();

                printf("Enter any one No. in 1 to 5: ");
                scanf("%d",&i);

   switch(i)
      {
                case 1:
                printf("Case 1");

                case 2:
                printf("Case 2");

                case 3:
                printf("Case 3");

                case 4:
                printf("Case 4");

                default:
                printf("Default");
      }

      getch();
}


·     Write a program to make calculator using switch.
/* Source code to create a simple calculator for addition, subtraction, multiplication and division using switch...case statement in C programming. */

# include <stdio.h>
#include <conio.h>
int main()
{
                char o;
                float num1,num2;
                clrscr();

                                printf("Enter operator either \n'+' Addition or \n'-' Substraction or \n'*' Multiplication or \n'/' Division : ");
                                scanf("%c",&o);

                                printf("Enter First operands: ");
                                scanf("%f,&num1);
                                printf("Enter Second operands: ");
                                scanf("%f,&num2);

                switch(o)
                     {
                case '+':
           
                                                printf("Addition of %.1f and %.1f is %.1f",num1, num2, num1+num2);

                break;
       
                                case '-':

                                printf("Substraction of %.1f and %.1f is %.1f",num1, num2, num1-num2);

                break;
       
                                case '*':

                                printf("Multiplication of %.1f and %.1f is %.1f",num1, num2, num1*num2);

                break;
       
                                case '/':
                               
                                                printf("Division of %.1f and %.1f is %.1f",num1, num2, num1/num2);

                break;

                default:
           
                                printf("Error! Operator is not correct");    // If operator is other than +, -, * or /, error message is shown //

                break;
                     }
    getch();
}


·     Write a program to determine whether an entered character is vowel or not.

#include<stdio.h>
#include<conio.h>
main()
{
                char a;
                clrscr();

                printf("Enter the charactor: ");
                scanf("%c",&a);

   if (a=='a' || a=='e' || a=='i' || a=='o' || a=='u')
                {
                   printf("\n\nCharactor %c is vowel.",a);
                }
   else
                   printf("\n\nCharactor %c is consonant.",a);

                getch();
}


·     Write a program to find a Factorial of given number.

#include<stdio.h>
#include<conio.h>
int main()
{
                int c,n,fact=1;
                clrscr();

                printf("Enter a number to calculate it's factor: ");
                scanf("%d",&n);

   for (c=1;c<=n;c++)
                {
                   fact*= c;
                                printf("\nFactorial of %d = %d",n,fact);
                }
                getch();

}


·     Write a program to print Fibonacci series. 1, 1, 2, 3, 5, ...... N using Do-while loop.

#include<stdio.h>
#include<conio.h>
main()
{
                int a,b,c,n,i;
                clrscr();

                a=0;
                b=1;
                i=3;

                printf("Enter the number of terms: ");
                scanf("%d",&n);

                printf("\n %d \n %d",a,b);

   do
    {
                c=a+b;
                printf("\n %d",c);
                a=b;
                b=c;
                i=i+1;
    }
   while (i<=n);

                getch();
}


·     Write a program to print the following Patterns.
*
**
***
****

#include<stdio.h>
#include<conio.h>
main()
{
                int n,i,j;
                clrscr();

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

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

                getch();
}


·     Write a program to print the following Patterns.
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5

#include<stdio.h>
#include<conio.h>
main()
{
                int i,j,n=0;
                clrscr();

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

      getch();
}


·     Write a program to print the following Patterns.

1
0 1
1 0 1
0 1 0 1


#include<stdio.h>
#include<conio.h>
void main()
{
                int i,j,s;
                clrscr();

   for (i=1;i<=5;i++) //Maintains rows.
      {
                 for (s=1;s<=5-i;s++) //Maintains spaces.
                    {
                      printf(" ");  //Print one space.
                    }
                                for (j=1;j<=i;j++) //Maintains columns.
                                   {
                                      if ((i+j)%2==0)
                                                 {
                                                   printf("1 ");
                                                 }
                                      else
                                                   printf("0 ");
                                   }
                                                   printf("\n");
      }

      getch();
}


·     Write a program to read array of integers and print it.

#include<stdio.h>
#include<conio.h>
void main()
{
                int n[5],i;
                clrscr();

   for (i=0;i<=4;i++)
      {
                printf("Enter element Number %d: ",i+1);
                scanf("%d",&n[i]);
      }
                 for (i=0;i<=4;i++)
                    {
                       printf("\nEntered element %d is %d",i+1,n[i]);
                    }
      getch();
}



  •      Write a program that takes N numbers from user and display sum and average of it.

#include<stdio.h>
#include<conio.h>
main()
{
                int i;
                float n[5],sum=0;
                clrscr();

   for (i=0;i<=4;i++)
      {
                 printf("Enter element number %d: ",i+1);
                 scanf("%f",&n[i]);
                 sum=sum+n[i];
      }
                 printf("\nThe sum is %.2f",sum);
                 printf("\nThe average is %.2f",sum/5);

      getch();
}


·     Write a program to find the occurrence of a given numbering an array of 10 elements.

#include<stdio.h>
#include<conio.h>
void main()
{
                int a[10],n,i,p,count=0;
                clrscr();

                printf("\n Enter the size of array: ");
                scanf("%d",&n);

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


   for (i=0;i<n;i++)
      {
                 scanf("\n%d",&a[i]);
      }
                 printf("\nEnter the element to be searched: ");
                 scanf("\n%d",&p);

                                for (i=0;i<n;i++)
                                   {
                                      if (p==a[i])
                                                {
                                                  count++;
                                                }
                                   }
                                   printf("\n %d exists %d times in the array.",p,count);

                 getch();
}



  •      Write a program to add two matrices.

#include <stdio.h>

int main()
{
   int m, n, c, d, first[10][10], second[10][10], sum[10][10];

   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix\n");

   for (c = 0; c < m; c++)
      for (d = 0; d < n; d++)
         scanf("%d", &first[c][d]);

   printf("Enter the elements of second matrix\n");

   for (c = 0; c < m; c++)
      for (d = 0 ; d < n; d++)
            scanf("%d", &second[c][d]);

   printf("Sum of entered matrices:-\n");

   for (c = 0; c < m; c++) {
      for (d = 0 ; d < n; d++) {
         sum[c][d] = first[c][d] + second[c][d];
         printf("%d\t", sum[c][d]);
      }
      printf("\n");
   }

   return 0;
}



  •               Write a program of matrix multiplication of two matrices.

#include <stdio.h>
int main()
{
    int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
    printf("Enter rows and column for first matrix: ");
    scanf("%d%d", &r1, &c1);
    printf("Enter rows and column for second matrix: ");
    scanf("%d%d",&r2, &c2);

/* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */
    while (c1!=r2)
    {
        printf("Error! column of first matrix not equal to row of second.\n\n");
        printf("Enter rows and column for first matrix: ");
        scanf("%d%d", &r1, &c1);
        printf("Enter rows and column for second matrix: ");
        scanf("%d%d",&r2, &c2);
    }

/* Storing elements of first matrix. */
    printf("\nEnter elements of matrix 1:\n");
    for(i=0; i<r1; ++i)
    for(j=0; j<c1; ++j)
    {
        printf("Enter elements a%d%d: ",i+1,j+1);
        scanf("%d",&a[i][j]);
    }

/* Storing elements of second matrix. */
    printf("\nEnter elements of matrix 2:\n");
    for(i=0; i<r2; ++i)
    for(j=0; j<c2; ++j)
    {
        printf("Enter elements b%d%d: ",i+1,j+1);
        scanf("%d",&b[i][j]);
    }

/* Initializing elements of matrix mult to 0.*/
    for(i=0; i<r1; ++i)
    for(j=0; j<c2; ++j)
    {
       mult[i][j]=0;
    }

/* Multiplying matrix a and b and storing in array mult. */
    for(i=0; i<r1; ++i)
    for(j=0; j<c2; ++j)
    for(k=0; k<c1; ++k)
    {
        mult[i][j]+=a[i][k]*b[k][j];
    }

/* Displaying the multiplication of two matrix. */
    printf("\nOutput Matrix:\n");
    for(i=0; i<r1; ++i)
    for(j=0; j<c2; ++j)
    {
        printf("%d  ",mult[i][j]);
        if(j==c2-1)
            printf("\n\n");
    }
    return 0;
}




No comments:

Post a Comment