Wednesday, 8 June 2016

Design a web page using PHP to insert student information in a database and display all the student details in a page. At the time of insertion student id will be generated automatically and display in same form.

Dbconn.php

<?php
session_start();
ob_start();
error_reporting(0);
$serverName = "localhost";
$dbUserName = "root";
$dbPassword ="";
$dbName = "student";
mysql_connect($serverName,$dbUserName,$dbPassword)
        or die(mysql_error());
mysql_select_db($dbName);

Index.php

<?php
require('dbconn.php');
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="" method="post">
        <h1 align="center"><u>Student Database Table</u></h1>
        <table align="center">
            <tr>
                <td>Name</td>
                <td> <input type="text" name="t1"/> </td>
            </tr>
            <tr>
                <td>Roll No</td>
                <td> <input type="text" name="t2"/> </td>
            </tr>
            <tr>
                <td>Marks</td>
                <td> <input type="text" name="t3"/> </td>
            </tr>
            <tr>
                <td></td>
                <td> <input type="submit" value="Submit" name="btn"/> </td>
            </tr>
        </table>
        <?php
        if(isset($_POST['btn'])) 
    {
    $name = $_POST['t1'];
    $roll = $_POST['t2'];
    $marks = $_POST['t3'];
    
    $sql = "INSERT INTO student VALUES('','$name','$roll','$marks')";
    mysql_query($sql) or die(mysql_error());
    }
        ?>
        </form>
        
        <br/><br/><h3><a href="data.php">Show Student Database</a></h3>
    </body>
</html>

Data.php

<?php
    require('dbconn.php');
    ob_start();
?>
<html>
    <head>
        <title>Student Database</title>
        <style>
            td{
                padding: 10px;
            }
            th{
                padding: 10px;
            }
        </style>
    </head>
    <body>
        <h3 align="right"><a href="index.php">Go to Home Page</a></h3>
        <h1 align="center"><u>Student Database</u></h1>
        <table align="center" border="1">
            <th>Id</th>
            <th>Name</th>
            <th>Roll</th>
            <th>Marks</th>
        
            <?php

                $sql = "select * from student";
                $result=mysql_query($sql); 
                while($rws = mysql_fetch_assoc($result)){ ?>
            <tr><td><?php echo $rws['Id']."\n"; ?> </td>
                <td><?php echo $rws['Name']."\n"; ?> </td>
                <td><?php echo $rws['Roll']."\n"; ?> </td>
                <td><?php echo $rws['Marks']."\n"; ?> </td>
                <?php } ?>
        </table>
    </body>
</html>

Write a PHP code which takes input from user and perform the sorting (bubble sorting) the searching (linear searching) techniques depending upon the user choice.

Index.php

<html>
    <head>
        <title>Assignment - 17</title>
    </head>
    
    <frameset cols="20%,80%">
        <frame name="left" src="left.php"/>
         <frame name="right" src=""/>   
    </frameset>
    
</html>

Left.php

<html>
    <head>
        <title>Demo</title>
        <base href="" target="right">
    </head>
    <body>
        <a href="bubble.php" >Bubble Sort</a><br/>
        <a href="linear.php" >Linear Search</a>
    </body>   
</html>

Bubble.php

<html>
    <head>
        <title>Bubble sort</title>
    </head>
    <body>
        <form action="" method="post">
            Enter numbers : <input type="text" name="num"/><br/>
            <input type="submit"/>
            <br/>
            <?php

if($_POST)
{
//get the post value from form
$numbers = $_POST['num'];
//separate the numbers and make into array
$arr = explode(',', $numbers);
function bubbleSort(array $arr)
{
    $n = sizeof($arr);
    for ($i = 1; $i < $n; $i++) {
        for ($j = $n - 1; $j >= $i; $j--) {
            if($arr[$j-1] > $arr[$j]) {
                $tmp = $arr[$j - 1];
                $arr[$j - 1] = $arr[$j];
                $arr[$j] = $tmp;
            }
        }
    }
     
    return $arr;
}
$result = bubbleSort($arr);
print_r($result);
        }
?>
        </form>
    </body>
</html>


Linear.php

