Sunday, 18 September 2016

PROGRAMS OF C++



  • PROGRAM TO FIND THE SIZE OF DATA TYPES.
  • PROGRAM BASED ON INCREMENT / DECREMENT OPERATORS.
  • PROGRAM BASED ON SWAPPING.
  • WRITE A PROGRAM TO FIND THE SUM OF TWO NUMBERS.
  • WRITE A PROGRAM TO REVERSE A NUMBER WITHOUT USING ANY CASE .
  • WRITE A PROGRAM TO MULTIPLY 2 FLOATING NUMBERS.
  • WRITE A PROGRAM TO PRINT "HELLO WORLD" IN DOUBLE QUOTES.
  • WRITE A PROGRAM TO FIND SMALLEST NUMBER WITHOUT USING ANY CASE.
  • WRITE A PROGRAM TO FIND THE FACTORIAL OF A NUMBER.
  • PROGRAM TO FIND THE PERIMETER AND AREA OF RECTANGLE.
  • WRITE A PROGRAM TO FIND THE MULTIPLICATION TABLE OF ANY NUMBER.
  • WRITE A PROGRAM TO DISPLAY THE FOLLOWING OUTPUT USING A SINGLE COUT STATEMENT.                                                                                                                       subject              marks                                                                                                                        mathematics=       86                                                                                                                          physics        =        96                                                                                                                         chemistry   =         98
  • WRITE A PROGRAM WHICH ACCEPT TEMPERATURE IN FAHRENHEIT AND PRINT IN CENTIGRADES. 
  • WRITE A PROGRAM TO FIND THE SIMPLE INTEREST.
  • WRITE A PROGRAM WHICH ACCEPTS A CHARACTER AND PRINTS ITS ASCII VALUE.
  • WRITE A PROGRAM TO CHECK WHEATHER THE GIVEN NUMBER IS POSITIVE OR NEGATIVE.



   
* WRITE A PROGRAM TO FIND THE SIZE OF DATA TYPES.


#include<iostream.h>
#include<conio.h>
    void main()
{
 int a;
 float b;
 char c; 
 double d;
 short e;

    cout<<"size of integer is %d= " <<sizeof(a);    //we use %d for integer
    cout<<"size of float is %f ="<<sizeof(b);         //we use %f for float 
    cout<<"size of character is %c = "<<sizeof(c); //we use %c for character
    cout<<"size of double is %f "<<sizeof(d);      //we use %f for double
    cout<<"size of short is %d="<<sizeof(e);        //we use %d for short

getch();

}
SAMPLE OUTPUT
SIZE OF INTEGER IS =2
SIZE OF FLOAT IS =4
SIZE OF CHARACTER IS=1
SIZE OF DOUBLE IS =8
SIZE OF SHORT IS =2

========================================================================


*WRITE  PROGRAMS TO SHOW INCREMENT OF A NUMBER .

CASE 1:

#include<iostream.h>
#include<conio.h>
void main();
{
   int i,j;
   i=5;
   j=++i;
   cout<<j;
   cout<<"\n"<<i;

getch();

}

