Saturday, 28 April 2012

Hello world program

      value to list hello community. This program printing hello community, printf operate is used to display textual content on display, '\n' locations pointer on the beginning of next line. This may be your first c value while learning development.

#include<stdio.h>
void  main()
{
      printf("Hello world\n");
      getch(0;
}

We may store "hello world" in a character array and then print it.

#include<stdio.h>
void main()
{
      char string[ ] = "Hello World";
      printf("%s\n", string);
      getch();
}

output:-


Print integer

              This c system first information an integer and then printing it. Feedback is done using scanf operate and variety is produced on display using printf.


 #include
 void main()
{
    int a;

    printf("Enter an integer\n");
    scanf("%d", &a);

    printf("Integer that you have Enter is %d\n", a);

    getch();
}

output:-

Addition

c program to add two numbers:-

                    C program to add two numbers: This c language program perform the basic arithmetic operation of addition on two numbers and then prints the sum on the screen. For example if the user entered two numbers as 5, 6 then 11 ( 5 + 6 ) will be printed on the screen.

#include<stdio.h>

void main()
{
   int a, b, c;

   printf("Enter two number to add\n");
   scanf("%d%d",&a,&b);

   c = a + b;

   printf("Sum of Enter number = %d\n",c);
   getch();
}

output:-




Addition without using third variable:-
#include<stdio.h>

void main()
{
   int a = 1, b = 2;

   /* Storing result of addition in variable a */

   a = a + b;

   /* Not recommended because original value of a is lost
    * and you may be using it some where in code considering it
    * as it was entered by the user.*/

   printf("Sum of a and b = %d\n", a);
   getch();
}



                                                                                                                                                                 C program to add two numbers repeatedly:-







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

   while(1)
   {
      printf("Enter values of a and b\n");
      scanf("%d%d",&a,&b);

      c = a + b;

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

      printf("Do you wish to add more numbers(y/n)\n");
      scanf(" %c",&ch);

      if ( ch == 'y' || ch == 'Y' )
         continue;
      else
    break;
   }
getch();
}




Adding numbers in c using function:-

We have used lengthy information kind as it can manage thousands.

#include<stdio.h>
#include<conio.h>

long addition(long, long);

void main()
{
   long first, second, sum;

   scanf("%ld%ld", &first, &second);

   sum = addition(first, second);

   printf("%ld\n", sum);

   getch();
}

long addition(long a, lengthy b)
{
   long result;

   return = a + b;

  return result;
}

Addition,subtraction,multiplication and division


                                    C program to perform basic arithmetic operations i.e. addition , subtraction, multiplication and division of two numbers. Numbers are assumed to be integers and will be entered by the user.

C program code:-


#include <stdio.h>
#include<conio.h>

void main()
{
   int   first, second, add, subtract, multiply;
   float divide;

   printf("Enter two integers\n");
   scanf("%d%d", &first, &second);

   add = first + second;
   subtract = first - second;
   multiply = first * second;
   divide = first / (float)second;   //typecasting

   printf("Sum = %d\n",add);
   printf("Difference = %d\n",subtract);
   printf("Multiplication = %d\n",multiply);
   printf("Division = %.2f\n",divide);

   getch();
}

output:-

Check vowel



                  This code checks whether an input alphabet is a vowel or not. Both lower-case and upper-case are checked.

C programming code:-


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

      printf("Enter a character\n");
      scanf("%c",&ch);

      if ( ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
         printf("%c is a vowel.\n", ch);
      else
          printf("%c is not a vowel.\n", ch);

      getch();
}




Check vowel using switch statement:-

#include<stdio.h>
#include<conio.h>

void main()
{
   char ch;

   printf("Enter a character\n");
   scanf("%c",&ch);

   switch(ch)
   {
      case  'a':
      case 'A':
      case 'e':
      case 'E':
      case 'i':
      case 'I':
      case 'o':
      case'O':
      case 'u':
      case 'U':
            printf("%c is a vowel.\n", ch);
            break;
      default:
            printf("%c is not a vowel.\n", ch);
   }                  

   getch();
}


odd or even



#include<stdio.h>

void main()
{
   int n;

   printf("Enter an integer\n");
   scanf("%d",&n);

   if ( n%2 == 0 )
      printf("Even\n");
   else
      printf("Odd\n");

   getch();
}


C program to check odd or even using bitwise operator:-


#include<stdio.h>
#include<conio.h>

void main()
{
   int n;

   printf("Enter an integer\n");
   scanf("%d",&n);

   if ( n & 1 == 1 )
      printf("Odd\n");
   else
      printf("Even\n");

   getch();
}




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