<html>
    <head>
        <title>Linear Search</title>
    </head>
    <body>
        <form method="post">
            Enter numbers : <input type="text" name="num"/><br/>
            Enter number to search : <input type="text" name="value"/><br/>
            <input type="submit"/>
            <br/>
            <?php
                if($_POST)
                {
//get the post value from form
                    $numbers = $_POST['num'];
//separate the numbers and make into array
                    $arr = explode(',', $numbers);
                    
                    $value = $_POST['value'];
                    
                    function linear_search(&$arr, $value) {
        for ($i = 0; $i < sizeof($arr); $i++) {
            if ($arr[$i] == $value) {
                return $i;
            }
        }
        return -1;
    }
   
    # Print numbers to search
    
    
    # Find the index of 'value'
    $index = linear_search($arr, $value) + 1;
    
    # Print the index where 'value' is located
    echo "\nNumber $value is at index $index\n";
                    
                }
            
            ?>
        </form>
    </body>
</html>

Tuesday, 7 June 2016

Write a C program which will take student info such as name, roll no., department as input and store in a structure student_details. Store information about the students library details which includes roll no. and book_id in a structure library_details. The program will be able to calculate the total number of books issued to the student of a particular department

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

typedef struct student_details
{
char name[20];
int roll;
int dept;
struct student_details *next;
}student;

typedef struct library_details
{
int roll;
int dept;
int book_id;
struct library_details *next;
}library;

void print_info()
{
printf("\n 1. Enter student details \n");
printf("\n 2. Issue book \n");
printf("\n 3. Count book issued \n");
printf("\n 4. Exit \n");
}

int main()
{
int choice=0;
int roll,dept,book_id,bi;
char name[20];
student *sstart=NULL,*stmp,*sn;
library *lstart=NULL,*ltmp,*ln;

print_info();

while(choice != 4)
{
printf("\n Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1 : printf("\n Enter name, roll no., dept no : ");
scanf("%s %d %d",&name[20],&roll,&dept);
stmp = (student *)malloc(sizeof(student));
stmp->name[20]=name[20];
stmp->roll=roll;
stmp->dept=dept;
stmp->next=NULL;

if(sstart==NULL)
sstart=stmp;
else
{
sn=sstart;
while(sn->next != NULL)
sn=sn->next;
sn->next=stmp;
}

break;

case 2 : printf("\n Enter roll, dept and book_id : ");
scanf("%d %d %d",&roll,&dept,&book_id);
ltmp = (library *)malloc(sizeof(library));
ltmp->roll=roll;
ltmp->dept=dept;
ltmp->book_id=book_id;
ltmp->next=NULL;

if(lstart==NULL)
lstart=ltmp;
else
{
ln=lstart;
while(ln->next != NULL)
ln=ln->next;
ln->next=ltmp;
}

break;

case 3 : printf("\n Enter dept no and roll no : ");
scanf("%d %d",&dept,&roll);
bi=0;
ln=lstart;
while(ln!=NULL)
{
if(ln->roll == roll && ln->dept == dept)
{
bi++;
}
ln=ln->next;
}
printf("\n Total issued book to dept id %d and roll no %d : %d", dept,roll,bi);

break;

default : printf("WRONG INPUT !!!");
break;
}
}
return 0;
}

Write a program to perform sorting operation of an integer array such that even elements will be sorted in ascending order followed by odd elements sorted is descending order:

#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],a[10],b[10],m,n,i,j=0,k=0,temp;
printf("\n Enter array size: ");
scanf("%d",&n);
printf("\n Enter the array elements : ");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
if(arr[i]%2==0)
{
a[j]=arr[i];
j++;
}
else
{
b[k]=arr[i];
k++;
}
}
printf("\n Sorted array : ");
for(i=0;i<j;i++)
{
for(m=0;m<j;m++)
{
 if(a[i]<a[m])
 {
temp=a[i];
a[i]=a[m];
a[m]=temp;
 }
}
}
for(i=0;i<j;i++)
printf("%d\t",a[i]);
for(i=0;i<k;i++)
{
for(m=0;m<k;m++)
{
if(b[i]>b[m])
{
temp=b[i];
b[i]=b[m];
b[m]=temp;
}
}
}
for(i=0;i<k;i++)
printf("%d\t",b[i]);
getch();
}

Wednesday, 18 May 2016

Write a C-program to show first n Fibonacci and Non-Fibonacci numbers

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

