Friday 20 January 2012

joke


Bap: a beta jara bol to kis boys cricket team captan larki? 
Beta: a kase hoga larka team main captan larki?? A nehi ho sakta. 
Bap: abe hain . jara soch ke bata. 
Beta: mujhe to bakwas lagta he. 
Bap: Srilanka ki us team ki captan “Mahila Joyabardhana

joke

Shadi ke bad dusre din Parul apni dady se: Meri unse ladai ho gayie! 
Dady: Shadi mein jhagde toh hote rehte hai fikar mat karo. 
Parul: Woh toh thik hai par ab “LAASH” ka kya karu 

Sunday 8 January 2012

C PROGRAMMING

C PROGRAMMING EXAMPLES


c program to check odd or even

c program to check odd or even: We will determine whether a number is odd or even by using different methods all are provided with a code in c language. As you have study in mathematics that in decimal number system even numbers are divisible by 2 while odd are not so we may use modulus operator(%) which returns remainder, For example 4%3 gives 1 ( remainder when four is divided by three). Even numbers are of the form 2*p and odd are of the form (2*p+1) where p is is an integer.
We can use bitwise AND (&) operator to check odd or even, as an example consider binary of 7 (0111) when we perform 7 & 1 the result will be one and you may observe that the least significant bit of every odd number is 1, so ( odd_number & 1 ) will be one always and also ( even_number & 1 ) is zero.
In c programming language when we divide two integers we get an integer result, For example the result of 7/3 will be 2.So we can take advantage of this and may use it to find whether the number is odd or even. Consider an integer n we can first divide by 2 and then multiply it by 2 if the result is the original number then the number is even otherwise the number is odd. For example 11/2 = 5, 5*2 = 10 ( which is not equal to eleven), now consider 12/2 = 6 and 6 *2 = 12 ( same as original number). These are some logic which may help you in finding if a number is odd or not.

C program to check odd or even using modulus operator


#include<stdio.h>
 
main()
{
   int n;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   if ( n%2 == 0 )
      printf("Even\n");
   else
      printf("Odd\n");
 
   return 0;
}

C program to check odd or even using bitwise operator


#include<stdio.h>
 
main()
{
   int n;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   if ( n & 1 == 1 )
      printf("Odd\n");
   else
      printf("Even\n");
 
   return 0;
}

C program to check odd or even without using bitwise or modulus operator


#include<stdio.h>
 
main()
{
   int n;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   if ( (n/2)*2 == n )
      printf("Even\n");
   else
      printf("Odd\n");
 
   return 0;
}

Find odd or even using conditional operator


#include<stdio.h>
 
main()
{
   int n;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   n%2 == 0 ? printf("Even number\n") : printf("Odd number\n");
 
   return 0;
}

C PROGRAMMING EXAMPLES


Decimal to binary conversion

C program to convert decimal to binary: c language code to convert an integer from decimal number system(base-10) to binary number system(base-2). Size of integer is assumed to be 32 bits. We use bitwise operators to perform the desired task. We right shift the original number by 31, 30, 29, ..., 1, 0 bits using a loop and bitwise AND the number obtained with 1(one), if the result is 1 then that bit is 1 otherwise it is 0(zero).

C programming code


#include <stdio.h>
 
main()
{
   int n, c, k;
 
   printf("Enter an integer in decimal number system\n");
   scanf("%d",&n);
 
   printf("%d in binary number system is:\n", n);
 
   for ( c = 31 ; c >= 0 ; c-- )
   {
      k = n >> c;
 
      if ( k & 1 )
         printf("1");
      else
         printf("0");
   }
 
   printf("\n");
 
   return 0;
}

Above code only prints binary of integer, but we may wish to perform operations on binary so in the code below we are storing the binary in a string. We create a function which returns a pointer to string which is the binary of the number passed as argument to the function.

C code to store decimal to binary conversion in a string


#include <stdio.h>
#include <stdlib.h>
 
char *decimal_to_binary(int);
 
