Thursday 18 September 2014

learn programming in c language

Introduction
C language Developed in 1972 in Bell Laboratories.
Purpose is to develop Unix oprating system.
High level language
Extension is .c

Use of C:
Operating system.
Text Editor
DataBase
Modern program
Language compiler


/* This is comment
Is multiline
 */
//single line comment

Simple Program.
#include<stdio.h> 
Int main()
{
Printf(“hello world \n”);
Return 0;
}


DataTypes
Char : for character
int : for integer number
float : for decimal values
double : float * 2

Variable :Name given to memory location


Int   number   =   10 ;

Int
Number
10
DataType
Variable
value


Functions:
Int function();

int main()
{
int i = function();
printf(“value is: %d \n”i);
}

Int function()
{
Return 0;
}



Operator: Symbol that tell compiler to perform specific mathematical or logical operation.
Arithmetic 


Arithmetic Operators:


Operator
Description
Example
+
Adds two operands
A + B will give 30
-
Subtracts second operand from the first
A - B will give -10
*
Multiply both operands
A * B will give 200
/
Divide numerator by denumerator
B / A will give 2
%
Modulus Operator and remainder of after an integer division
B % A will give 0
++
Increment operator, increases integer value by one
A++ will give 11
--
Decrement operator, decreases integer value by one
A-- will give 9

Increment and decrement operators:


Let a=5 and b=10
a++;  //a becomes 6
a--;  //a becomes 5
++a;  //a becomes 6
--a;  //a becomes 5 

Logical Operator:

Operator
Description
Example
&&
Called Logical AND operator. If both the operands are non-zero, then condition becomes true.
(A && B) is false.
||
Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true.
(A || B) is true.
!
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
!(A && B) is true.

Assignment Operators:


Operator
Example
Same as
=
a=b
a=b
+=
a+=b
a=a+b
-=
a-=b
a=a-b
*=
a*=b
a=a*b
/=
a/=b
a=a/b
%=
a%=b
a=a%b

Relational Operators


Operator
Description
Example
==
Checks if the values of two operands are equal or not, if yes then condition becomes true.
(A == B) is not true.
!=
Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.
(A != B) is true.
> 
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
(A > B) is not true.
< 
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
(A < B) is true.
>=
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
(A >= B) is not true.
<=
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
(A <= B) is true.

Bitwise Operators:


Operators
Meaning of operators
&
Bitwise AND
|
Bitwise OR
^
Bitwise exclusive OR
~
Bitwise complement
<< 
Shift left
>> 
Shift right












Programs : 

Write a program to calculate area of rectangle use #define preprocessor?
#include<stdio.h>
#define length 10
#define width 5

Void  main()
{
Int area;
Area=length * width;
Printf(“value of area is %d”,area);
}



Write a program to swap two numbers?
#include <stdio.h>
int main(){
      float a, b, temp;
      printf("Enter value of a: ");
      scanf("%f",&a);
      printf("Enter value of b: ");
      scanf("%f",&b);
      temp = a;    /* Value of a is stored in variable temp */
      a = b;       /* Value of b is stored in variable a */
      b = temp;    /* Value of temp(which contains initial value of a) is stored in variable b*/
      printf("\nAfter swapping, value of a = %.2f\n", a);
      printf("After swapping, value of b = %.2f", b);
      return 0;

}

Write a program to  Check Whether a Number is Even or Odd?
#include <stdio.h>
int main(){
      int num;
      printf("Enter an integer you want to check: ");
      scanf("%d",&num);
      if((num%2)==0)      /* Checking whether remainder is 0 or not. */
           printf("%d is even.",num);
      else
           printf("%d is odd.",num);
      return 0;
}

Write a program to  Check Whether a Character is Vowel or consonant?

#include <stdio.h>
int main(){
  char c;
  printf("Enter an alphabet: ");
  scanf("%c",&c);
  if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U')
       printf("%c is a vowel.",c);
  else
       printf("%c is a consonant.",c);
  return 0;
}

Check Leap Year?
#include <stdio.h>
int main(){
      int year;
      printf("Enter a year: ");
      scanf("%d",&year);
      if(year%4 == 0)
      {
          if( year%100 == 0) /* Checking for a century year */
          {
              if ( year%400 == 0)
                 printf("%d is a leap year.", year);
              else
                 printf("%d is not a leap year.", year);
          }
          else
             printf("%d is a leap year.", year );
      }
      else
         printf("%d is not a leap year.", year);
      return 0;
}

Calculate Sum of Natural Numbers?
#include <stdio.h>
int main()
{
    int n, count, sum=0;
    printf("Enter an integer: ");
    scanf("%d",&n);
    count=1;
    while(count<=n)       /* while loop terminates if count>n */
    {
        sum+=count;       /* sum=sum+count */
        ++count;
    }
    printf("Sum = %d",sum);
    return 0;
}

Find the Largest Number Among Three Numbers?

#include <stdio.h>
int main(){
      float a, b, c;
      printf("Enter three numbers: ");
      scanf("%f %f %f", &a, &b, &c);
      if(a>=b && a>=c)
         printf("Largest number = %.2f", a);
      if(b>=a && b>=c)
         printf("Largest number = %.2f", b);
      if(c>=a && c>=b)
         printf("Largest number = %.2f", c);
      return 0;
}

Check Character is an alphabet or not?
#include <stdio.h>
int main()
{
    char c;
    printf("Enter a character: ");
    scanf("%c",&c);
    if( (c>='a'&& c<='z') || (c>='A' && c<='Z'))
       printf("%c is an alphabet.",c);
    else
       printf("%c is not an alphabet.",c);
    return 0;
}

Check Whether a Number is Positive or Negative or Zero.

#include <stdio.h>
int main()
{
    float num;
    printf("Enter a number: ");
    scanf("%f",&num);
    if (num<=0)
    {
        if (num==0)
          printf("You entered zero.");
        else
          printf("%.2f is negative.",num);
    }
    else
      printf("%.2f is positive.",num);
    return 0;
}

Generate Multiplication Table?
#include <stdio.h>
int main()
{
    int n, i;
    printf("Enter an integer to find multiplication table: ");
    scanf("%d",&n);
    for(i=1;i<=10;++i)
    {
        printf("%d * %d = %d\n", n, i, n*i);
    }
    return 0;
}

Reverse an Integer?
#include <stdio.h>
int main()
{
  int n, reverse=0, rem;
  printf("Enter an integer: ");
  scanf("%d", &n);
  while(n!=0)
  {
     rem=n%10;
     reverse=reverse*10+rem;
     n/=10;
  }
  printf("Reversed Number = %d",reverse);
  return 0;
}

 Check Palindrome Number?
#include <stdio.h>
int main()
{
  int n, reverse=0, rem,temp;
  printf("Enter an integer: ");
  scanf("%d", &n);
  temp=n;
  while(temp!=0)
  {
     rem=temp%10;
     reverse=reverse*10+rem;
     temp/=10;
  } 
/* Checking if number entered by user and it's reverse number is equal. */ 
  if(reverse==n) 
      printf("%d is a palindrome.",n);
  else
      printf("%d is not a palindrome.",n);
  return 0;
}

 Check Prime Number
#include <stdio.h>
int main()
{
  int n, i, flag=0;
  printf("Enter a positive integer: ");
  scanf("%d",&n);
  for(i=2;i<=n/2;++i)
  {
      if(n%i==0)
      {
          flag=1;
          break;
      }
  }
  if (flag==0)
      printf("%d is a prime number.",n);
  else
      printf("%d is not a prime number.",n);
  return 0;
}

Write to a Sentence to a File?

#include <stdio.h>
#include <stdlib.h>  /* For exit() function */
int main()
{
   char c[1000];
   FILE *fptr;
   fptr=fopen("program.txt","w");
   if(fptr==NULL){
      printf("Error!");
      exit(1);
   }
   printf("Enter a sentence:\n");
   gets(c);
   fprintf(fptr,"%s",c);
   fclose(fptr);
   return 0;
}


Read a String of Text from File

#include <stdio.h>
#include <stdlib.h> /* For exit() function*/
int main()
{
   char c[1000];
   FILE *fptr;
   if ((fptr=fopen("program.txt","r"))==NULL){
       printf("Error! opening file");
       exit(1);         /* Program exits if file pointer returns NULL. */
   }
   fscanf(fptr,"%[^\n]",c);
   printf("Data from file:\n%s",c);
   fclose(fptr);
   return 0;
}


Fibonacci series up to n terms?
#include <stdio.h>
int main()
{
  int count, n, t1=0, t2=1, display=0;
  printf("Enter number of terms: ");
  scanf("%d",&n);
  printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */
  count=2;    /* count=2 because first two terms are already displayed. */
  while (count<n) 
  {
      display=t1+t2;
      t1=t2;
      t2=display;
      ++count;
      printf("%d+",display);
  }
  return 0;
}

 Factorial program in c using recursion?
#include<stdio.h>
 long factorial(int);
 int main()
{
  int n;
  long f;
   printf("Enter an integer to find factorial\n");
  scanf("%d", &n); 
   if (n < 0)
    printf("Negative integers are not allowed.\n");
  else
  {
    f = factorial(n);
    printf("%d! = %ld\n", n, f);
  }
   return 0;
}
 long factorial(int n)
{
  if (n == 0)
    return 1;
  else
    return(n * factorial(n-1));
}

Factorial program in c using for loop

#include <stdio.h>
 int main()
{
  int c, n, fact = 1;
   printf("Enter a number to calculate it's factorial\n");
  scanf("%d", &n);
   for (c = 1; c <= n; c++)
    fact = fact * c;
   printf("Factorial of %d = %d\n", n, fact);
   return 0;
}