Monday, 22 June 2015

nmc lab set



SET 1



Q. Write a C program to solve algebraic equation x4+2x3-x-1=0between 0 and 1 by using Method of Bisection.


Solution:
#include<stdio.h>

#include<math.h>
float f(float);
main()
{
float x,a,b,err=0.00001;
printf("\nEnter values of a and b=");
scanf("%f%f",&a,&b);
if(f(a)*f(b)>0)
printf("\nRoot does not lies between %f and %f",a,b);
else
{
while(fabs(b-a)>=err)
{
x=(a+b)/2;
if(f(x)==0)
{
break;
}
else if(f(a)*f(x)<0)
b=x;
else
a=x;
}
printf("\nRoot of the equation=%f",x);
}
}

float f(float x)
{
return(x*x*x*x+2*x*x*x-x-1);
}

OUTPUT

Enter values of a and b=0 1
Root of the equation=0.866768


SET 2



Q. Write a C program to solve algebraic equation x3-5x-7=0between 2 and 3 by using Method of False position.

Solution:
#include<stdio.h>
#include<math.h>
float f(float);


main()

{

float x0,x1,x2,err=0.00001;

printf("\nEnter the value of x0: ");

scanf("%f",&x0);

printf("\nEnter the value of x1: ");

scanf("%f",&x1);

if(f(x0)*f(x1)>0)

printf("\nRoot does not lies between %f and %f",x0,x1);

else

{

do

{

x2=x0-((f(x0)*(x1-x0))/(f(x1)-f(x0)));


if(f(x0)*f(x2)<0)

{

x1=x2;

}

else

{

x0 = x2;

}

}while(fabs(f(x2))>err);

printf("\n\nApp.root = %f",x2);

}

}


float f(float x)

{

return(x*x*x-5*x-7);

}

OUTPUT:
Enter the value of x0: 2

Enter the value of x1:
3

App.root = 2.747346

SET 3



Q. Write a C program to solve algebraic equation 3x3-9x2+8=0by using Newton Raphson Method.

Solution:
#include<stdio.h>

#include<math.h>

float f(float);

float df(float);

main()

{

float a,x,h,err=0.00001;

printf("\nEnter initial value=");

scanf("%f",&a);

x=a;

do

{

h=-(f(x)/df(x));

a=x;

x=x+h;

}while(fabs(x-a)>=err);

printf("\nThe approximate value is %f",x);

}



float f(float x)

{

return(3*x*x*x-9*x*x+8);

}



float df(float x)

{

return(9*x*x-18*x);

}

OUTPUT:
Enter initial value=1


The approximate value is 1.226074
SET 4



Q. Write a C program to solve algebraic equation x3-4x-9=0between 2 and 3 by using Method of Bisection.

Solution:
#include<stdio.h>

#include<math.h>



float f(float);

main()

{

float x,a,b,err=0.00001;

printf("\nEnter values of a and b=");

scanf("%f%f",&a,&b);

if(f(a)*f(b)>0)

printf("\nRoot does not lies between %f and %f",a,b);

else

{

while(fabs(b-a)>=err)

{

x=(a+b)/2;

printf("\na=%f, f(a)=%f, b=%f, f(b)=%f, x=%f, f(x)=%f",a,f(a),b,f(b),x,f(x));

if(f(x)==0)

{

break;

}

else if(f(a)*f(x)<0)

b=x;

else

a=x;

}

printf("\nRoot of the equation=%f",x);

}


}



float f(float x)

{

return(x*x*x-4*x-9);

}

OUTPUT

Enter values of a and b=2 3


Root of the equation=2.706535


SET 5



Q. Write a C program to solve algebraic equation x3-2x-5=0 between 1 and 3 by using Method of False position.

Solution:
#include<stdio.h>

#include<math.h>


float f(float);


main()

