Monday, December 16, 2013

Queue (First In First Out)

First In First Out
Queue : : Rear & Fornt
Applications: Job Scheduling
First in First Out
Queue ADT
objects: a finite ordered list with zero or more elements.
methods:
     for all queue Î Queue, item Î element,
             
max_ queue_ size Î positive integer
    
Queue createQ(max_queue_size) ::=
              create an empty queue whose maximum size is
             
max_queue_size
    
Boolean isFullQ(queue, max_queue_size) ::=   
  
           if(number of elements in queue == max_queue_size)
  
           return TRUE
             
else return FALSE
    
Queue Enqueue(queue, item) ::=
              if
(IsFullQ(queue)) queue_full
             else insert item at rear of queue and return queue    
Queue ADT (cont’d)
Boolean isEmptyQ(queue) ::=
            
 if (queue ==CreateQ(max_queue_size))
             
return TRUE
              else return FALSE
     Element
dequeue(queue) ::=
             
if (IsEmptyQ(queue)) return
              else
remove and return the item
at front of queue.

What is Queue ? Examples of Queue ?

  1. Stores a set of elements in a particular order
  2. Stack principle: FIRST  IN  FIRST  OUT
  3. = FIFO
  4. It means: the first element inserted is the first one to be removed
  5. Example
The first one in line is the first one to be served

Real life examples
  1. Waiting in line
  2. Waiting on hold for tech support
Applications related to Computer Science
  1. Threads
  2. Job scheduling (e.g. Round-Robin algorithm for CPU allocation)

Tuesday, July 16, 2013

Number Pattern-01

C programming Pattern-01
Pattern-01 source code in C Programming


#include<stdio.h>
int main()
{
  int i,j,k=1;
  for(i=1;i<=5;i+=2)
  {
    for(j=5;j>=1;j--)
    {
      if(j>i)
        printf(" ");
      else
        printf("%d ",k++);
    }
    printf("\n");
  }
}

Monday, July 8, 2013

Linear Search

Linear Search
Linear Search in C
C Programming "Linear Search"
#include<stdio.h>
int main()
{
 int arr[10],i,value,index;
 printf("Please enter 10 values:\n");
 for(i=0;i<10;i++)
  scanf("%d",&arr[i]);
  
 printf("\nEnter a value to be searched: ");
 scanf("%d",&value);
 
 index = -1;
 for(i=0;i<10;i++)
 {
  if(arr[i]==value)
  {
   index=i;
   break;
  }
 }
 
 if(index>=0)
  printf("Value found in Array at %d location",index);
 else
  printf("Value not found in Array");
 return 0;
}

LiBinary Search
Binary Search in C
C Programming "Binary Search" - See more at: http://www.programming-and-it-research-center.blogspot.com/#sthash.b5niQNhp.dpuf
bBinary Search
Binary Search in C
C Programming "Binary Search" - See more at: http://www.programming-and-it-research-center.blogspot.com/#sthash.b5niQNhp.dpuf

Binary Search

Binary Search
Binary Search in C
C Programming "Binary Search"
Programming and IT Research Center logo
#include<stdio.h>
int main()
{
 int arr[10],i,max,min,mid,val,index;

 printf("Please enter 10 values in ascending order:\n");
 for(i=0;i<10;i++)
  scanf("%d",&arr[i]);
 
 printf("\nEnter a value to be searched: ");
 scanf("%d",&val);
 
 max=9;
 min=0;
 index=-1;
 while(min<=max)
 {
  mid=(max+min)/2;
  if(val==arr[mid])
  {
   index=mid;
   break;
  }
  if(arr[mid]>val)
   max=mid-1;
  else
   min=mid+1;
 }
 
 if(index>=0)
  printf("Value found in Array at %d location",index);
 else
  printf("Value not found in Array");
 return 0;
}

Tuesday, July 2, 2013

Star Pattern in C

Star pattern in C
Print the following pattern in C
logo

