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