CASE 2:

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

    void main( )
{
   int i,j;
   i=5;
   j=i++;
  cout<<"\n"<<j;
  cout<<"\n"<<i;

getch();

CASE 3:

#include<iostream.h.
#include<conio.h>
   void main ()
{
  int i;
  i=5;
  i= ++i + ++i + i++;
  cout<<"\n"<<i;

getch();

}

CASE 4:

#include<iostream.h>
#include<conio.h>
 void main()
{
 int i;
 i=5;
 j=++i + i++ + ++i + i++;
 cout<<"\n"<<j;

 getch();

}

CASE 5:

#include<iostream.h>
#include<conio.h>
void main ()
{
 int i,j;
 i=2;
 j=5;
 i=j++ + --i + j-- + --i + i-- + j--;
 cout<<"\n"<<i;
 getch( );
}
========================================================================

*WRITE A PROGRAM TO SHOW SWAPPING OF NUMBERS.
  CASE 1:

#include<iostream.h>
#include<conio.h.
   void main ()
{
 int a,r;
 a=52;
 r=a/10;                   // value of a is divided by 10 
 cout<<"\n"<<r;
getch( )
}

CASE 2:

#include<iostream.h>
#include<conio.h>
 void main ()
{
  int n,s,r;                  //initialization
  n=123;
  s=0;
r=n%10
  n=n/10;                  //value of n is divided by 10
  s=s+r;       
  r=n%10;                // % symbol is called modulus it gives the remainder 
  s=s+r;
  n=n/10;
  s=s+n;
  cout<<"\n"<<s;   //this line represent the output  

getch();                 //getch means get the character which holds the screen and show the output

}
========================================================================

* WRITE A PROGRAM TO FIND THE SUM OF TWO NUMBERS .
#include<iostream.h>
#include<conio.h>
  void main ()
{
 int a, b, sum=0;     //initialization of variables
 cout<<"enter the value of a ";    //print statement 
 cin>>a;                                       //output statement 
 cout<<"enter the value of b ";
 cin>>b;
 sum=a+b;                                   //sum statement
 cout<<"\n sum is "<<sum;         //this line shows the sum of two numbers
getch();
}
=======================================================================
* WRITE A PROGRAM TO REVERSE NUMBER WITHOUT USING ANY CASE.
 #include<iostream.h>
# include<conio.h>
 void main ()
{
int n=521,s=0,r;
r=n%10;
  n=n/10;                  
  s=s*10+r 
  r=n%10;                
  s=s*10+r
  n=n/10   
  s=s*10+n;
getch();
}

SAMPLE OUTPUT
125
========================================================================

* WRITE A PROGRAM TO MULTIPLY TWO FLOATING NUMBERS

#include<iostream.h>
#include<conio.h.
 void main ()
{
double a,b,mul;
cout<<"\nenter the first number %f";
cin>>a;
cout<<"\n enter the second number%f";
cin>>b;
mul=a*b;
cout<<mul;
getch();
}

======================================================================= *WRITE A PROGRAM TO PRINT "HELLO WORLD IN DOUBLE QUOTES.

#include<iostream.h>
#include<conio.h>
void main ()
{
cout<<"\"hello world\"";
getch();
}

SAMPLE OUTPUT

"HELLO WORLD"
=======================================================================
*WRITE A PROGRAM TO FIND SMALLEST NUMBER WITHOUT USING ANY CASE.

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

int compare(int a,int b)
{
return(a+4<b)? a:b;
}

  int main ()
{
  cout<<"\nsmallest number is :"<<compare(11,12);
  cout<<"\n"smallest number is :"<<compare(55,88);
  cout<<"\n"smallest number is :"<<compare(100,89);

getch();
return0;
}

SAMPLE OUTPUT

SMALLEST NUMBER IS 11
SMALLEST NUMBER IS 55
SMALLEST NUMBER IS 89
======================================================================= *WRITE A PROGRAM TO FIND THE FACTORIAL OF A NUMBER .

#include<iostream.h>
#include<conio.h>
int factorial(int var)
{
int fact=1;
for(int i=1;i<=var;i++)
fact=fact*i;
return fact;
}

  int main()
{
 cout<<"\nfactorial of a number is :"<<factorail(7);
 getch();
 return 0;
}

SAMPLE OUTPUT

FACTORIAL IS 5040.
 =======================================================================
* WRITE A PROGRAM TO FIND THE PERIMETER AND AREA OF A RECTANGLE.

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

void main ()
{

clrscr();
int length,width ,perimeter ,area;        //Declaration
cout<<"length=\n";                             //prompt user
cin>>length;
cout<<"width=\n" ;
cin>>width;

perimeter=2*(length+width);            //formula to find the perimeter
area=length*width;                            //formula to find the area

cout<<endl;
       <<"perimeter is "<<perimeter;
cout<<endl;
       <<"area is "<<area;
        <<endl;
getch();
}
========================================================================
*WRITE A PROGRAM TO FIND THE MULTIPLICATION TABLE OF  A NUMBER.

#include<iostream.h>
#include<conio.h>
void main()
{
int n;
cout<<"enter the number";
cin>>n;
for(int  i=1;i<=10;i++)
{
cout<<n<<" * "<<i<<"="<<n*i<<endl;
}
getch();
}
========================================================================
*  WRITE A PROGRAM TO DISPLAY THE FOLLOWING OUTPUT USING A SINGLE COUT STATEMENT.                                                                                                                                   subject              marks                                                                                                                               mathematics       86                                                                                                                                    physics                96                                                                                                                                   chemistry            98
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();
cout<<"subject"<<"\marks"<<"mathematics\n"<<86<<"physics\n"<<96<<"chemistry\n"<<98;
getch();
}

