Thursday, 26 March 2015

Linear Searching in C


Linear search in c programming: The following code implements linear search (Searching algorithm) which is used to find whether a given number is present in an array and if it is present then at what location it occurs. It is also known as sequential search. It is very simple and works as follows: We keep on comparing each element with the element to search until the desired element is found or list ends.

Linear search c program

#include <stdio.h>
#include <conio.h>
 
void main()
{
   int array[100], search, c, n;
   clrscr();
   printf("Enter the number of elements in array\n");
   scanf("%d",&n);
 
   printf("Enter %d integer(s)\n", n);
 
   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);
 
   printf("Enter the number to search\n");
   scanf("%d", &search);
 
   for (c = 0; c < n; c++)
   {
      if (array[c] == search)     /* if required element found */
      {
         printf("%d is present at location %d.\n", search, c+1);
         break;
      }
   }
   if (c == n)
      printf("%d is not present in array.\n", search);
 
   getch() ;
}


Print Pyramid structure in C



 

    *
   ***
  *****
 *******
*********
We have shown five rows above, in the program you will be asked to enter the numbers of rows you want to print in the pyramid of stars.
C programming code
#include <stdio.h>
#include <conio.h>

void main()
{
   int row, c, n, temp;
   clrscr();
   printf("Enter the number of rows in pyramid of stars you wish to see ");
   scanf("%d",&n);

   temp = n;

   for ( row = 1 ; row <= n ; row++ )
   {
      for ( c = 1 ; c < temp ; c++ )
         printf(" ");

      temp--;

      for ( c = 1 ; c <= 2*row - 1 ; c++ )
         printf("*");

      printf("\n");
   }

   getch();
}



Monday, 23 March 2015

Print Floyd's triangle using C language



C program to print Floyd's triangle:- This program prints Floyd's triangle. Number of rows of Floyd's triangle to print is entered by the user. First four rows of Floyd's triangle are as follows :-
1
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.
C programming code
#include <stdio.h>
#include<conio.h>

void main()
{
  int n, i,  c, a = 1;
  clrscr();
  printf("Enter the number of rows of Floyd's triangle to print\n");
  scanf("%d", &n);

  for (i = 1; i <= n; i++)
  {
    for (c = 1; c <= i; c++)
    {
      printf("%d ",a);
      a++;
    }
    printf("\n");
  }

  getch();
}

Monday, 16 March 2015

Print fibonacci series i.e., 0 1 1 2 3 5 8.....



Using the code below you can print as many numbers of terms of series as desired. Numbers of Fibonacci sequence are known as Fibonacci numbers. First few numbers of series are 0, 1, 1, 2, 3, 5, 8 etc, Except first two terms in sequence every other term is the sum of two previous terms, For example 8 = 3 + 5 (addition of 3, 5). This sequence has many applications in mathematics and Computer Science.

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

void main()
{
   int n, a = 0, b = 1, t, c;
   clrscr();
   printf("Enter the number of terms\n");
   scanf("%d",&t);
   printf(“%2d\n %2d\n”,a,b);
   n=3;
   while(n<=t)
   {
      c=a+b;
      printf(“%2d\n”,c);
      a=b;
      b=c;
      n+=1;
    }
   getch();
   }

Output of program: