Sunday, 11 June 2017

Write a C program for Binary Search ?

C Program for Binary Search

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

 main()
{
   int c, first, last, middle, n, search, array[100];
   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;
   middle = (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 present in the list.\n", search);
   return 0;  
}

Write a C Program for Bubble Sort ?


/* Bubble sort code */

#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 list in ascending order:\n");

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

   return 0;
}

Write a C Program for Finding a Leap Year.

C Program for Finding Leap Year.


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

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

   return 0;
}

Write a Program for compare Strings by using If in C ?

Compare Strings by using If

#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;
}

Write a C program to find ncr and npr value ?

C Program for finding the value for Combinations & Permutations.

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

long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);

main()
{
   int n, r;
   long ncr, npr;

   printf("Enter the value of n and r\n");
   scanf("%d%d",&n,&r);

   ncr = find_ncr(n, r);
   npr = find_npr(n, r);

   printf("%dC%d = %ld\n", n, r, ncr);
   printf("%dP%d = %ld\n", n, r, npr);

   return 0;
}

long find_ncr(int n, int r)
{
   long result;

   result = factorial(n)/(factorial(r)*factorial(n-r));

   return result;
}

long find_npr(int n, int r)
{
   long result;

   result = factorial(n)/factorial(n-r);

   return result;
}

long factorial(int n)
{
   int c;
   long result = 1;

   for( c = 1 ; c <= n ; c++ )
      result = result*c;

   return ( result );
}

Write a Program for compare Strings by using pointers in C ?

Compare Two Strings.

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

How to Print Star in C ?



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

main()
{
    int n, c, k, space;

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

    space = n;

    for ( k = 1 ; k <= n ; k++ )
    {
        for ( c = 1 ; c < space ; c++ )
            printf(" ");

        space--;

        for( c = 1 ; c <= k ; c++ )
            printf("*");

        printf("\n");
    }

    return 0;
}

How to Check Palindrome String in C ?

What is Palindrome?
 
It's a word, phrase, or sequence that reads the same backwards as forwards, e.g. malayalam , madam or nurses run.

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

int is_palindrome(char*);
void copy_string(char*, char*);
void reverse_string(char*);
int string_length(char*);
int compare_string(char*, char*);

main()
{
   char string[100];
   int result;

   printf("Enter a string\n");
   gets(string);

   result = is_palindrome(string);

   if ( result == 1 )
      printf("\"%s\" is a palindrome string.\n", string);
   else
      printf("\"%s\" is not a palindrome string.\n", string);

   return 0;
}

int is_palindrome(char *string)
{
   int check, length;
   char *reverse;

   length = string_length(string);   
   reverse = (char*)malloc(length+1);   

   copy_string(reverse, string);
   reverse_string(reverse);

   check = compare_string(string, reverse);

   free(reverse);

   if ( check == 0 )
      return 1;
   else
      return 0;
}

int string_length(char *string)
{
   int length = 0; 

   while(*string)
   {
      length++;
      string++;
   }

   return length;
}

void copy_string(char *target, char *source)
{
   while(*source)
   {
      *target = *source;
      source++;
      target++;
   }
   *target = '\0';
}

void reverse_string(char *string)
{
   int length, c;
   char *begin, *end, temp;

   length = string_length(string);

   begin = string;
   end = string;

   for ( c = 0 ; c < ( length - 1 ) ; c++ )
       end++;

   for ( c = 0 ; c < length/2 ; c++ )
   {       
      temp = *end;
      *end = *begin;
      *begin = temp;

      begin++;
      end--;
   }
}

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

Friday, 9 June 2017

How to add 3 numbers through ajax.

First of all Make an indexpage.php.





<html>

<head>

<title>How to add three value through Ajax</title>

<link href="<?php echo base_url(); ?>css/bootstrap.min.css" rel="stylesheet">

</head>



<body>

   

    <h2 style="text-align:center;">How to add three value through Ajax</h2>

   

<form action="javascript:add(document.getElementById('frm'));" name="frm" id="frm">

    <div class="col-md-12">

        <div class="col-md-4"></div>

          <div class="col-md-4">

              <table style="border:10px;">

<tr>

<td>Enter Value One</td>

<td><input id="num1" type="text"/></td>

</tr>

<tr>

<td>Enter Value Two</td>

<td><input id="num2" type="text"/></td>

</tr>

<tr>

<td>Enter Value Three</td>

<td><input id="num3" type="text"/></td>

</tr>





<tr>

<td colspan="10">

<input type="submit" name="button" value="Answer">

</td>

</tr>

</table>

</form>

   

<div name="txt" id="txt"></div>

    </div>

<div class="col-md-4"></div>

    </div>

<script type="text/javascript" language="javascript">

function add(obj)

{



var XMLHttpRequestObject=false;

if(window.XMLHttpRequest)

{

XMLHttpRequestObject=new XMLHttpRequest();

}

else if(window.ActiveXObject)

{

XMLHttpRequestObject=new ActiveXObject("Microsoft.XMLHTTP");

}



var str1= "num1=" + document.getElementById("num1").value;

var str2="&num2=" +document.getElementById("num2").value;

var str3="&num3=" +document.getElementById("num3").value;



XMLHttpRequestObject.onreadystatechange = show;

XMLHttpRequestObject.open('POST', '<?php echo base_url();?>jobsite/tes5', true);

XMLHttpRequestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");


XMLHttpRequestObject.send(str1+str2+str3);


function show()

{

if (XMLHttpRequestObject.readyState == 4)

{

if (XMLHttpRequestObject.status == 200)

{

result = XMLHttpRequestObject.responseText;

document.getElementById('txt').innerHTML = result;

}

}

}

}

</script>

</body>

</html>






Now Make another page named it sum.php.


<?php
$num1=$_POST['num1'];
$num2=$_POST['num2'];
$num3=$_POST['num3'];
$num4=$num1+$num2+$num3;
echo $num4;
?>