SAMPLE OUTPUT
SUBJECT                MARKS
MATHEMATICS     86
PHYSICS                  96 
CHEMISTRY             98
========================================================================
*WRITE A PROGRAM WHICH ACCEPT TEMPERATURE IN FAHRENHEIT AND PRINT IN CENTIGRADES.

#include<iostream.h>
#include<conio.h>
void main()
{
int f,c;
cout<<"enter the temperature in fahrenheit";
cin>>f;

c=5*(f-39)/9;                    //formula which is converting the temperature 

cout<<"temperature in celcius is ";
cin>>c;

getch();
}
========================================================================
*WRITE A PROGRAM TO FIND THE SIMPLE INTEREST.

#include<iostream.h>
#include<conio.h>
void main ()
{
int p,r,t,s;
cout<<"enter the principal ";      // user input
cin>>p;                                        //the values

cout<<"enter the rate ";
cin>>r;

cout<<"enter the time";
cin>>t;

s=(p*r*t)/100;       //formula to find the simple interest

cout<<"simple interest is "<<s;

getch();
}
========================================================================
*WRITE A PROGRAM WHICH ACCEPTS A CHARACTER AND PRINTS ITS ASCII VALUE

#include<iostream.h>
#include<conio.h>
int main ()                 //int means it returns a value otherwise it shows an error.
{

char ch;

cout<<"enter any character ";
cin>>ch;

cout<<"its ASCII value is" <<(int)ch;        

return 0;
getch();
}
========================================================================
*WRITE A PROGRAM TO CHECK WHEATHER THE GIVEN NUMBER IS POSITIVE OR NEGATIVE.

#include<iostream.h>
#include<conio.h>
void main()
{
int n;

cout<<"enter the number ";
cin>>a;

(a>0)?cout<<"the number is positive ":cout<<"the number is negative";  //using ternary operator

getch();
}
========================================================================


Saturday, 3 September 2016

characteristics of an array


  • ARRAY TERMINOLOGY
  • CHARACTERISTICS OF AN ARRAY 
 

                                               ARRAY TERMINOLOGY

SIZE : Number of elements or capacity to store elements in an array is called its size . It is always                    mentioned in brackets([ ]).


TYPE: Types refer to data type.It decides which type of element is stored in the array .It also instructs              the compiler to reserve memory according to data type .

BASE: The address of the first element (0th) is a base address . The array name itself stores address                of the first  element.

INDEX: The array name is used to refer to the array element. For example , num[x] , num is array                       name and x is index.The value of x begins from 0 to onwards depending on the size of the                   array .The index value is always an integer .

RANGE: Index of an array i.e value of x varies from lower bound to upper bound while writing or                     reading elements from an array . For example  in  num[100]  the range of index is 0 to 99.

WORD: It includes the space required for an element. in each memory location,computer can store a                  data piece.The space occupation varies from machine to machine.If the size of element is                     more than word (one byte) then it occupies two successive locations.The variables of data                    type  int,float,long need more than one byte in memory.

                                  CHARACTERISTICS OF AN ARRAY

  1. The declaration int a[5]  is nothing but creation of five variables of integer type in memory instead of declaring five variables for five values, the programmer can define them in an array .
  2. All the elements of an array share the same name,and they are distinguished from one another with the help of the element number.
  3. The element number in an array play a major role for calling each element.
  4. Any particular element of an array can be modified separately without disturbing the other elements.
                    int a[5]={1,2,3,4,8};
            If a programmer needs to replace  8 with 10,then it need not require changing all other                         numbers except 8.To carry out this task the statement  a[4]=10 can be used. Here,other four               elements are not disturbed.

      5. Any element of an array  a[ ]  can be assigned/equated to another ordinary variable or array                 variable of its type.
   

  EXAMPLE:
   
       b=a[2];
      a[2]=a[3];
  •   In the statement   b=a[2]  or vice versa,the value of a[2]  is asigned to 'b',where 'b' is an integer.
  • In the statement  a[2]=a[3]or vice versa ,the value of a[2] is assigned to  a[3],where both the elements are of the same array .
  • The array elements are stored in continuous memory locations.
     6. Array elements are stored in contigious memory locations.
 
     7. Once the arrays is declared,its lowest boundary cannot be changed but upper boundary can be               expanded .The array name itself is  a constant pointer and we cannot modify it.Therefor, the                 lowest boundary of an array cannot be expanded.In other words ,even if the boundary exceeds             then specified,nothing happens.the compiler throws no errors. 
     8. We know that an array name itself is a pointer . Though it is a pointer , it does not need '*'                    operator.The brackets([ ]) automatically denote that the variable is a pointer.
     9. All the elements of an array share the same name, and they are distinguished from one another            with the help of the element number.
   10. The amount of memory required for an array depends upon the data type and the number of                elements.The total size in bytes for a single dimensional array is computed as shown below:
               total bytes=sizeof(data type)  x size of array 
   11. The  operation such as insertion,deletion of an element can be done with the list  but cannot be            done with an array .Once an array is created we cannot remove or insert memory location. An              element can be deleted,replaced but the memory location as it is.

Friday, 2 September 2016

Initialization of Array

                              INITIALIZATION OF ARRAY 

The array initialization can be done as under:

int a[5]={1,2,3,4,5};

Here 5 elements are stored in an array 'a'. List of elements initialized is shown within the braces.The elements are stored sequentially in separate locations.Then the question arises how to call individually to each element from this bunch of integer elements.

Reading of element begins from '0'.By indicating the position of elements, one can retrieve element of an array.Array elements are called with array names.

a[0] refers to 1st element i.e 1.
a[1] refers to 2nd element i.e 2.
a[2] refers to 3rd element i.e 3.
a[3] refers to 4th element i.e 4.
a[4] refers to 5th element i.e 5.

EXAMPLE:

To store more than one value the programming languages have an built data structure called an array .

  1.  int num[5] ;
          In the above declaration.an integer array of five elements is declared .Memories for five                       integers,i.e. successive 10 bytes ,are reserved for the num array.To initialize the num array                   following syntax can be used.



      2. int num[5] = {1,2,4,2,5};

          In the above statement ,all elements are initialized .It is also possible to initialize individual                 element by specifying the subscript number in the square bracket following the array name.
          Array elements are accessed as follows:

              num[0]=1;
              num[1]=2;
              num[2]=4;
              num[3]=2;
              num[4]=5;

The initialization can be done at the compile time or dynamic at the run time .The above is an example of compile time initialization./

In the above array, the element num[0]  i.e 1 is the lowest bound and num[4] i.e 5 is the last element .In c and c++ ,there is no  bound checking .Hence the programmer has to check it while accessing or storing elements.Once   the array is declared , its lowest bound cannot be changed but the upper bound can be expanded.The array name itself is a constant pointe,and therefore we cannot modify it. Storing elements in contigious memory locations can expand the upper bound.  


The array name itself is a pointer .The array num  is pointer to the first element i.e  num cotains address of memory locations where element 1 is stored .The address stored in the array name is called the base address .



To access individual elements,the following syntax is used:

num[0] refers to the 1

num[1] refers to the 2


num[2] refers to the 4


num[3] refers to the 2


num[4] refers to the 5


Thus, an array is a colection of elements of the same data type ,stored in unique and successive memory location.