{

float x0,x1,x2,err=0.00001;

printf("\nEnter the value of x0: ");

scanf("%f",&x0);

printf("\nEnter the value of x1: ");

scanf("%f",&x1);

if(f(x0)*f(x1)>0)

printf("\nRoot does not lies between %f and %f",x0,x1);

else

{

do

{

x2=x0-((f(x0)*(x1-x0))/(f(x1)-f(x0)));

if(f(x0)*f(x2)<0)

{

x1=x2;

}

else

{

x0 = x2;

}

}while(fabs(f(x2))>err);

printf("\n\nApp.root = %f",x2);

}

}


float f(float x)

{

return(x*x*x-2*x-5);

}

OUTPUT:
Enter the value of x0: 1


Enter the value of x1: 3


App.root = 2.094551
SET 6



Q. Write a C program to solve algebraic equation x3+3x-1=0by using Newton Raphson Method.

Solution:

#include<stdio.h>

#include<math.h>

float f(float);

float df(float);

main()

{

float a,x,h,err=0.00001;

printf("\nEnter initial value=");

scanf("%f",&a);

x=a;

do

{

h=-(f(x)/df(x));

a=x;

x=x+h;

}while(fabs(x-a)>=err);

printf("\nThe approximate value is %f",x);

}



float f(float x)

{

return(x*x*x+3*x-1);

}



float df(float x)

{

return(3*x*x+3);

}
OUTPUT:
Enter initial value=1


The approximate value is 0.322185
SET 7



Q. Write a C program to solve algebraic equation x3=8 between 2 and 3 by using Method of Bisection.


Solution:
#include<stdio.h>

#include<math.h>



float f(float);

main()

{

float x,a,b,err=0.00001;

printf("\nEnter values of a and b=");

scanf("%f%f",&a,&b);

if(f(a)*f(b)>0)

printf("\nRoot does not lies between %f and %f",a,b);

else

{

while(fabs(b-a)>=err)

{

x=(a+b)/2;

printf("\na=%f, f(a)=%f, b=%f, f(b)=%f, x=%f, f(x)=%f",a,f(a),b,f(b),x,f(x));

if(f(x)==0)

{

break;

}

else if(f(a)*f(x)<0)

b=x;

else

a=x;

}

printf("\nRoot of the equation=%f",x);

}


}



float f(float x)

{

return(x*x*x-8);

}

OUTPUT

Enter values of a and b=2 3

Root of the equation=2.999992


SET 8



Q. Write a C program to solve algebraic equation x4-x-1=0between 1 and 2 by using Method of False position.

Solution:
#include<stdio.h>

#include<math.h>


float f(float);


main()

{

float x0,x1,x2,err=0.00001;

printf("\nEnter the value of x0: ");

scanf("%f",&x0);

printf("\nEnter the value of x1: ");

scanf("%f",&x1);

if(f(x0)*f(x1)>0)

printf("\nRoot does not lies between %f and %f",x0,x1);

else

{

do

{

x2=x0-((f(x0)*(x1-x0))/(f(x1)-f(x0)));

if(f(x0)*f(x2)<0)

{

x1=x2;

}

else

{

x0 = x2;

}

}while(fabs(f(x2))>err);

printf("\n\nApp.root = %f",x2);

}

}


float f(float x)

{

return(x*x*x*x-x-1);

}

OUTPUT:
Enter the value of x0: 1


Enter the value of x1: 2

App.root = 1.220743
SET 9



Q. Write a C program to solve algebraic equation x2+2x-2=0 by using Newton Raphson Method.

Solution:
#include<stdio.h>

#include<math.h>

float f(float);

float df(float);

main()

{

float a,x,h,err=0.00001;

printf("\nEnter initial value=");

scanf("%f",&a);

x=a;

do

{

h=-(f(x)/df(x));

a=x;

x=x+h;

}while(fabs(x-a)>=err);

printf("\nThe approximate value is %f",x);

}



float f(float x)

{

return(x*x+2*x-2);

}



float df(float x)

{

return(2*x+2);

}
OUTPUT:
Enter initial value=1


The approximate value is 0.732051
SET 10



Q. Write a C program to solve algebraic equation x2+2x-2=0between 0 and 1 by using Method of Bisection.


Solution:
#include<stdio.h>

#include<math.h>



float f(float);

main()