main()
{
   int n, c, k;
   char *pointer;
 
   printf("Enter an integer in decimal number system\n");
   scanf("%d",&n);
 
   pointer = decimal_to_binary(n);
   printf("Binary string of %d is: %s\n", n, t);
 
   free(pointer);
 
   return 0;
}
 
char *decimal_to_binary(int n)
{
   int c, d, count;
   char *pointer;
 
   count = 0;
   pointer = (char*)malloc(32+1);
 
   if ( pointer == NULL )
      exit(EXIT_FAILURE);
 
   for ( c = 31 ; c >= 0 ; c-- )
   {
      d = n >> c;
 
      if ( d & 1 )
         *(pointer+count) = 1 + '0';
      else
         *(pointer+count) = 0 + '0';
 
      count++;
   }
   *(pointer+count) = '\0';
 
   return  pointer;
}

C PROGRAMMING EXAMPLES


c program to add two complex numbers

c program to add two complex numbers :- This program calculate the sum of two complex numbers which will be entered by the user and then printed their sum. User will have to enter the real and imaginary parts of two complex numbers. Then on our program we will add real parts and imaginary parts of complex numbers and then we add them and prints the complex number, i is the symbol used for iota. For example if user entered two complex numbers as
(1 + 2i) and (4 + 6 i) then output of program will be (5+8i).

C programming code


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 
struct complex
{
   int real;
   int img;
};
 
main()
{
   struct complex a, b, c;
 
   printf("Enter a and b where a + ib is the first complex number.");
   printf("\na = ");
   scanf("%d", &a.real);
   printf("b = ");
   scanf("%d", &a.img);
   printf("Enter c and d where c + id is the second complex number.");
   printf("\nc = ");
   scanf("%d", &b.real);
   printf("d = ");
   scanf("%d", &b.img);
 
   c.real = a.real + b.real;
   c.img = a.img + b.img;
 
   if ( c.img >= 0 )
      printf("Sum of two complex numbers = %d + %di",c.real,c.img);
   else
      printf("Sum of two complex numbers = %d %di",c.real,c.img);
 
   getch();
   return 0;
}



output of c program to add two complex numbers

C PROGRAMMING EXAMPLES


Factorial program in c

Factorial program in c: c code to find and print factorial of a number, three methods are given, first one uses a for loop, second uses a function to find factorial and third usingrecursion. Factorial is represented using !, so five factorial will be written as 5!, n factorial as n!. Also
n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0!=1.

Factorial program in c using for loop

: Here we find factorial using for loop.
#include<stdio.h>
#include<conio.h>
 
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);
 
   getch();
   return 0;
}

Factorial
Output of code:

factorial code output

Factorial program in c using function


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

Factorial program in c using recursion


#include<stdio.h>
 
long factorial(int);
 
main()
{
   int num;
   long f;
 
   printf("ENTER A NUMBER TO FIND FACTORIAL :");
   scanf("%d",&num); 
 
   if(num<0)
      printf("NEGATIVE NUMBERS ARE NOT ALLOWED");
   else
   {
      f = factorial(num);
      printf("%d!=%ld",num,f);
   }
   return(0);
}
 
long factorial(int n)
{
   if(n==0)
      return(1);
   else
      return(n*factorial(n-1));
}

C PROGRAMMING EXAMPLES


c program to compare two strings

This c program compares two strings using strcmpwithout strcmp and using pointers. For comparing strings without using library function see another code below.

C program to compare two strings using strcmp


#include<stdio.h>
#include<string.h>
 
main()
{
   char a[100], b[100];
 
   printf("Enter the first string\n");
   gets(a);
 
   printf("Enter the second string\n");
   gets(b);
 
   if( strcmp(a,b) == 0 )
      printf("Entered strings are equal.\n");
   else
      printf("Entered strings are not equal.\n");
 
   return 0;
}

C program to compare two strings without using strcmp

Here we create our own function to compare strings.
int compare(char a[], char b[])
{
   int c = 0;
 
   while( a[c] == b[c] )
   {
      if( a[c] == '\0' || b[c] == '\0' )
         break;
      c++;
   }
   if( a[c] == '\0' && b[c] == '\0' )
      return 0;
   else
      return -1;
}

C program to compare two strings using pointers

In this method we will make our own function to perform string comparison, we will use character pointers in our function to manipulate string.
#include<stdio.h>
 
int compare_string(char*, char*);
 
main()
{
    char first[100], second[100], result;
 
    printf("Enter first string\n");
    gets(first);
 
    printf("Enter second string\n");
    gets(second);
 
    result = compare_string(first, second);
 
    if ( result == 0 )
       printf("Both strings are same.\n");
    else
       printf("Entered strings are not equal.\n");
 
    return 0;
}
 
int compare_string(char *first, char *second)
{
   while(*first==*second)
   {
      if ( *first == '\0' || *second == '\0' )
         break;
 
      first++;
      second++;
   }
   if( *first == '\0' && *second == '\0' )
      return 0;
   else
      return -1;
}

C PROGRAMMING EXAMPLES


c program to swap two numbers

C program to swap two numbers with and without using third variable, swapping in c using pointers and functions (Call by reference) , swapping means interchanging. For example if in your c program you have taken two variable a and b where a = 4 and b = 5, then before swapping a = 4, b = 5 after swapping a = 5, b = 4
In our c program to swap numbers we will use a temp variable to swap two numbers. Swapping is used in sorting that is when we wish to arrange numbers in a particular order either in ascending order or in descending order.

Swaping of two numbers in c


#include<stdio.h>
#include<conio.h>
 
main()
{
   int x, y, temp;
 
   printf("Enter the value of x and y ");
   scanf("%d%d",&x, &y);
 
   printf("Before Swapping\nx = %d\ny = %d\n",x,y);
 
   temp = x;
   x = y;
   y = temp;
 
   printf("After Swapping\nx = %d\ny = %d\n",x,y);
 
   getch();
   return 0;
}

Swapping of two numbers without third variable

You can also swap two numbers without using temp or temporary or third variable. In that case c program will be as shown :-

#include<stdio.h>
 
main()
{
   int a, b;
 
   printf("Enter two numbers to swap ");
   scanf("%d%d",&a,&b);
 
   a = a + b;
   b = a - b;
   a = a - b;
 
   printf("a = %d\nb = %d\n",a,b);
   return 0;
}

To understand above logic simply choose a as 7 and b as 9 and then do what is written in program. You can choose any other combination of numbers as well. Sometimes it's a good way to understand a program.

Swap two numbers using pointers


#include<stdio.h>
 
main()
{
   int x, y, *a, *b, temp;
 
   printf("Enter the value of x and y ");
   scanf("%d%d",&x,&y);
 
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 
   a = &x;
   b = &y;
 
   temp = *b;
   *b = *a;
   *a = temp;
 
   printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
   return 0;
}

Swapping numbers using call by reference

In this method we will make a function to swap numbers.
#include<stdio.h>
 
void swap(int*, int*);
 
main()
{
   int x, y;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d",&x,&y);
 
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 
   swap(&x, &y); 
 
   printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
   return 0;
}
 
void swap(int *a, int *b)
{
   int temp;
 
   temp = *b;
   *b = *a;
   *a = temp;   
}

Output of code:
swap numbers

C PROGRAMMING EXAMPLES


c program to add n numbers

This c program add n numbers which will be entered by the user. Firstly user will enter a number indicating how many numbers user wishes to add and then user will enter n numbers.In our c program to add numbers we are not using an array, if you wish you can use an array.

C programming code


#include<stdio.h>
#include<conio.h>
 
main()
{
   int n, sum = 0, c, var;
 
   printf("Enter the number of integers you want to add\n");
   scanf("%d",&n);
 
   printf("Enter %d numbers\n",n);
 
   for ( c = 1 ; c <= n ; c++ )
   {
      scanf("%d",&var);
      sum = sum + var;
   }
 
   printf("Sum of entered numbers = %d\n",sum);
 
   getch();
   return 0;
}

Add n numbers.

Output:

n numbers