void main()
{
int a=0,b=1,i,j,k,n;
clrscr();
printf("\n Enter the range: ");
scanf("%d",&n);
printf("\n Fibonacci series: ");
for(i=0;i<n;i++)
{
printf("%d\t",a);
j=a+b;
a=b;
b=j;
}
printf("\n Non-fibonacci series: ");
a=0,b=1;
for(i=0;i<n;)
{
j=a+b;
a=b;
b=j;
for(k=a+1;k<j;k++,i++)
if(i<n)
printf("%d\t",k);
}
getch();
}

Write a C program to print the reverse of a floating point number.

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

int fun(int a)
{
int r, res=0;
while (a != 0)
{
r=a%10;
res=res*10 + r;
a=a/10;
}
return res;
}

int main()
{
float a,r;
int nm,nm1,rev1,rev2;
printf("\n Enter floating point number: ");
scanf("%f",&a);
nm=a;
r=a-nm;
nm1= r * 10000;
rev1=fun(nm);
rev2=fun(nm1);
printf("\n Reversed number = %d.%d",rev2,rev1);
getch();
}

Design a web page using PHP which will print different patterns according to user choice.

<html>
    <head>
        <meta charset="UTF-8">
        <title>Pattern</title>
    </head>
    <body>
        <form action="" method="post">
        <h1><u>PHP project to print patterns</u></h1>
       
        <h3>Please choose a pattern to print</h3>
        1.<image src="1.jpg"/> &nbsp;
        2.<image src="2.jpg"/> &nbsp;
        3.<image src="3.jpg"/> &nbsp;<br/><br/>
        Enter your choice : <input type="text" name="pat"/><br/><br/>
        Enter no of rows : <input type="text" name="r"/><br/><br/>
        <br/><input type="submit" name="btn" value="Print Pattern"/><br/><br/>
        <?php
           
            if(isset($_POST['btn']))
            {
                $n= $_POST['r'];
                if($_POST['pat']==1)
                {
                    for($x=0;$x<=$n;$x++)
                    {
                        for($y=1;$y<=$x;$y++)
                        {
                            echo "*";
                        }
                        echo "<br>";
                    }
                }
                elseif ($_POST['pat']==2)
                    {
                        for($x=$n;$x>=1;--$x)
                    {
                        for($y=1;$y<=$x;++$y)
                        {
                            echo "*";
                        }
                        echo "<br>";
                    }
               
                    }
                    elseif ($_POST['pat']==3) {
                       
                       $temp=$n;
                        for($row=1;$row<=$n;$row++)
                        {
                            for($c=1;$c<$temp;$c++)
                            {
                                echo "&nbsp ";
                            }
                            $temp--;
                            for ($c = 1; $c <= 2 * $row - 1; $c++) {
                                    echo "*";
                            }
                            echo "<br>";
                        }
                    }
                    else
                        echo "<br/>Invalid Input!!!";
            }
           
           
        ?>
        </form>
    </body>
</html>

Write a PHP code to test whether a given year is leap year or not.

<html>
<body>
<h2>PHP Script to find Leap year or not </h2>
<form action="" method="post">
<input type="text" name="year" />
<input type="submit" />
</form>
</body>
</html>

<?php

if( $_POST )
{
//get the year
$year = $_POST[ 'year' ];

//check if entered value is a number
if(!is_numeric($year))
{
echo "Strings not allowed, Input should be a number";
return;
}

//multiple conditions to check the leap year
if( (0 == $year % 4) and (0 != $year % 100) or (0 == $year % 400) )
{
echo "$year is a leap year";
}
else
{
echo "$year is not a leap year";
}

}

?>

Write a program to implement the concept of singleton pattern.

#include<iostream.h>
#include<conio.h>
class GlobalClass
{
    int m_value;
    static GlobalClass *s_instance;
    GlobalClass(int v = 0)
    {
m_value = v;
    }
  public:
    int get_value()
    {
return m_value;
    }
    void set_value(int v)
    {
m_value = v;
    }
    static GlobalClass *instance()
    {
if (!s_instance)
 s_instance = new GlobalClass;
return s_instance;
    }
};

// Allocating and initializing GlobalClass's
// static data member.  The pointer is being
// allocated - not the object inself.

GlobalClass *GlobalClass::s_instance = 0;

void foo(void)
{
  GlobalClass::instance()->set_value(1);
  cout << "foo: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
}

void bar(void)
{
  GlobalClass::instance()->set_value(2);
  cout << "bar: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
}

int main()
{
  clrscr();
  cout << "main: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
  foo();
  bar();
  getch();
  return 0;
}

