Wednesday, 18 May 2016

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