Tuesday, August 7, 2012

FIFO using Paging and Page Faults in C

Below is a Simple C Language Program Which shows simulation of demand paging & show page faults
  according to FIFO replacement


#include<stdio.h>
#include<conio.h>
int frames[]={-1,-1,-1};
int page_found(int pno)
{
 int i;
 for(i=0;i<3;i++)
 if(frames[i]==pno)
   return i;
 return -1;
}
void main()
{
 int i;
 int page_fault;
 int p_request[]={9,14,10,11,15,9,11,9,15,10,9,15,10,12,5};
 int size=15,page_faults=0;
 clrscr();
 printf("\n Page No.___Page_Frames");
 printf("\n_________________________\n");
 for(i=0;i<size;i++)
 {
  if(page_found(p_request[i]==-1))//next page is not in frame
  {
   frames[page_fault%3]=p_request[i];
   page_faults++;
   printf("%4d___%4d%4d%4d\n",p_request[i],frames[0],frames[1],frames[2]);
  }
  else
  printf("\n %4d",p_request[i]);
 }
 printf("\n_________________________\n");
 printf("\n Total Page Faults=%d",page_faults);
 getch();
}

Shell Program In C

Here is a Simple Shell Program In C Language which counts the number of  chars, words, and Lines Provided in the input.

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

void count();

void main(int argc, char *argv[])
{
    char fname[20];
    char option;

    if( argc != 3 )
    {
        printf("Invalid number of arguments");
        return ;
    }
    option = *argv[1];
    strcpy(fname,argv[2]);
    count(fname, option);
} // main

void count( char fname[], char option)

{
    long ccnt, wcnt,lcnt;
    FILE  *fp;

    long lflag,wflag;
    int ch;

    fp = fopen(fname,"r");
    if( fp == NULL )
    {
        printf("\nUnable to open a file");
        exit(0);
    }
    ccnt = wcnt = lcnt = 0;
    lflag = wflag = 1;
    ch = fgetc(fp);
    while( ch != EOF )
    {
        ccnt++;
        if( ch == ' ' || ch == '\t')
        {
            lflag = 0;
            if( wflag == 0 )
            {
                wcnt++;
                wflag = 1;
            }
        }
        else
        if( ch == 10 )
        {
            ccnt++;
            lcnt++;
            lflag = 1;
            if( wflag == 0 )
            {
                wcnt++;
                wflag = 1;
            }
        }
        else   // normal
        {
            wflag = 0;
            lflag = 0;
        }
        ch = fgetc(fp);
    } // while

    if( wflag == 0 )
        wcnt++;
    if( lflag == 0 )
        lcnt++;
    switch(option)
    {
        case 'c' :printf("\n char  count  = %ld\n",ccnt);
                  break;
        case 'w' :
                    printf("\n word  count  = %ld\n",wcnt);
                    break;
        case 'l' :
                    printf("\n line  count  = %ld\n",lcnt);
    } // switch
} // count

Monday, August 6, 2012

Welcome to our Blog

Welcome To our blog..!

Here You can Find All the things that you require related to programming. Here you can find code related to various languages such as C,C++,Php,Dot net,Java, Javascript and many more.

Also a variety of articles related to Open source Cms  such as WordPress, Magento, Joomla, Os Commerce, and many more.

Sunday, August 5, 2012

GCD and LCM of two numbers

Here is a Simple Program in Php which gives GCD and LCM of two Numbers.

   1) Here is a Simple Html Form to Enter the Two Numbers.

           <html>
<head>
<title>"LCMGCD"</title>
</head>
<body>
<form name="LCMGCD" method="GET" action="lcmgcd.php">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>
<fieldset>
  <ol>
      <li>
          <label>ENTER THE INTEGER NUMBER1:</label>
          <input type="text" name="Num1" value=" ">
      </li>
      <li>
          <label>ENTER THE INTEGER NUMBER2:</label>
          <input type="text" name="Num2" value=" ">
      </li>
      <li>   
        <input type="submit" name="LCMGCD" value="submit"><br>
      </li> 
</ol>
</fieldset>.
</td>
</tr>
</table>
</form>
</body>
</html>


2)  Here is lcmgcd.php for Calculating the two numbers.

        <?php
$n1=$_GET['Num1'];
$n2=$_GET['Num2'];
echo"FIRST NUMBER IS :-> $n1<br>";
echo"SECOND NUMBER IS :-> $n2<br>";
echo"GCD OF 2 NUMBERS IS :-> ".gcd($n1,$n2)."<br>";


function gcd($a,$b)
{
  $a1=$a;
  $b1=$b;
  while($a<>$b)
  {
     if($a>$b)
       $a=$a-$b;
     else
       $b=$b-$a;  
  }
  echo"<br>LCM OF 2 NUMBERS IS :-> ".(($a1*$b1)/$a)."<br>";
  return $a;

}
?>

Program For Perfect Number

Here is a Simple PHP program to check whether the given number is Perfect or Not.

1) Below in an Html Form to Enter the Number.

     <html>
<head>
<title>PERFECT NUMBER</title>
</head>
<body>
<form name="perfect" method="GET" action="perfect.php">
<table width="100%" cellpadding="0" cellspacing="0" >
<tr>
<td>
<fieldset>
<ol>
    <li>
        <label>ENTER THE NUMBER:</label>
        <input type="text" name="Num" value=" ">
    </li>
    <li>
        <input type="submit" name="calculate" value="submit"><br>
    </li>
</ol>
</fieldset>   
</td>
</tr>
</table>
</form>
</body>
</html>


2) Below is the Perfect .php

          <?PHP
$n=$_GET['Num'];
for($i=2,$sum=1;$i<$n;$i++)
{
   if($n%$i==0)
     $sum=(int)$sum+$i;
}
if($sum==$n)
  echo"      ENTERED NO. $n IS PERFECT";
else
  echo"      ENTERED NO. $n IS NOT PERFECT";  
?>

PHP : Palindrome Number Program

Here is a simple PHP program which checks whether the given number is palindrome or not.

1) Here is the form to enter the number.
 
<html>
    <head>
        <title>INTEGER PALINDROME</title>
    </head>
    <body>
        <table width="100%" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    <form name="palindrome" method="GET" action="palindrome.php">
                        <fieldset>
                            <ol>
                                <li>
                                    <label>ENTER THE INTEGER NUMBER:</label>
                                    <input type="text" name="Num" value=" ">
                                </li>
                                <li>
                                    <input type="submit" name="check" value="submit"><br>
                                </li>
                            </ol>
                        </fieldset>    
                    </form>
                </td>
            </tr>
        </table>
    </body>
</html>   

2) Now here is the Palindrome.php which gets the number and checks whether it  is palindrome or not.

<?php
$n = $_GET['Num'];
function intpal($n1)
{
    $sum = 0;
    $nn = $n1;
    while ($nn > 0) {
        $sum = $sum * 10;
        $sum = $sum + $nn % 10;
        if (($nn % 10) <> 0)
            $nn = ceil($nn / 10) - 1;
        else
            $nn = $nn / 10;
    }
    if ($sum == $n1)
        echo"INTEGER $n1 IS Palindrome ";
    else
        echo"INTEGER $n1 IS NOT Palindrome ";
}
if (empty($n)) {
    echo"ENTER VALID INPUT";
    exit;
}
intpal($n);
?>    
             

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...