Blog Moved

This website completely moved to new domain. For latest content, visit www.programmingposts.com

Search This Blog

28 Jul 2012

C program for showing printf Format Spcifiers in c

\* C program for showing format specifiers in c *\

PROGRAM:
#include<stdio.h>
main()
{
 printf("\n This is a integer Number1: %d",123);
 printf("\n This is a integer Number2: %06d",25);

 //convert it to 6 digit number
 printf("\n This is a integer Number3: %i",123);
 printf("\n This is a Float value: %f",3.14);
 printf("\n This is a Hexa Decimal value: %X",150);
 printf("\n This is a Octal Value: %o",150);
 printf("\n This is unsigned value: %u",150);
 printf("\n This is a character: %c",'A');
 printf("\n This is a string: %s","Cprogramsblog");
 printf("\n Just print : %%");
 printf("\n Just print \" \" symbol: \" \" ");
}


Output: ( using GNU GCC Compiler with Code Blocks IDE )

This is a integer Number1: 123
This is a integer Number2: 000025
This is a integer Number3: 123 
This is a Float value: 3.140000
This is a Hexa Decimal value: 96
This is a Octal Value:  226
This is unsigned value: 150
This is a character: A
This is a string:  Cprogramsblog
Just print : %
Just print " " symbol: ""       

25 Jul 2012

C program to find the Largest of Three Numbers using Conditional Operator


C program to find the Largest of Three Numbers using Conditional Operator..

PROGRAM:

#include<stdio.h>
int main()
{
  int a,b,c,large;

  printf("\n Enter Three Numbers:");

  scanf("%d %d %d", &a , &b , &c);

  large = (a>b && a>c ? a : b>c?b:c) ;
  /* if a>b and a>c then large=a, else check whether b>c if 
    yes large=b else large=c */
  printf("\n The Largest Number is: %d",large);

  return(0);
}

Sample Output:  ( using GNU GCC Compiler with Code Blocks IDE )

Enter Three Numbers: 5 7 6
The Largest Number is: 7


C program to find Greatest of two Numbers Using Conditional Operator

C program to find Greatest of two Numbers Using Conditional Operator..

PROGRAM:

#include<stdio.h>
main()
{
 int a,b,c;
 printf("\n Enter two Numbers: ");
 scanf("%d %d",&a,&b); /* (a>b) is true then c=a else c=b  */
 c=(a>b)?a:b;
 printf("\n The Greatest number is : %d",c);
 return(0);
}

Sample Output:   ( using GNU GCC Compiler with Code Blocks IDE )
Enter two Numbers: 10 20
The Greatest Number is : 20


24 Jul 2012

C Program to find Number Even or Odd using Bitwise AND ( & ) Operator

c program to understand the usage of Bitwise Operator & .
                   (Or)
Example c program for Bitwise operator & ..

PROGRAM :

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

    printf("\n Enter a Integer Number to find Even or Odd:");
    scanf("%d",&n);
    i=!(n&1);  /* & is a bitwise and operator*/
    if ((i))
        printf("\n The Entered Number Is Even");
    else
       printf("\n The Entered Number is Odd");

    getch();
    return(0);
}


Sample Output: (using GNU GCC Compiler with Code Blocks IDE)
  1) Enter a Integer Number to find Even or Odd: 5
     The Entered Number is Odd
  2) Enter a Integer Number to find Even or Odd:
     The Entered Number Is Even
  


      n       1     (n&1)     !(n&1)
    
n=2  0010   0001    0000      returns 1 ( if statment executes)    
n=3  0011   0001    0001      returns 0 ( else statment executes)

Truth Table:

    p    q     p&q

    0    0      0
    0    1      0
    1    0      0
    1    1      1

c program to find the number even or odd

c program to find whether the given number is even or odd..

PROGRAM:
#include <stdio.h>
void main()
{
    int num;

     printf("Enter a number to find Even or Odd: \n");
     scanf("%d",&num);  /* taking input from user*/

    if (num%2==0) /*checking whether the remainder is zero when divided by 2*/
        printf(" The number %d is Even",num);
    else
        printf(" The number %d is Odd",num);

return(0);
}