{

float x,a,b,err=0.00001;

printf("\nEnter values of a and b=");

scanf("%f%f",&a,&b);

if(f(a)*f(b)>0)

printf("\nRoot does not lies between %f and %f",a,b);

else

{

while(fabs(b-a)>=err)

{

x=(a+b)/2;

printf("\na=%f, f(a)=%f, b=%f, f(b)=%f, x=%f, f(x)=%f",a,f(a),b,f(b),x,f(x));

if(f(x)==0)

{

break;

}

else if(f(a)*f(x)<0)

b=x;

else

a=x;

}

printf("\nRoot of the equation=%f",x);

}


}



float f(float x)

{

return(x*x+2*x-2);

}

OUTPUT

Enter values of a and b=0 1

Root of the equation=0.732048


SET 11



Q. Write a C program to solve algebraic equation x3-2x2-5=0 between 1 and 3 by using Method of False position.

Solution:
#include<stdio.h>

#include<math.h>


float f(float);


main()

{

float x0,x1,x2,err=0.00001;

printf("\nEnter the value of x0: ");

scanf("%f",&x0);

printf("\nEnter the value of x1: ");

scanf("%f",&x1);

if(f(x0)*f(x1)>0)

printf("\nRoot does not lies between %f and %f",x0,x1);

else

{

do

{

x2=x0-((f(x0)*(x1-x0))/(f(x1)-f(x0)));

if(f(x0)*f(x2)<0)

{

x1=x2;

}

else

{

x0 = x2;

}

}while(fabs(f(x2))>err);

printf("\n\nApp.root = %f",x2);

}

}


float f(float x)

{

return(x*x*x-2*x*x-5);

}

OUTPUT:
Enter the value of x0: 1


Enter the value of x1: 3


App.root = 2.690647
SET 12



Q. Write a C program to solve algebraic equation 3x3+5x-40=0by using Newton Raphson Method.

Solution:
#include<stdio.h>

#include<math.h>

float f(float);

float df(float);

main()

{

float a,x,h,err=0.00001;

printf("\nEnter initial value=");

scanf("%f",&a);

x=a;

do

{

h=-(f(x)/df(x));

a=x;

x=x+h;

}while(fabs(x-a)>=err);

printf("\nThe approximate value is %f",x);

}



float f(float x)

{

return(3*x*x*x+5*x-40);

}



float df(float x)

{

return(9*x*x+5);

}
OUTPUT:
Enter initial value=1


The approximate value is 2.1378125

Thursday, 11 June 2015

OS

Q.    What is file and file system with respect to Operating system? Enumerate different types of the files in operating system.
Hint:
    For most users, the file system is the most visible aspect of an operating system. It provides the mechanism for on-line storage of and access to both data and programs of the operating system and all the users of the computer system. The file system consists of two distinct parts: a collection of files, each storing related data, and a directory structure, which organizes and provides information about all the files in the system. File systems live on devices.

File Concept
    Computers can store information on various storage media, such as magnetic disks, magnetic tapes, and optical disks. So that the computer system will be convenient to use, the operating system provides a uniform logical view of stored information. The operating system abstracts from the physical properties of its storage devices to define a logical storage unit, the file. Files are mapped by the operating system onto physical devices. These storage devices are usually nonvolatile, so the contents are persistent between system reboots.

Tuesday, 9 June 2015

PPL Lab Manual


c

  1. Write a C program to read an integer array and display the maximum element.

#include<stdio.h>
int main(){
  int a[50],size,i,big;
  printf("\nEnter the size of the array: ");
  scanf("%d",&size);
  printf("\nEnter %d elements in to the array: ”, size);
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);
printf(“The elements are”);
for(i=0;i<size;i++)
printf("%d",a[i]);

  big=a[0];
  for(i=1;i<size;i++){
      if(big<a[i])
            big=a[i];
  }
  printf("\nBiggest: %d",big);
  return 0;
}

Output:
Enter the size of the array: 5
Enter elements in to the array:3 4 9 6 7
The elements are 3 4 9 6 7
Biggest:9

  1. Write a C program to reverse the content of an array data structure.