#include <stdio.h>
int main()
{
    int i, j, k;
    for(i=1;i<=5;i++)
    {
        for(j=1;j<=6-i;j++)
        {
            printf("*");
        }
        for(k=1;k<i;k++)
        {
            printf("  ");
        }
        for(j=1;j<=6-i;j++)
        {
            printf("*");
        }
        printf("\n");
    }
    for(i=2;i<=5;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("*");
        }
        for(k=1;k<=5-i;k++)
        {
            printf("  ");
        }
        for(j=1;j<=i;j++)
        {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Pattern in C

Print Pattern in C
Print the following Pattern
logo
                                        56789
                                    5678
                                    567
                                    56
                                    5
#include <stdio.h>
int main()
{
    int i, j;
    for(i=9;i>=5;i--)
    {
        for(j=5;j<=i;j++)
        {
            printf("%d",j);
        }
        printf("\n");
    }
    return 0;
}


 


Pattern in C

Print Pattern in C
Print The following pattern
                                    56789
                                    6789
                                    789
                                    89
                                    9
Logo

#include <stdio.h>
int main()
{
  int i,j;
  for(i=5;i<=9;i++)
  {
    for(j=i;j<=9;j++)
    {
      printf("%d",j);
    }
    printf("\n");
  }
  return 0;
}

Tuesday, June 25, 2013

Pattern in C

Pattern: Print the following pattern.
Pattern in C
Write a program to print the following pattern
Logo

Patten


#include<stdio.h>
int main()
{
int a[10][10],i,j,low=0,top=9,n=1;
for(i=0;i<5;i++,low++,top--)
{
for(j=low;j<=top;j++,n++)
a[i][j]=n;
for(j=low+1;j<=top;j++,n++)
a[j][top]=n;
for(j=top-1;j>=low;j--,n++)
a[top][j]=n;
for(j=top-1;j>low;j--,n++)
a[j][low]=n;
}
for(i=0;i<10;i++)
{
printf("\n\n\t");
for(j=0;j<10;j++)
{
printf("%5d",a[i][j]);
}
}
printf("\n\n");
return 0;
}


Friday, June 7, 2013

Project Euler- Problem Number 2 Solution

Project Euler 2: Even Fibonacci numbers
Project Euler- Problem Number 2 Project Euler-Problem Number 2 Solution Project Euler-Problem Number 2 Solution in C  
Even Fibonacci numbers
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.


My first suggestion to solve this problem, read more carefully the problem and think that how should we have to do to solve it. well. At first we have to know what is Fibonacci number or Fibonacci series? What is Fibonacci formula? If we had already known any Fibonacci C program then it would be easy to us. well, Fibonacci sequence is defined as F1 = F2 = 1 .
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
F_n = F_{n-1} + F_{n-2},\!\,
with seed values
F_0 = 0,\; F_1     = 1.

Well Now we know what is Fibonacci Number. Look at our problem. We have to generate all Fibonacci number under 4000000 and sum of all number. We use 4 variables.  A loop which is help us us generate fibonacci number under 4 millions times. Another two variables are one=1 & two=2; 
Now, C program do just a simple job that is, in three (variable name) variable it add the vaule of one & two and total variable added the value of two of it's own memory for every time. Then C program replace the value of one and two variable. One variable contains the values of two variable and two variable contains the values of three variable. Below is the C program source code of this problem:

#include<stdio.h>
#include<conio.h>
int main()
{
    long total=0,one=1,two=2,three=0;
    while(two<=4000000)
    {
        three=one+two;
        if(two%2==0)
        total+=two;
        one=two;
        two=three;
    }
    printf("%ld",total);
    return 0;
}
After completing the loop function the C program will excite it's final result. To solve this problem I use some modulo operator such as while, % ,||,if etc.While loop help us to loop the C program from 1 to 4000000 times.

Project Euler- Problem Number 1 Solution

Project Euler 1: Multiples of 3 and 5
Project Euler- Problem Number 1
Project Euler-Problem Number 1 Solution
Project Euler-Problem Number 1 Solution in C
Multiples of 3 and 5 Problem in Project Euler
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

My first suggestion to solve this problem, read more carefully the problem and think that how should we have to do to solve it. well. Now we need to iterate/count the numbers from 1 to 1000. Now what should we have done? Don't worry.. Now we take a number  and check it if it's divisible by 3 or 5. .If any number is divisible by 3 or 5 then we will remark this number and go to the next step. 
After checking all numbers which is divisible by 3 or 5 with re-mark now  we will sum all numbers one by one which is divisible by 3 or 5.
The we will find out final result. I use C programming language to solve this problem. I think C programming is the best programming. Below is my C programming source code: 

//Find the sum of all the multiples of 3 or 5 below 1000
#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b,i,Sum=0;
    for (i=1;i<1000;i++)
    {
       a=i%3;
       b=i%5;
        if(a==0||b==0)
        {
            Sum+=i;
        }
    }
    printf("\nThe sum of all the multiples of 3 or 5 below 1000: %d\n",Sum);


To solve this problem I use some modulo operator such as for, % ,||,if etc.
For loop help us to count number from 1 to 1000. 
In 2nd step C program check the number if it divisor by 3 (a) or 5 (b).
In 3rd option if the number is divisor by any one of 3 and 5 then the sum variable will add (sum) this number
After completing the for loop condition the C program will excite it's result.

Monday, May 27, 2013

for loop

for loop

 
 A loop construct found in many procedural languages which repeatedly executes some instructions while a condition is true.
In C, the for loop is written in the form; 

for(INITIALISATION;CONDITION;INCRIMENTATON /DECREMENTATION )STATEMENT;
where INITIALISATION is an expression that is evaluated once before the loop, CONDITION is evaluated before each iteration and the loop exits if it is false, AFTER is evaluated after each iteration, and STATEMENT is any statement, including a compound statement within braces "
..", that is executed if CONDITION is true.
Syntex:

int i;
 for (i = 0; i < 10; i++);
        (INITIALISATION;CONDITION;INCRIMENTATON /DECREMENTATION )
 printf("Hello\n");

prints "Hello" 10 times.

Friday, May 17, 2013

if else if

if else if


An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.
Rule of if else if:

  •   An if can have zero or one else's and it must come after any else if's.
  •   An if can have zero to many else if's and they must come before the else.
  •   Once an else if succeeds, none of the remaining else if's or else's will be tested.

Syntax:

[ Note: /*......................*/ is used only for comment ] 

The syntax of an if...else if...else statement in C programming language:

If (expression 1)
{
   /* Executes when the expression 1 is true */
}
else if (expression 2)
{
   /* Executes when the expression 2 is true */
}
else if (expression 3)
{
   /* Executes when the expression 3 is true */
}
else 
{
   /* executes when the none of the above condition is true */
}

What is Prime Number

Prime Number

Prime number is a number which is  divisor by 1 and itself. It is a number that can't divisor except 1 or itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.
On the other hand, A prime number is a whole number greater than 1, whose only two whole-number factors are 1 and itself..
Suppose n is a whole number, and we want to test it to see if it is prime.   First, we take the square root (or the 1/2 power) of n; then we round this number up to the next highest whole number.  Call the result m.  We must find all of the following quotients:
qm = n / m
q(m-1) = n / (m-1)
q(m-2) = n / (m-2)
q(m-3) = n / (m-3)
. . .
q3 = n / 3
q2 = n / 2
The number n is prime if and only if none of the q's, as derived above, are whole numbers.

if and statement

if and statement

The  if   statement controls conditional branching. The body of an if statement is executed if the value of the expression is nonzero. The syntax for the if statement has two forms.

Syntax
Selection-statement:
if ( expression ) statement
if ( expression ) statement else statement

In both forms of the if statement, the expressions, which can have any value except a structure, are evaluated, including all side effects.

In the first form of the syntax, if expression is true (nonzero), statement is executed. If expression is false, statement is ignored. In the second form of syntax, which uses else, the second statement is executed if expression is false. With both forms, control then passes from the if statement to the next statement in the program unless one of the statements contains a break, continue, or goto.

The following are examples of the if statement: 

if ( i > 0 )
    y = x / i;
else
{
    x = i;
    y = f( x );
}