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


No comments:

Post a Comment