Algorithm:
Start
1)Initialize start and end indexes.
start = 0, end = n-1
2) In a loop, swap arr[start] with arr[end] and change start and end as follows.
start = start +1; end = end – 1
Stop

Program:

#include<stdio.h>

voidrvereseArray(intarr[], int start, int end)
{
   inttemp;
   while(start < end)
   {
     temp = arr[start];  
     arr[start] = arr[end];
     arr[end] = temp;
     start++;
     end--;
 }  
}    
voidprintArray(intarr[], int size)
{
   inti;
   for(i=0; i < size; i++)
     printf("%d ", arr[i]);

   printf("\n");
}
intmain()
{
  intarr[] = {1, 2, 3, 4, 5, 6};
   printArray(arr, 6);
   rvereseArray(arr, 0, 5);
   printf("Reversed array is \n");
   printArray(arr, 6);
   getchar();
   return0;
}

Output: 6 5 4 3 2 1

  1. Write a C program to demonstrate the use of structure.
ALGORITHM:
Step1: Start the program
Step2: Declare the Structure
Step3: Declare the variable and initialize
Step4: Display
Step5: Stop the program


PROGRAM:
#include <stdio.h>
#include <string.h>

struct student
{
int id;
char name[20];
float percentage;
};

int main()
{
struct student record = {0}; //Initializing to null

record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;

printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
return 0;
}


Output:
Id is:1
Name is: Raju
Percentage: 86.5
  1. Write a program in C to demonstrate the difference between call-by-value and call-by-reference.

Call by value

#include

void main()
{
void swap(int,int);
int a,b,r;
clrscr();
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping the value for a & b is : %d %d",a,b);
}

Output:
enter value for a&b: 12 55
after swapping the value for a & b is :55 12





Call by reference



#include <stdio.h>

void swap(int*, int*);

int main()
{
int x, y;

printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
 
printf("Before Swapping\nx = %d\ny = %d\n", x, y);

swap(&x, &y);

printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
return 0;
}

void swap(int *a, int *b)
{
int temp;

temp = *b;
*b = *a;
*a = temp;
}

Output:
enter value for a&b: 4 8
after swapping the value for a & b is :8

…………………………………………………

C++

  1. Write a C++ program to illustrate the use of function overloading

ALGORITHM:

STEP 1:  Start the program.
STEP 2:  Declare the class name as fn with data members and member functions.
STEP 3:  Read the choice from the user.
STEP 4:  Choice=1 then go to the step 5.
STEP 5:  The function area() to find area of circle with one integer argument.
STEP 6:  Choice=2 then go to the step 7.
STEP 7:  The function area() to find area of rectangle with two integer argument.
STEP 8:  Choice=3 then go to the step 9.
STEP 9:  The function area() to find area of triangle with three arguments, two as Integer and one as float.
STEP 10: Choice=4 then stop the program.

PROGRAM:

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn
{
      public:
        void area(int); //circle
        void area(int,int); //rectangle
        void area(float ,int,int);  //triangle
};

void fn::area(int a)
{
      cout<<"Area of Circle:"<<pi*a*a;
}
void fn::area(int a,int b)
{
      cout<<"Area of rectangle:"<<a*b;
}
void fn::area(float t,int a,int b)
{
      cout<<"Area of triangle:"<<t*a*b;
}

void main()
{
     int ch;
     int a,b,r;
     clrscr();
     fn obj;
     cout<<"\n\t\tFunction Overloading";
     cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n:”;
     cout<<”Enter your Choice:";
     cin>>ch;

     switch(ch)
     {
              case 1:
                cout<<"Enter Radious of the Circle:";
                cin>>r;
                obj.area(r);
                break;
              case 2:
                cout<<"Enter Sides of the Rectangle:";
                cin>>a>>b;
                obj.area(a,b);
                break;
              case 3:
                cout<<"Enter Sides of the Triangle:";
                cin>>a>>b;
                obj.area(0.5,a,b);
                break;
              case 4:
                exit(0);
     }
getch();
}