Sample Output:  (using GNU GCC Compiler with Code Blocks IDE)
Enter a number to find Even or Odd: 5
The number 5 is Odd




22 Jul 2012

C Program to Show the Operations of Arithmetic Operators


c program to understand the Arithmetic Operators And their Precedences..

PROGRAM: 

#include<stdio.h>
void main()
{

    int a=25,b=5,c=10,d=4;
    int result;
    result=a+b;  /*adding a and b*/
    printf("\n Addition Result: %d",result);
    result=a-b;  /* subtracting b from a*/
    printf("\n Subtraction Result is: %d",result);
    result=a*b; /* multiplying a and b*/
    printf("\n Multiplication result is:%d",result);
    result=a/b; /* dividing a by b to get quotient*/
    printf("\n Division Result is: %d",result);
    result=a%b; /* dividing a by b to get ramainder*/
    printf("\n Division Result Remainder is: %d",result);
    result=a+b*c/d; /*refer precedence 1 explanation below*/
    printf("\n The Result of (a+b*c/d): %d",result);
    result=a+b-c*d; /*refer precedence 2 explanation below*/
    printf("\n The result of (a+b-c*d): %d",result);
    result=++a + b++ /d; /*refer precedence 3 explanation below*/
    printf("\n The Result of (++a + b++ / d): %d",result);
}

Output: (using GNU GCC Compiler with Code Blocks IDE)


Addition Result: 30
Subtraction Result is: 20
Multiplication Result is : 125
Division Result is: 5
Division Result Remainder is: 0
The Result of (a+b*c/d): 37
The Result of (a+b-c*d): -10
The Result of (++a + b++ /d): 27


EXPLANATION:
Every time the variable result is overritten with a new value and printing

Precedence1:  a+b*c/d;
              i)   c*d = 50
             ii) (c*d)/d i.e.,50/4=12.5 
(here float value is not considered because result is declared as int)
            iii) 12 is added to a (25+12=37)

precedence2: a+b-c/d;
             i) c/d is evaluated 
            ii) b-(c/d) evaluated
           iii) a+(b-(c/d)) evaluated

precedence3:  ++a+b++/d;
              i)a value incremented
             ii)b value incremented
            iii)b++/d evaluated
             iv)++a + (b++/d) is evaluated

Link: for more about precedence and order of evaluation

C Program for pre decrement and post decrement

c program for understanding the concept of pre decrement and post decrement

#include<stdio.h>
main()
{
 int i=10,j;
 j=i--;          /* j value 10 , i value 11*/
 /*post decrement*/
 printf("\n After post-decrement: j value: %d , i value:%d \n",j,i);
 j=--i;         /* j value 12, i value 12*/
  /*pre-decrement*/
 printf("\n After pre-decrement: j value: %d, i value:%d \n",j,i);

 return(0);
}

Output( using GNU GCC Compiler with Code Blocks IDE )
 After post-decrement: j value: 10 , i value: 9
 After pre-decrement: j value: 8, i value: 8

21 Jul 2012

c program for pre-increment and post-increment

c program for understanding the concept of pre increment and post increment

PROGRAM:
#include<stdio.h>
main()
{
 int i=10,j;
 j=i++; /* j value 10 , i value 11*/
 /*post increment*/
 printf("\n After post-increment: j value:  %d , i value: %d \n",j,i);
 j=++i; /* j value 12, i value 12*/
 /*pre-increment*/
 printf("\n After pre-increment: j value: %d, i value : %d \n",j,i);

 return(0);
}

Output: ( using GNU GCC Compiler with Code Blocks IDE )
After post-increment: j value: 10 , i value:11
After pre-increment: j value: 12, i value:12

C Program for Division

c program for explaining the division operation, for getting quotient and remainder..


PROGRAM:


#include<stdio.h>
#include<conio.h>
int main()
{
     int x,y;
     int result1,result2;
     printf("\n Enter the dividend : ");
     scanf("%d",&x);
     printf("\n Enter the divisor: ");
     scanf("%d",&y);
     result1 = x/y;          /*it gives the quotient*/
     result2 = x%y;        /* it gives remainder*/
     printf("\n The Quotient is: %d ",result1);
     printf("\n The Remainder is: %d ",result2);
     getch();
     return 0;
}

Sample Output: ( using GNU GCC Compiler with Code Blocks IDE )
  
Enter the dividend: 26
Enter the divisor: 5
The Quotient is: 5
The Remainder is: 1

c program to get the product of two numbers

c program to calculate the product of two numbers:

PROGRAM:
#include<stdio.h>
int main()
{
     int x,y;
     int result;
     printf("\n Enter the first number : ");
     scanf("%d",&x);  /* reading 1st number from user*/
     printf("\n Enter the second number: ");
     scanf("%d",&y);  /* reading 2nd number from user*/
     result = x * y;    /* storing the product in result*/
     printf("\n The product of two numbers is: %d \n",result);
     return 0;
}

Sample Output:  ( using GNU GCC Compiler with Code Blocks IDE )

Enter the first number  : 5
Enter the second number: 5
The product of two numbers is: 25

For C# program :  C# PROGARM TO GET PRODUCT OF TWO NUMBERS

C Program to add two numbers / Integers using Pointers

C Program to add two numbers / Integers using Pointers :

PROGRAM: 


#include<stdio.h>
#include<conio.h>
main()
{

   int num1,num2, *p, *q, sum;
   /* *p,*q are integer pointer variables*/
   printf("\n Enter two integers to add: ");
   scanf("%d %d", &num1, &num2);

   p = &num1;   /* &num1 gives the value at address of num1*/
   q = &num2;

   sum = *p + *q;  /* adding the values of two pointer variables*/

   printf("\n Sum of entered numbers is: %d",sum);  /*printing sum*/
   getch();

   return 0;
}

Sample Output: ( using GNU GCC Compiler with Code Blocks IDE )

Enter two integers to add: 15 10
Sum of the entered numbers is: 25



C Program to Add Two Numbers Using Function



Write a c program to add two numbers / integers using function..

Note: The same program can be used for adding floating numbers, just we have
           to change the function return type,and all variables to float type and
           for large numbers declare as long..

PROGRAM:

#include<stdio.h>
#include<conio.h>
 int sum(int,int);   /*declaring prototype of function*/
 void main()
 {
  int a,b,c;
  printf("\n Enter the two numbers : ");
  scanf("%d  %d",&a,&b);    /* taking two numbers as input*/
  c = sum(a,b);      /* calling function,
                       *the value returned by the function is stored in c */
  printf("\n The sum of two numbers is : %d ",c);
  getch();
 }

 int sum ( int num1,int num2)
 {
  int result;  /* defining variable, its scope lies within function */
  result = num1 + num2 ;   /*adding two numbers*/
  return (result) ;     /* returning result */
 }


Sample Output: ( using GNU GCC Compiler with Code Blocks IDE )

 Enter the two numbers: 10 15
 The sum of two numbers is: 25

For C# program :

C# PROGRAM TO ADD TWO INTEGERS USING FUNCTION


20 Jul 2012

c program to add two numbers without using third variable

Write a c program to add two Numbers without using Third variable..

PROGRAM:

#include<stdio.h>
int main()
{
     int x,y;
     printf(" ***PROGRAM TO ADD TWO NUMBERS WITHOUT USING
                THIRD VARIABLE*** ");
     printf("\n Enter the first number to be added: ");
     scanf("%d",&x);         /* taking x value from keyboard*/
     printf("\n Enter the second number to be added: ");
     scanf("%d",&y);         /* taking y value from keyboard*/
     x = x + y;     /*assining the value of sum of x and y to x*/

     printf("\n The sum of two numbers is: %d\n",x);    /*printing the sum.*/
     return 0;
}

Sample Output: ( using GNU GCC Compiler with Code Blocks IDE )
  Enter the first number to be added: 15
  Enter the second number to be added: 5
  The sum of two numbers is: 20


For c# Program:

C# Program To Add Two Numbers/Integers without using Third Variable

c program for adding two numbers / integers

Simple c program for adding two numbers and its explanation:

PROGRAM:
#include<stdio.h>
int main()
{
     int x;
     int y;
     int result;
     printf("\n Enter the first number to be added: ");
     scanf("%d",&x);
     printf("\n Enter the second number to be added: ");
     scanf("%d",&y);
     result = x + y;
     printf("\n The sum of two numbers is: %d\n",result);
     return 0;
}

Sample Output:
( using GNU GCC Compiler with Code Blocks IDE )

   Enter the first number to be added: 10
   Enter the second number to be added: 15
   The sum of two numbers is: 25
 --------------------------------------------------------
EXPLANATION for the above program :    

 1) The #include <stdio.h> includes the file stdio.h into your code so it knows what the

      functions such as  printf() mean.

 2) main() is the main function in which you write most of your code .  int is the default 

      return type of main function.

 3) int x; , int y; , and int result. the int stands for integer or a number
     (no decimal points you need a float for that) the X and Y are the numbers you are 
      adding together and result is the result of the numbers.

 4) printf("\nEnter the first number to be added: ");
     scanf("%d",&x); the Printf() prints the text, Enter the first number to be added: , 

     onto the screen and the         
     scanf("%d",&x); looks for the number input from the keyboard for x.

 5) printf("\n Enter the second number to be added: ");
      scanf("%d",&y); do the same as the last 2 lines except it looks for the 
       value for y instead of x.

 6) result = x + y; that adds the values of X and Y and stores the result in the 

      variable result.

 7) printf(" The sum of two numbers: %d\n",result);  that prints the result on the screen  

      return(0); tells the OS that the program ended  successfully.


For c# program : 

C# PROGRAM TO ADD TWO NUMBERS / INTEGERS 


Simple C Program


#include<stdio.h>     
main()           
{
   Printf(“ C PROGRAMS BLOG ”);   
}

 output: C PROGRAMS BLOG
--------------------------------------------------------------------------------------

19 Jul 2012

Basic Structure of C program

  • Documentation Block
  • Preprocessor Directive Block 
  • Definition Section 
  • Global Declaration Section
  •  main()       
             {               
                     Variable Declaration Block     
                      Executable Instruction Block
             }
  • Function Block
                   Function 1 module
                   Function 2 module
                   .
                   .
                   .
                  Function x module

---------------------------------------------------------------------------


Why we learn C Language ?

There are many Languages in world today. some of the popular languages are : C++, Java, Ada, BASIC, PASCAL, COBOL, FORTRAN, SMALLTALK, VISUAL BASIC,.etc..
These all are high level languages, where as C is a middle level Language..

There are many reasons to learn C :
>> C is a neither a high level nor low level, it is a middle level language..
>> It is closer to hardware and number of the common programming languages
      (ex: C++,Java,etc..) are based on C  and  Operating Systems(ex: Linux) is 
      developed using C.
>> C is a small language. because C has only 32 keywords and 20 of them are in
      common use. Hence learning C is more easy when compared to any other 
      high level language.
>> Often it is said that C is the second best Programming Language for any
      programming task.
>> C is a very old language that is since 1983, and hence number of the applications
       are  available in C.

13 Jul 2012

HISTORY OF C LANGUAGE

                                                                                                      
                                                                                                   
        DENNIS RITCHIE
_______________________________________________________                
'C' Language was written  by  'Dennis Ritchie' in early 1970's at AT & T  BELL Laboratories, USA.
The basic idea behind the development of 'C' is to develop UNIX  Operating System. In 1970's it is used only in BELL LABS and later the programmers began to use 'C' for writing their programs and hence in 1983, the AMERICAN NATIONAL STANDARDS INSTITUTE (ANSI) established a standard for 'C' for its global acceptance. Then 'C' bagan to replace all the existing languages of that time like ALGOL,PL/I, etc,..
            The whole Story started with the 'Common Programming Language (CPL)' . 'Martin Richards' at the university of Cambridge turned CPL into 'Basic Programming Language(BCPL)'. Later 'Ken Thompson' at BELL LABS wrote a language followed by BCPL and called it as 'B'. and 'C' is a successor of B and hence it is named as 'C'.