#include<stdio.h>
#include<conio.h>

void main()
{
   int n;

   printf("Enter an integer\n");
   scanf("%d",&n);

   if ( (n/2)*2 == n )
      printf("Even\n");
   else
      printf("Odd\n");

   getch();
}

Check leap year


c program to check leap year: c code to check leap year, year will be entered by the user.

#include<stdio.h>
#include<conio.h>
 
void main()
{
      int year;
 
      printf("Enter a year to check if it is a leap year\n");
      scanf("%d", &year);
 
      if ( year%400 == 0)
         printf("%d is a leap year.\n", year);
      else if ( year%100 == 0)
         printf("%d is not a leap year.\n", year);
      else if ( year%4 == 0 )
         printf("%d is a leap year.\n", year);
      else
         printf("%d is not a leap year.\n", year);   
 
      getch();
}

Add digits

                    C program to add digits of a number: Here we are using modulus operator(%) to extract individual digits of number and adding them.
#include<stdio.h>
 void main()
{
   int n, sum = 0, remainder;
   printf("Enter an integer\n");
   scanf("%d",&n);
    while( n != 0 )
   {
      remainder = n % 10;
      sum = sum + remainder;
      n = n / 10;
   }

   printf("Sum of digits of entered number = %d\n",sum);

   getch();
}
output :-


Add digits using recursion :-

#include <stdio.h>
#include<conio.h>

int add_digits(int);
 
int main() 
{
  int n, result;
  scanf("%d", &n);
  result = add_digits(n);
  printf("%d\n", result);
  getch();
}
 int add_digits(int n) 
{
  static int sum = 0;
   if (n == 0) 
{
    return 0;
  }
  sum = n%10 + add_digits(n/10);
 return sum;
}

Factorial



       Factorial system in c: c value to discover and list factorial of a variety, three techniques are given, first one uses a for cycle, second uses a operate to discover factorial and third using recursion. Factorial is showed using !, so five factorial will be published as 5!, n factorial as n!. Also
n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is determined as one i.e. 0!=1.


#include<stdio.h>
#include<conio.h>

void 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();
 
}

output :-
              Enter a number to it's factorial=6
              Factorial of 6=720


Factorial program in c using function :-

#include<stdio.h>
#include<conio.h>

long factorial(int);

void 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));

   getch();
}

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>
 #inculde<conio.h>

long factorial(int);

void 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));
}

Add n numbers

                         
                                  This c system add n statistics which will be joined by the individual. First of all individual will get into a variety showing how many statistics individual desires to add and then individual will get into n statistics. In our c system to add statistics we are not using an array, if you wish you can use an array.


#include<stdio.h>
#include<conio.h>

void 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 Enter number = %d\n",sum);

   getch();
}

Swaping

          
                          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 number in c :-


#include<stdio.h>
#include<conio.h>
void 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();
}


Swapping of two numbers without third variable:-

#include<stdio.h>
#include<conio.h>

void 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);

   getch();
}

swap two number using pointers :-


#include<stdio.h>
#include<conio.h>
 void 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);
  getch();
}


 swapping number using call by reference:-

#include<stdio.h>
#include<conio.h>
void swap(int*, int*);

void 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);

   getch();
}

void swap(int *a, int *b)
{
   int temp;

   temp = *b;
   *b = *a;
   *a = temp;
}

output :-

Reverse a number

                 C System to opposite a variety :- The offer opposite the variety joined by the individual and then printing the changed variety on the screen. For example if individual get into 123 as feedback then 321 is produced as result. In our program we use modulus(%) owner to obtain the numbers of a variety. To change variety look at it and write it from other or the result of value is a variety acquired by writing unique variety from right to left. To opposite thousands use lengthy information kind or lengthy long information kind if your compiler can handle it, if you still have thousands then use post or other information structure.



#include<stdio.h>
#include<conio.h>

void main()
{
   int n, reverse = 0;

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

   while( n != 0 )
   {
      reverese = reverese * 10;
      reverese = reverese + n%10;
      n = n/10;
   }

   printf("Reverse of enter No. is = %d\n", reverse);

   getch();
}

output:-

Enter the number to reverse
1234
reverse of entered number is=4321

Palindrome numbers

Palindrome number algorithm :-

1. Get the variety from user.
2. Reverse it.
3. Evaluate it with the number entered by the usar.
4. If both are same then list palindrome number
5. Else list not a palindrome number.

Palindrome number program c