Output:

 Function Overloading
              1. Area of Circle
              2. Area of Rectangle
              3. Area of Triangle
              4. Exit
              Enter Your Choice: 2

              Enter the Sides of the Rectangle: 5 5
             
              Area of Rectangle is: 25

              1. Area of Circle
              2. Area of Rectangle
              3. Area of Triangle
              4. Exit
              Enter Your Choice: 4

  1. Write a C++ program to illustrate the use of operator Overloading

ALGORITHM:

Step 1: Start the program.
Step 2: Declare the class.
Step 3: Declare the variables and its member function.
Step 4: Using the function getvalue() to get the two numbers.
Step 5: Define the function operator +() to add two complex numbers.
Step 6: Define the function operator –()to subtract two complex numbers.
Step 7: Define the display function.
Step 8: Declare the class objects obj1,obj2 and result.
Step 9: Call the function getvalue using obj1 and obj2
Step 10: Calculate the value for the object result by calling the function operator + and     operator -.
Step 11: Call the display function using obj1 and obj2 and result.
Step 12: Return the values.
Step 13: Stop the program.

PROGRAM:

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

class complex
{
              int a,b;
    public:
              void getvalue()
              {
                 cout<<"Enter the value of Complex Numbers a,b:";
                 cin>>a>>b;
              }
              complex operator+(complex ob)
              {
                            complex t;
                            t.a=a+ob.a;
                            t.b=b+ob.b;
                            return(t);
              }
              complex operator-(complex ob)
              {
                            complex t;
                            t.a=a-ob.a;
                            t.b=b-ob.b;
                            return(t);
              }
              void display()
              {
                            cout<<a<<"+"<<b<<"i"<<"\n";
              }
};

void main()
{
    clrscr();
   complex obj1,obj2,result,result1;

   obj1.getvalue();
   obj2.getvalue();

   result = obj1+obj2;
   result1=obj1-obj2;

   cout<<"Input Values:\n";
   obj1.display();
   obj2.display();
  
   cout<<"Result:";
   result.display();
   result1.display();
  
   getch();
}

Output:

Enter the value of Complex Numbers a, b
4                  5
Enter the value of Complex Numbers a, b
2                  2
Input Values
4 + 5i
2 + 2i
Result
6 +   7i
2 +   3i



  1. Write a C++ program to illustrate the use inline function

ALGORITHM:

Step 1: Start the pogram.
Step 2: Declare the class.
Step 3: Declare and define the inline function for multiplication and cube.
Step 4: Declare the class object and variables.
Step 5: Read two values.
Step 6: Call the multiplication and cubic functions using class objects.
Step 7: Return the values.
Step 8: Display.
Step 9: Stop the program.

PROGRAM:

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

class line
{
   public:
              inline float mul(float x,float y)
              {
                            return(x*y);
              }
              inline float cube(float x)
              {
                            return(x*x*x);
              }
};

void main()
{
              line obj;
              float val1,val2;
              clrscr();
              cout<<"Enter two values:";
              cin>>val1>>val2;
              cout<<"\nMultiplication value is:"<<obj.mul(val1,val2);
              cout<<"\n\nCube value is          :"<<obj.cube(val1)<<"\t"<<obj.cube(val2);
              getch();
}

Output:

              Enter two values: 5  7
              Multiplication Value is: 35
              Cube Value is: 25 and 343

8. Write a C++ program to illustrate the use of virtual base class.

ALGORITHM:

Step 1: Start the program.
Step 2: Declare the base class student.
Step 3: Declare and define the functions getnumber() and putnumber().
Step 4: Create the derived class test virtually derived from the base class student.
Step 5: Declare and define the function getmarks() and putmarks().
Step 6: Create the derived class sports virtually derived from the base class student.          
Step 7: Declare and define the function getscore() and putscore().
Step 8: Create the derived class result derived from the class test and sports.
Step 9: Declare and define the function display() to calculate the total.
Step 10: Create the derived class object obj.
Step 11: Call the function get number(),getmarks(),getscore() and display().
Step 12: Stop the program.




PROGRAM:

#include<iostream.h>
#include<conio.h>
 