Using template, write a suitable class declaration and implementation for a generic array class and define a stack class using the array class, where array will support push and pop operations and not the [ ] operator.

#include<iostream.h>
#include<conio.h>
#define MSIZE 5

template <class T>
class Carray{


public:
int size;
T array[MSIZE];
Carray(){
size=0;
}
};
template <class T>
class Stack{
       Carray <T> stack;
       public:
       int push(T item){
   if(stack.size==MSIZE)
   {
   cout<<"Stack OverFLow"<<endl;
   return -1;
   }
   stack.array[stack.size++]=item;
   cout<<item<<" Is pushed into stack"<<endl;
   return 0;
}
       T pop(){
  if(stack.size==0)
  {
  cout<<"Stack UnderFlow"<<endl;
  return -1;
  }
  cout<<stack.array[stack.size-1]<<" Is popped"<<endl;
  return stack.array[--stack.size];
       }

};
void main(){
Stack<int> stack;
int i;
clrscr();
for(i=0;i<6;i++){
   stack.push(i);
}
for(i=0;i<6;i++){
   stack.pop();
}

getch();
}

Write a program to implement following main() function: int main() { string str1(“CSE”),str2(“MCKVIE”); string str3; str3=str1+str2; return 0; }

#include<iostream.h>
#include<conio.h>
#include<string.h>

 class string{
public:
char str[100];
string(char *);
string(){
strcpy(str,"");
}
void operator=(const string);

string operator+(const string& s){
      string s1("");
      strcpy(s1.str,this->str);
      strcat(s1.str,s.str);
      return s1;

}
friend ostream &operator<<(ostream &output,const string s){
output<<s.str<<endl;
return output;
}


};

string::string(char *s){
strcpy(str,s);
}
void  string::operator=(const string s){

strcpy(this->str,s.str);

}
void main()
{

    string str1("CSE"),str2("MCKVIE");
    string str3;
    clrscr();
    str3=str1+str2;
    cout<<str3;
    getch();
}

Write a program to implement following main() function: int main() { string str1(“MCKVIE”),str2(“MCKVIE”); if(str1= =str3) cout<<”A”; else cout<<”B”; return 0; }

#include<iostream.h>
#include<conio.h>
#include<string.h>

 class string{
public:
char str[100];
string(char *);
int operator==(const string& s){
if(! strcmp(this->str,s.str))
return 1;
      else
return 0;

}



};

string::string(char *s){
strcpy(str,s);
}

void main()
{

    string str1("MCKVIE"),str2("MCKVIE");

    clrscr();
    if(str1==str2)
    cout<<"A";
    else
    cout<<"B";
    getch();
}

Write a program to implement following main() function:

int main() { string str2(“MCKVIE”);string str3=str2;cout<<str3; return 0; }



#include<iostream.h>
#include<conio.h>
#include<string.h>

 class string{
public:
char str[100];
string(char *);
string operator=(const string);
friend ostream &operator<<(ostream &output,const string s){
output<<s.str<<endl;
return output;
}
};

string::string(char *s){
strcpy(str,s);
}
string string::operator=(const string s){
string s1("");
strcpy(s1.str,s.str);

return s1;
}

void main()
{
  //  int a=0;
    string str2("MCKVIE");
    string str3=str2;
    clrscr();
    cout<<str3;
    getch();
}

Write a C++ program to create a „MATRIX‟ class of size m×n. Overload the „+‟ operator to add two Matrix objects. Write a main function to implement it.

#include<iostream.h>
#include<conio.h>

class MATRIX{

public:
int m,n;
int val[10][10];
int i,j;
void print();
MATRIX operator+(const MATRIX);

};

MATRIX MATRIX:: operator+(const MATRIX mat1){
int i,j;
MATRIX rmat;
rmat.m=mat1.m;
rmat.n=mat1.n;
for(i=0;i<mat1.m;i++)
for(j=0;j<mat1.n;j++)
rmat.val[i][j]=mat1.val[i][j]+this->val[i][j];

return rmat;
}
void MATRIX::print(){
int i,j;
for(i=0;i<m;i++){
 for(j=0;j<n;j++)
cout<<val[i][j]<<"\t";
cout<<endl;
}
}
void main(){
int m,n;
int i,j;
MATRIX mat1,mat2,rmat;
clrscr();
cout<<"Enter The value of row and col : ";
cin>>m>>n;
mat1.m=m;
mat1.n=n;
mat2.m=m;
mat2.n=n;
cout<<"Enter the value of Matrix 1 : ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>mat1.val[i][j];
cout<<"Enter the value of Matrix 2 : ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>mat2.val[i][j];

rmat=mat1+mat2;
cout<<"\nSum Of Matrix is \n";
rmat.print();
getch();
}