#include<stdio.h>
 #include<conio.h>

void main()
{
   int n, reverse = 0, temp;

   printf("Enter a No. to check if it is a palindrome or not\n");
   scanf("%d",&n);

   temp = n;

   while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }

   if ( n == reverse )
      printf("%d is a palindrome number.\n", n);
   else
      printf("%d is not a palindrome number.\n", n);

   getch();
}

Friday, 27 April 2012

Addition using pointer


#include<stdio.h>
#include<conio.h>

void main()
{
   int   first, second, *p, *q, sum;

   printf("Enter two integers to add\n");
   scanf("%d%d", &first, &second);

   p = &first;
   q = &second;

   sum = *p + *q;

   printf("Sum of entered numbers = %d\n",sum);

   return 0;
}

output:-

Enter two numbers:-
10
20
sum of entered number=30

Minimum element in array


#include <stdio.h>

void main()
{
    int array[30], maximum, size, c, location = 1;

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

    printf("Enter %d integers\n", size);

    for ( c = 0 ; c < size; c++ )
        scanf("%d", &array[c]);

   maximum = array[0];

    for ( c = 1 ; c < size ; c++ )
    {
        if ( array[c] > maximum )
        {
           maximum = array[c];
           location = c+1;
        }
    }

    printf("Maximum element is present at place number %d and it's valu is %d.\n", location, maximum);
    getch0;
}

 c programming in code using pointers


#include <stdio.h>
#include<conio.h>

void main()
{
    int array[30], *maximum, size, c, location = 1;

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

    printf("Enter %d  integers\n", size);

    for ( c = 0 ; c < size ; c++ )
        scanf("%d", &array[c]);

    maximum = array;
    *maximum = *array;

    for ( c = 1 ; c < size ; c++ )
    {
        if ( *(array+c) > *maximum )
        {
           *maximum = *(array+c);
           location = c+1;
        }
    }

    printf("Maximum element is existing at location number %d and it's value is %d.\n", location, *maximum);
    getch0;
}

Linear search



                                                             linear search:


#include<stdio.h>

void main()
{
   int array[25], search, c, number;

   printf("Enter the No of element in array\n");
   scanf("%d",&number);

   printf("Enter %d numbers\n", number);

   for ( c = 0 ; c < number ; c++ )
      scanf("%d",&array[c]);

   printf("Enter the No to search\n");
   scanf("%d",&search);

   for ( c = 0 ; c < number ; c++ )
   {
      if ( array[c] == search )     /* if necessary factor found */
      {
         printf("%d is present at place %d.\n", search, c+1);
break;
      }
   }
   if ( c == number )
      printf("%d is not present in range.\n", search);  

   getch0;
}
output:-


                                     linear search for multiple occurrences


#include<stdio.h>

void main()
{
   int  array[30], search, c, n, count = 0;

   printf("Enter the No. of elemant in array\n");
   scanf("%d",&n);

   printf("Enter %d numbers\n", n);

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

   printf("Enter the No. to search\n");
   scanf("%d",&search);

   for ( c = 0 ; c < n ; c++ )
   {
      if ( array[c] == search )  
      {
         printf("%d is present at location %d.\n", seaech, c+1);
count++;
      }
   }
   if ( count == 0 )
      printf("%d is not existing in array.\n", search);
   else
      printf("%d is present %d times in array.\n", search, count);

  getch0;
}

output:-

                                 

                                   C program for  linear search using function:


#include<stdio.h>
#include<conio.h>

int linear_search(int*, int, int);

void main()
{
   int array[30], search, c, n, position;

   printf("Enter the No. of element in array\n");
   scanf("%d",&n);

   printf("Enter %d numbers\n", n);

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

   printf("Enter the No. to search\n");
   scanf("%d",&search);

   position = linear_search(array, n, search);

   if ( position == -1 )
      printf("%d is not existing in array.\n", search);
   else
      printf("%d   is present at place %d.\n", search, position+1);

   getch0;
}

int linear_search(int *pointer, int n, int find)
{
   int c;
            for ( c = 0 ; c < n ; c++ )
   {
      if ( *(pointer+c) == find )
         return c;
   }

   return -1;
 }

Binary search:



#include<stdio.h>

 void main()
{
   int   c, first, last, middle, n, search, array[25];

   printf("Enter number of elements\n");
   scanf("%d",&n);

   printf("Enter %d integers\n", n);

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

   printf("Enter value to find\n");
   scanf("%d",&search);

   first = 0;
   last = n - 1;
   center = (first+last)/2;

   while( first <= last )
   {
      if ( array[middle] < search )
         first = middle + 1;  
      else if ( array[middle] == search )
      {
         printf("%d  found at location %d.\n", search, middle+1);
         break;
      }
      else
         last = middle - 1;

      middle= (first + last)/2;
   }
   if ( first > last )
      printf("Not found! %d  is not existing in the record.\n", search);

   getch0;  
}

Reverse array


                                               $  Reverse array:-

#include<stdio.h>

void main()
{
   int n, c, d, a[50], b[50];

   printf("Enter the number of elements in array\n");
   scanf("%d",&n);

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

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

   for ( c = n - 1, d = 0 ; c >= 0 ; c--, d++ )
      b[d] = a[c];

   for ( c = 0 ; c < n ; c++ )
      a[c] = b[c];

   printf("Reverse array is\n");

   for( c = 0 ; c < n ; c++ )
      printf("%d\n", a[c]);

   getch0;
}

output:-

                  $ Reverse array by swapping(without using additional memory):-



#include <stdio.h>

int main()
{
  int array[100], n, c, t, end;

  scanf("%d", &n);
  end = n - 1;

  for (c = 0; c < n; c++)
{
    scanf("%d", &array[c]);
  }

  for (c = 0; c < n/2; c++)
{
    t          = array[c];
    array[c]   = array[end];
    array[end] = t;

    end--;
  }

  printf("Reversed range components are:\n");

  for (c = 0; c < n; c++)
{
    printf("%d\n", array[c]);
  }

  getch0;
}


                             $ c program to reverse an array using pointers:-


#include<stdio.h>
#include<conio.h>

void reverse_array(int*, int);

main()
{
   int n, c, *pointer;

   scanf("%d",&n);

   suggestion = (int*)malloc(sizeof(int)*n);

   if( suggestion == NULL )
      exit(EXIT_FAILURE);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d",(pointer+c));

   reverse_array(pointer, n);

   printf("Original range on revers is\n");

   for ( c = 0 ; c < n ; c++ )
      printf("%d\n",*(pointer+c));

   free(pointer);

    getch0;
}

void reverse_array(int *pointer, int n)
{
   int *s, c, d;

   s = (int*)malloc(sizeof(int)*n);

   if( s == NULL )
      exit(EXIT_FAILURE);

   for ( c = n - 1, d = 0 ; c >= 0 ; c--, d++ )
      *(s+d) = *(pointer+c);

   for ( c = 0 ; c < n ; c++ )
      *(pointer+c) = *(s+c);

   free(s);
}

                                $  c program to insert an element in an array:-



C development code:-

#include <stdio.h>

main()
{
   int array[100], position, c, n, value;

   printf("Enter variety of elements's in array\n");
   scanf("%d", &n);

   printf("Enter %d elements\n", n);

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

   printf("Enter the place where you wish to insert an element\n");
   scanf("%d", &position);

   printf("Enter the value to insert\n");
   scanf("%d", &value);

   for ( c = n - 1 ; c >= place - 1 ; c-- )
       array[c+1] = array[c];

   array[position-1] = value;

   printf("Resultant range is\n");

   for( c = 0 ; c <= n ; c++ )
        printf("%d\n", array[c]);

   getch0;
}

Delete element in array



#include <stdio.h>

main()
{
   int array[50], position, c, n;

   printf("Enter variety of components in array\n");
   scanf("%d", &n);

   printf("Enter %d elements\n", n);

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

   printf("Enter the place where you wish to delete of element\n");
   scanf("%d", &position);

   if ( place >= n+1 )
      printf("Deletion not possible.\n");
   else
   {
      for ( c = position - 1 ; c < n - 1 ; c++ )
         array[c] = array[c+1];

      printf("Resultant array is\n");

      for( c = 0 ; c < n - 1 ; c++ )
         printf("%d\n", array[c]);
   }

   getch0;
}

Bubble sort



#include<stdio.h>

main()
{
   int array[100], n, c, d, swap;

   printf("Enter number of elements\n");
   scanf("%d", &n);

   printf("Enter %d integers\n", n);

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

   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      for ( d = 0 ; d < n - c - 1 ; d++ )
      {
          if ( array[d] > array[d+1] ) /* For decreasing order  use < */
          {
             swap = array[d];
             array[d] = array[d+1];
             array[d+1] = swap;
          }
      }
   }

   printf("Sorted record in ascending order:\n");

   for ( c = 0 ; c < n ; c++ )
       printf("%d\n", array[c]);

   getch0;
}