class student
{
   int rno;
  public:
   void getnumber()
   {
              cout<<"Enter Roll No:";
              cin>>rno;
   }
   void putnumber()
   {
              cout<<"\n\n\tRoll No:"<<rno<<"\n";
   }
};
 
class test:virtual public student
{
  
  public:
   int part1,part2;
   void getmarks()
   {
              cout<<"Enter Marks\n";
              cout<<"Part1:";
              cin>>part1;
              cout<<"Part2:";
              cin>>part2;
   }
   void putmarks()
   {
              cout<<"\tMarks Obtained\n";
              cout<<"\n\tPart1:"<<part1;
              cout<<"\n\tPart2:"<<part2;
   }
};

class sports:public virtual student
{
 
  public:
    int score;
    void getscore()
    {
              cout<<"Enter Sports Score:";
              cin>>score;
    }
    void putscore()
    {
              cout<<"\n\tSports Score is:"<<score;
    }
};

class result:public test,public sports
{
    int total;
  public:
   void display()
   {
      total=part1+part2+score;
      putnumber();
      putmarks();
      putscore();
      cout<<"\n\tTotal Score:"<<total;
   }
};
 
void main()
{
   result obj;
   clrscr();
   obj.getnumber();
   obj.getmarks();
   obj.getscore();
   obj.display();
   getch();
}

Output:

              Enter Roll No: 200
 
              Enter Marks
 
              Part1: 90
              Part2: 80
              Enter Sports Score: 80


              Roll No: 200
              Marks Obtained
              Part1: 90
              Part2: 80
              Sports Score is: 80
              Total Score is: 250
9. Write a C++ program to illustrate the use of single inheritance (Public/Private)

Algorithm:

Step 1: Start the program.
Step 2: Declare the base class emp.
Step 3: Define and declare the function get() to get the employee details.
Step 4: Declare the derived class salary.
Step 5: Declare and define the function get1() to get the salary details.
Step 6: Define the function calculate() to find the net pay.
Step 7: Define the function display().
Step 8: Create the derived class object.
Step 9: Read the number of employees.
Step 10: Call the function get(),get1() and calculate() to each employees.
Step 11: Call the display().
Step 12: Stop the program


Program:

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

class emp
{
   public:
     int eno;
     char name[20],des[20];
     void get()
     {
              cout<<"Enter the employee number:";
              cin>>eno;
              cout<<"Enter the employee name:";
              cin>>name;
              cout<<"Enter the designation:";
              cin>>des;
      }
};

class salary:public emp
{
     float bp,hra,da,pf,np;
    public:
     void get1()
     {             
              cout<<"Enter the basic pay:";
              cin>>bp;
              cout<<"Enter the Humen Resource Allowance:";
              cin>>hra;
              cout<<"Enter the Dearness Allowance :";
              cin>>da;
              cout<<"Enter the Profitablity Fund:";
              cin>>pf;
     }
     void calculate()
     {
              np=bp+hra+da-pf;
     }
     void display()
     {
              cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t"<<pf<<"\t"<<np<<"\n";
     }
};

void main()
{
    int i,n;
    char ch;
    salary s[10];
    clrscr();
    cout<<"Enter the number of employee:";
    cin>>n;
    for(i=0;i<n;i++)
    {
              s[i].get();
              s[i].get1();
              s[i].calculate();
    }
    cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
    for(i=0;i<n;i++)
    {
              s[i].display();
    }
    getch();
}

Output:

Enter the Number of employee:1
Enter the employee No: 150
Enter the employee Name: ram
Enter the designation: Manager
Enter the basic pay: 5000
Enter the HR allowance: 1000
Enter the Dearness allowance: 500
Enter the profitability Fund: 300

E.No   E.name   des      BP    HRA   DA   PF     NP
150    ram      Manager  5000  1000  500  300    6200


10. Write a C++ program to illustrate the use of file open() and close() operation.

ALGORITHM:

STEP 1:  Start the program.
STEP 2:  Declare the variables.
STEP 3:  Read  the file name.
STEP 4:  open the file to write the contents.
STEP 5:  writing the file contents up to reach a particular condition.
STEP 6:  Stop the program.

PROGRAM: 

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
void main()
{
              char c,fname[10];
              ofstream out;
              cout<<"Enter File name:";
              cin>>fname;
              out.open(fname);
              cout<<"Enter contents to store in file (Enter # at end):\n";
              while((c=getchar())!='#')
              {
                            out<<c;
              }
              out.close();
              getch();
}

Output:

              Enter File name: one.txt
              Enter contents to store in file (enter # at end)
 
              Master of Computer Applications#







11. Write a C++ program to illustrate the use of Class template

#include <iostream>
using namespace std;
template <typename T>
class Number {
private:
T value;
public:
Number(T value) { this->value = value; };
T getValue() { return value; }
void setValue(T value) { this->value = value; };
};
int main() {
Number<int> i(55);
cout << i.getValue() << endl;
Number<double> d(55.66);
cout << d.getValue() << endl;
Number<char> c('a');
cout << c.getValue() << endl;
Number<string> s("Hello");
cout << s.getValue() << endl;
}


12. Write a C++ program to illustrate the use of function template.

#include <iostream>
using namespace std;
template <typename T>
void mySwap(T &a, T &b);
// Swap two variables of generic type passed-by-reference
// There is a version of swap() in <iostream>
int main() {
int i1 = 1, i2 = 2;
mySwap(i1, i2); // Compiler generates mySwap(int &, int &)
cout << "i1 is " << i1 << ", i2 is " << i2 << endl;
char c1 = 'a', c2 = 'b';
mySwap(c1, c2); // Compiler generates mySwap(char &, char &)
cout << "c1 is " << c1 << ", c2 is " << c2 << endl;
double d1 = 1.1, d2 = 2.2;
mySwap(d1, d2); // Compiler generates mySwap(double &, double &)
cout << "d1 is " << d1 << ", d2 is " << d2 << endl;
//mySwap(i1, d1);
// error: no matching function for call to 'mySwap(int&, double&)'
// note: candidate is:
// note: template<class T> void mySwap(T&, T&)
}
template <typename T>
void mySwap(T &a, T &b) {
T temp;
temp = a;
a = b;
b = temp;
}
……………………………………………………………

JAVA


13.Write a Java Program to add two numbers and display the result

Program:

importjava.util.Scanner;
 
classAddNumbers
{
publicstaticvoidmain(Stringargs[])
{
intx, y, z;
System.out.println("Enter two integers to calculate their sum ");
Scanner in =newScanner(System.in);
x =in.nextInt();
y =in.nextInt();
z =x +y;
System.out.println("Sum of entered integers = "+z);
}


14. Write a Java program to reverse a number.

Program:

import java.util.Scanner;
public classReverseNumberExample {

   
publicstatic voidmain(Stringargs[]) {

       
System.out.println("Please enter number to be reversed using Java program: ");
       
intnumber = newScanner(System.in).nextInt();
     
       
intreverse = reverse(number);
       
System.out.println("Reverse of number: " + number + " is " + reverse(number));  
   
    }

publicstatic intreverse(intnumber){
       
intreverse = 0;
       
intremainder = 0;
        do{
            remainder = number%10;
            reverse = reverse*10 + remainder;
            number = number/10;
         
        }while(number > 0);
     
       
returnreverse;
    }


}
Output:

Please enter number to be reversed using Java program:
1234
Reverse of number: 1234 is 4321


  1. Write a Java program to find out the factorial of a number.


Program:

importjava.util.Scanner;
 
classFactorial
{
publicstaticvoidmain(String args[])
{
intn, c, fact = 1;
 
System.out.println("Enter an integer to calculate it's factorial");
Scanner in = newScanner(System.in);
 
n = in.nextInt();
 
if( n < 0 )
System.out.println("Number should be non-negative.");
else
{
for( c = 1 ; c <= n ; c++ )
fact = fact*c;
 
System.out.println("Factorial of "+n+" is = "+fact);
}
}
}

Output:

Enter an integer to calculate it’s factorial 6
Factorial of 6 is = 720