Write a C program which will take student info such as name, roll no., department as input and store in a structure student_details. Store information about the students library details which includes roll no. and book_id in a structure library_details. The program will be able to calculate the total number of books issued to the student of a particular department.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
typedef struct student_detail{
    int roll;
    int department;
    struct student_detail *next;
}student;

typedef struct library_detail {
    int roll;
    int book_id;
    struct library_detail *next;
}library;

void print_info(){

    printf("1 . Enter Student Detail.\n");
    printf("2 . Issue Book.\n");
    printf("3 . Count Book Issued to a department.\n");
    printf("4 . Exit.\n");

}

int main()
{
    int choice=0;
    int roll,dept,book_id,bi;
    student *sstart=NULL,*stmp,*sn;
    library *lstart=NULL,*ltmp,*ln;
  //  int c=0,l=0;
    clrscr();
    print_info();

    while(choice!=4){
printf("\nEnter Your Choice : ");
scanf("%d",&choice);
   /* c=0,l=0;

stmp=sstart;
ltmp=lstart;
while(stmp!=NULL){
   c++;
   stmp=stmp->next;
}
while(ltmp!=NULL){
   l++;
   ltmp=ltmp->next;
}

printf("\n Total Entry of student and libray: %d %d\n",c,l);
    */
switch(choice){
   case 1: printf("Enter Roll and department no : ");
   scanf("%d %d",&roll,&dept);
    stmp=(student *) malloc(sizeof(student));
    stmp->roll=roll;
    stmp->department=dept;
    stmp->next=NULL;

    if(sstart==NULL)
    sstart=stmp;
    else
    {
sn=sstart;
while(sn->next!=NULL)
sn=sn->next;
sn->next=stmp;

    }
   break;
   case 2: printf("Enter Roll and book id : ");
   scanf("%d %d",&roll,&book_id);
    ltmp=(library *) malloc(sizeof(library));
    ltmp->roll=roll;
    ltmp->book_id=book_id;
    ltmp->next=NULL;


   if(lstart==NULL)
    lstart=ltmp;
    else
    {
ln=lstart;
while(ln->next!=NULL)
ln=ln->next;
ln->next=ltmp;

    }
   break;
   case 3:
   printf("Enter Department No: ");
   scanf("%d",&dept);
   bi=0;
   ln=lstart;
   while(ln!=NULL){
      roll=ln->roll;
      sn=sstart;
      while(sn!=NULL){
if(sn->roll==roll && sn->department==dept){
bi++;
break;
}
sn=sn->next;

      }
      ln=ln->next;
   }
   printf("Total Issued Book to dept id %d is %d \n.",dept,bi);
break;
   default: printf("Wrong Input!!\n\n");
    print_info();
     break;
}
}

    return 0;
}



Wednesday, 24 February 2016

C prgram to show that C= A * Transpose(B) where A,B,C are 3x3 matrices

#include <stdio.h>

int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], transpose[10][10], multiply[10][10];

  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");

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

  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);

  if (n != p)
    printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
    printf("Enter the elements of second matrix\n");

    for (c = 0; c < p; c++)
      for (d = 0; d < q; d++)
 scanf("%d", &second[c][d]);

  // transpose of second matrix

  for (c = 0; c < m; c++)
for( d = 0 ; d < n ; d++ )
transpose[d][c] = second[c][d];

  // multiply A * Transpose(B)

    for (c = 0; c < m; c++) {
      for (d = 0; d < q; d++) {
        for (k = 0; k < p; k++) {
sum = sum + first[c][k]*transpose[k][d];
 }

        multiply[c][d] = sum;
        sum = 0;
      }
    }

    printf("Product of entered matrices:-\n");

    for (c = 0; c < m; c++) {
      for (d = 0; d < q; d++)
        printf("%d\t", multiply[c][d]);

      printf("\n");
    }
  }

  return 0;
}

C prgram to find out nPr and nCr

#include <stdio.h>

long factorial(int);
long find_ncr(int, int);
long find_npr(int, 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;
}