Search & Find out

Monday, November 26, 2012

Basic of C Program

Basics of C

  Format :
                  
     #include  "stdio.h"                           \* Header files*/
    #include  "conio.h "
   
     void main()                \* Main function, the execution of the program       
    {                       starts from here */
      
                     --------------------
        --------------------                 \*  statements or body of the programe */
        --------------------
                 
                 }

   
    The body of the programe include declaration of variables, statements &....many more..

      Declaration of  variable :
                              Declaring the variable means declaring the datatype for that variable,
       in which the variable to be stored.
       (A variable is an identifier that is used to represent some specified type of information with in a
         designated portion of the program.) 

    int a;     -->   Interger value, a is a variable which is used to store the integer value.

    char b;    -->   Character , b is a variable which is used to store the character.

    float c;     -->   Real constant value, c is a variable which is used to store the real constant
               value or float value.

     Here we can initialise the values to the variables
    i.e
        int a=10;   
        char b={z};
        float c=10.205;

      Statements of the program :
            Statements or body of the program include lot of functions, like
       printf(), scanf(), arrays[], user defined functions, pointers, structures, ... and many more..


     printf() :
      This function is used to printing the statements, values, etc..
             E.g :
             printf("Hello, world");

    O/p:  Hello, world

             E.g :
             printf("The value is %d",a);

    O/p:  10                   /* Here the value of the "a" is printed */


    \n     -->     uses to break the line
    \t      -->     uses to give a tab space i.e. space of 8 characters

     scanf() :
      This function is used to accepting the values , etc..
             E.g :
             scanf("%d",&a);

    O/p:  _                                  /* The program is waiting for accepting a integer value */

   
    %d    -->   It accepts only integer values.

    %c    -->   It accepts only a single character.

    %f            -->   It accepts only real constant values.

    We can accept one or more values at a samr time.

     E.g :
             scanf("%d%c%d",&a,&b,&c);

    O/p:  _       (enter)        /* The program is waiting for accepting a integer value */
             _    (enter)        /* The program is waiting for accepting a character */
             _    (enter)           /* The program is waiting for accepting a real constant value */
   
  
      CONDITIONAL STATEMENT:

        This statements are depend upon the conditions
      There are two types:
http://www.brothersoft.com/d.php?soft_id=182798&url=http%3A%2F%2Flfiles3.brothersoft.com%2Fdevelopment%2Fc_c%2Ftc3.zip
       1) if else:
       
    syntax:        
        if(condition)
        {
            ----------------
            ----------------
        }
        else
        {
            ----------------
            ----------------
        }
   
    Here if condition is true then the ' if ' part is execute. if the condition is false then the ' else '
      part is execute.

         2)  switch :
   
    syntax :
        switch(expression \ variable)
        {
              case 1:           
            ----------------
            break;

              case 2:
            ----------------
            break;   

              default :
            ----------------
            break;
        }

        Here if the expresion or condition is declared.
        If condition is true the the case 1 : be execute,
        If condition is false the the case 2 : be execute.

        Here if the variable is declared, then the matching case would be executed.


           LOOP STATEMENTS :
            Loop means continue.... It means the execution of the loop
    statements are stop if and only if the condition in false.
           
    C has provided three types of iteration statements:

    1) for loop.
    2) while loop.
    3) do...while loop.

    1) for loop :
        For loop is a loop statement, In this first the condition is checked, and then the execution will be start.
        If the condition is false then the loop does not execute
        In which the statements are executes till the condition is false.
        In this loop, we can initialise & increment the variables.

        syntax :
            for(initialize; condition ; increment )
            {
                 ----------------------
            }
    E.g :  
       
    #include
    #include
   
    void main()
    {
            int i;

        for(i=0;i<100;i++)
        {
              printf("The value of i is %d",i);   
        }

        getch();
    }

     
         2 ) while loop :
        It is same as for loop, but the formate is different, means
     For loop is a loop statement, In this first the condition is checked, and then the execution will be start.
     If the condition is false then the loop does not execute
     In which the statements are executes till the condition is false.
   
        syntax :
            while (condition)
            {
                  -----------------
                  -----------------
            }

        E.g :
       
                #include
                #include
   
              void main()
             {
                 int i;

        while(i<100 )
        {                                  
                    printf("The value of i is %d",i);   
          }

        getch();
        }

        ...
   
          3) do .. while loop :
        In this loop, first the statements are executed before checking the condition.
    Means if the condition is false then only it will execute atleast one time.

    syntax :
        do
        {   
            ------------
        }
        while(condition);

    E.g :        
   
        do
    {
        printf("The value of i is %d",i);
    }
    while (i<100);
    ...
    ...


 Arrays:
                    An array is data type that uses subscripted variables to make the representation of a large number of homogeneous values possible.

    E.g: 
              int a[3];
              float c[5];

     Types of Arrays:

       1)  Single Dimensional Array:
               It uses only single dimensional ..
    E.g:   a[3]
             b[100]
             c[10]
       
       2) Multi Dimensional Array :
              It uses two or more dimensional i.e it uses two or morw seperate square
          brackets.

    E.g :  
        a[10][10]              -->    It is two dimensional Array.
        b[10][5][14]       -->    It is three dimensional Array.

       3)  Character Array or String :
               A group of characters can be stored in a character array.
           Character array are known as String.
          
    E.g:
                        char a[3]
           char b[100]
           char c[10]   
   
    In this String '\0' is used to indicate the last position of a word or sentence.

    i.e               a[0]  a[1]  a[2]  a[3]  a[4]
            T      E       J       A     \0

      POINTERS:
                Pointers is a variable which contains the address of the another variable.
         Basically it is used to store the memory location.
         Pointer itself might be another pionter.

    E.g:
              int *p;    /*p is declared as a pointer */


    E.g:
               int *p;   
               int a=10;
               p=&a;    /* It is a address operator, here p is stored the addres of the variable a */

                            int **k;            /* k is a pinters pointer , it points address of the pointer which points the address of a variable*/
               int *p;       
              int a=10;
              p=&a;
              k=&p;

           i.e
          a     p      k     /* the variable name */
                                  10           35084          48451        /* the value of the variable contains */
                  35084        48451          52101        /* the location of the variable */
             lets see how it works

    printf("%d",a);    -->    10
    printf("%d",*p);    -->    10
    printf("%d",p);    -->    35084
    printf("%d",*k);    -->    35084
    printf("%d",**k);    -->    10
    printf("%d",k);    -->    48451


          STRUCTURE :
        A structure is a user defined datatype. Structure is a data structure,
            whose individual elements can differ in type.
            It is a combination of several different previously data types, including othere structures.

    E.g:
           #include
           #include

          struct demo    /* "demo" is a name of the structure, we can take any name */
          {
              int a;    /* Here we can only declare & that would be only one variables in one line statement */
              int b;
             float c;
          };   
       
      void main()
     {
          struct demo s,s1;          /* s & s1 are variables, we uses this variable with the variable which are
                      declared in the struct demo */
        s.a=1;
        s.b=15;
        s.c=12.25;
   
        printf("%d\n%d\n%f",s.a,s.b,s.c);
    o/p:  1
            15
            12.25           

  

Read rest of entry

Sunday, December 6, 2009

SUM OF EVEN NUMBERS


Write a program to accept a number 'n' from the user and print the sum of all even numbers from 2 to number 'n'.


#include "iostream.h"
#include "conio.h"
class demo
{
 public:
 int a,b,i;
 void sum ();
};
void demo ::sum()
{
 cout<<"\n\nEnter a number  : ";
 cin >> a;
 for(i=2,b=0;i<=a;i++)
 {
        if(i%2==0)
          {
          b= b+i;
          cout<
            if(i              {
               cout<<"+";
               }
            }
 }
cout<<"="<

 }
 void main()
 {
  clrscr();
  demo d;
  cout <<"Welcome to find no. & total of even no.s\n\n";
  d.sum();
  getch();
 }



Get the executable ( .exe ) file for this program..
Download
Read rest of entry

Diomand By Asterix Symbols

#include "stdio.h"
#include "conio.h"

void main()
{
  int a,b,c,d,n,m,x;
  clrscr();

  printf("Welcome to astrick Diomand\n\n");
  printf("Enter a number  ");
  scanf("%d",&n);

   m=n-1;
   d=1;x=1;


   for(a=1; a<=n; a++)
   {
     for(b=1; b<=m; b++)
     {
       printf("   ");
     }

     for(c=1; c<=d; c++)
     {
       printf("*  ");
     }

     printf("\n");
     m--;
     d+=2;
   }

   m=(n*2)-3;

   for(a=1; a
   {
     for(b=1; b<=x; b++)
     {
       printf("   ");
     }

     for(c=1; c<=m; c++)
     {
       printf("*  ");
     }

     printf("\n");
     x++;
     m=m-2;
   }

   getch();
 }



Get the executable ( .exe ) file for this program..
Download
Read rest of entry

SUM OF DIGITS



#include "iostream.h"
#include "conio.h"
class digit
{
    private:
           long int n,N,q,r;
    public:
        digit ()
        {
            N = 0;
        }
        void accept ()
        {
            cout << "Enter a number: ";
            cin  >> n;
        }
        void cal ();
};
void digit :: cal ()
{
    cout << "The sum of individual digits for " << n << " is: ";
    for (; n>0; )
    {
        q  = n/10;
        r  = n%10;
        N += r;
        n  = q;
    }
    cout << N;
}
void main ()
{
    clrscr ();
    digit D;
    cout<<"Welcome to find sum of the digits\n\n";
    D.accept ();
    D.cal ();
getch ();
}



Get the executable ( .exe ) file for this program..
Download
Read rest of entry

STUDENT GRADE

#include "iostream.h"
#include "conio.h"
class grade
{
    private:
        int r,m[3];
        float p;
    public:
        void accept ()
        {
            cout << "Enter student details\n\n";
            cout << "Enter the Roll Number: ";
            cin  >> r;
            cout << "Enter marks of subject 1: ";
            cin  >> m[0];
            cout << "Enter marks of subject 2: ";
            cin  >> m[1];
            cout << "Enter marks of subject 3: ";
            cin  >> m[2];
        }
        void cal ();
};
void grade :: cal ()
{
    clrscr ();
    cout << "Roll Number:\t" << r;
    p = (m[0] + m[1] + m[2]) / 3;
    cout << endl << "Percentage:\t" << p << "%" << endl;
    if (p < 40.00)
        cout <<    "Grade:\t\tF";
    else if (p >= 40.00 && p < 60.00)
        cout << "Grade:\t\tB";
    else if (p >= 60.00 && p < 75.00)
        cout << "Grade:\t\tA";
    else if (p >= 75.00 && p < 100.00)
        cout << "Grade:\t\tDistinction";
    else
        cout << "Invalid Entries";
}
void main ()
{
    clrscr ();
    grade G;
    G.accept ();
    G.cal ();
getch ();
}


Get the executable ( .exe ) file for this program..
Download






Read rest of entry

SQUARE OF A Number


#include "conio.h"

#include "iostreame.h"
class demo
{
 public :
 int a,b,c;
 void sum();
 };
void demo :: sum()
 {
 b=0;
 clrscr();
 cout<<"Enter the Number : ";
 cin>>a;
 cout<<"\nThe Number You Entered is : "<
 c=a*a;
 cout<<"\n\nThe Square Of the Number is : "<
 }
void main()
 {
 demo obj;
 obj.sum();
 getch();
 }



Get the executable ( .exe ) file for this program..
Download
Read rest of entry

PRIME OR NOT



#include "iostream.h"
#include "conio.h"
class prime
{
    private:
        int n;
    public:
        void accept ()
        {
            cout << "Enter a number: ";
            cin  >> n;
        }
        void cal ();
};
void prime :: cal ()
{
    if (n != 0 && n != 1)
    {
        for (int i=2; i
        {
            if (n%i == 0)
            {
                cout << "The number " << n << " is not a Prime number";
                break;
            }
        }
        if (i == n)
            cout << "The number " << n << " is a Prime number";
    }
    else
        cout << "The number " << n << " is neither Prime nor Composite";
}
void main ()
{
    clrscr ();
    prime P;
    cout<<"Welcome to find Prime no. or not\n\n";
    P.accept ();
    P.cal ();
getch ();
}



Get the executable ( .exe ) file for this program..
Download
Read rest of entry

PALINDROME



#include "iostream.h"
#include "conio.h"
class demo
{
    private:
        int n,N,q,r,R;
    public:
        void cal ();
 };
void demo :: cal ()
{
    cout << "Enter the Number: ";
    cin >> n;
    if(n<=0)
    cout << "The is invalid\n";
    else
    {
    N = n;
    for (R = 0; n > 0;)
    {
        q = n/10;
        r = n%10;
        R = (R*10)+r;
        n = q;
    }
    if (R == N)
        cout << "The number is a Palindrome Number!";
    else
        cout << "The number is not a Palindrome Number!";
    }
 }
void main ()
{
    demo d;
    clrscr ();
    cout<< "Welcome to Palindrome no.\n\n";
    d.cal ();
getch ();
}



Get the executable ( .exe ) file for this program..
Download
Read rest of entry

OPERATOR



#include "iostream.h"
#include "conio.h"
int g=1000;
class f
{
    public:
    void c();
    private:
    int g;
};
void f::c()
{
    g=500;
    cout<<"\n the value of g by outside  "<
    {
        g=100;

        cout<<"\n the value of g by inside  "<
        cout<<"\n the value of g by inside using :: operator  "<<::g;
    }
    cout<<"\n the value of g by outside using :: operator  "<<::g;
}
void main()
{
    f k;
    clrscr();
    cout<<"Working of Operator\n\n";
    k.c();
    getch();
}



Get the executable ( .exe ) file for this program..
Download
Read rest of entry

MULIPLE TABLE

#include "iostream.h"
#include "conio.h"
class mult
{
    private:
        int n,x;
    public:
        void accept ()
        {
            cout << "Welcome to Multiple Table\n\n";
            cout << "Enter a number: ";
            cin  >> n;
        }
        void cal ();
};
void mult :: cal ()
{
    for (int i=1; i<11; i++)
    {
        x = n * i;
        cout << endl << n << " x " << i << " = " << x << endl;
    }
}
void main ()
{
    clrscr ();
    mult M;
    M.accept ();
    M.cal ();
getch ();
}



Get the executable ( .exe ) file for this program..
Download
Read rest of entry
 

My Blog List

Term of Use