Saturday, September 13, 2014

c programmes

1.  Program
#include
#include

void main()
{
   cout << "Hello World";
   return 0;
getch();
}

Output
            Hello World
2.  Program

#include 
#include
            extern int a, b;
extern int c;
extern float f;
  
void main ()
{
                        int a, b;
            int c;
            float f;
 
            a = 10;
            b = 20;
            c = a + b;
 
             cout << c << endl ;
 
            f = 70.0/3.0;
            cout << f << endl ;
 
            return 0;
 
getch();
}

Output
30
23.3333
 
3. Program
            #include 
            #include
            int g;
 
            int main ()
            {
    int a, b;
 
  a = 10;
  b = 20;
  g = a + b;
 
  cout << g;
 
  return 0;
 
getch(); 
}

Output
                        g:30
4. Program
#include 
#include
 
int g = 20;
 
void main ()
{
  int g = 10;
 
  cout << g;
 
        return 0;
  getch();
}

Output
               10
 
5. Program
#include 
#include
 
void main()
{
            const int  LENGTH = 10;
            const int  WIDTH  = 5;
            const char NEWLINE = '\n';
            int area;  
   
             area = LENGTH * WIDTH;
            cout << area;
             cout << NEWLINE;
             return 0;
 
   getch();
}

Output
                    50
 
6. Program
#include 
#include
int main()
{
     short int i;           
     short unsigned int j;  
 
      j = 50000;
 
      i = j;
       cout << i << " " << j;
 
       return 0;
   getch();
}

Output
                    -15536 50000
 
 
 
7. Program
#include 
#include 
void func(void);
                   main()
{
     while(count--)
    {
       func();
    }
    return 0;
}
void func( void )
{
    static int i = 5;
    i++;
    std::cout << "i is " << i ;
    std::cout << " and count is " << count << std::endl;
getch();
}

Output
i is 6 and count is 9
i is 7 and count is 8
i is 8 and count is 7
i is 9 and count is 6
i is 10 and count is 5
i is 11 and count is 4
i is 12 and count is 3
i is 13 and count is 2
i is 14 and count is 1
i is 15 and count is 0
 
8. Program
#include 
#include
            int max(int num1, int num2);
            void main ()
            {
  
                        int a = 100;
                       int b = 200;
                       int ret;
 
   
                        ret = max(a, b);
                       cout << "Max value is : " << ret << endl;
 
   return 0;
}
 
int max(int num1, int num2) 
{
   
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
            getch();
            }

Output
                    Max value is : 200

9. Program
          #include 
            #include
            int sum(int a, int b=20)
            {
                        int result;
 
                        result = a + b;
  
                        return (result);
            }
 
            void main ()
            {
                        int a = 100;
                         int b = 200;
                        int result;
 
                        result = sum(a, b);
                        cout << "Total value is :" << result << endl;
 
                         result = sum(a);
                        cout << "Total value is :" << result << endl;
 
                        return 0;
            getch();
            }
 
Output
 
                    Total value is: 300
                        Total value is: 120
 
10. Program
 
#include 
#include
 
void main ()
{
   
   short  s;
   int    i;
   long   l;
   float  f;
   double d;
   
   
   s = 10;      
   i = 1000;    
   l = 1000000; 
   f = 230.47;  
   d = 30949.374;
   
     cout << "short  s :" << s << endl;
     cout << "int    i :" << i << endl;
     cout << "long   l :" << l << endl;
     cout << "float  f :" << f << endl;
     cout << "double d :" << d << endl;
     return 0;
            getch();
            }
 
Output
 
short  s :10
int    i :1000
long   l :1000000
float  f :230.47
double d :30949.4
 
11. Program
 
#include 
#include
#include 
 
int main ()
{
   short  s = 10;
   int    i = -1000;
   long   l = 100000;
   float  f = 230.47;
   double d = 200.374;
 
   mathematical operations;
   cout << "sin(d) :" << sin(d) << endl;
   cout << "abs(i)  :" << abs(i) << endl;
   cout << "floor(d) :" << floor(d) << endl;
   cout << "sqrt(f) :" << sqrt(f) << endl;
   cout << "pow( d, 2) :" << pow(d, 2) << endl;
 
         return 0;
   getch();
}

Output
 
sign(d) :-0.634939
abs(i)  :1000
floor(d) :200
sqrt(f) :15.1812
pow( d, 2 ) :40149.7
 
12. Program
 
#include 
#include
#include 
#include 
                    int main ()
{
     int i,j;
 
 
      srand( (unsigned)time( NULL ) );
 
     for( i = 0; i < 10; i++ )
      {
     
                  j= rand();
                  cout <<" Random Number : " << j << endl;
      }
 
      return 0;
getch();
}
 
 
 
Output
 
       Random Number : 1748144778
 Random Number : 630873888
 Random Number : 2134540646
 Random Number : 219404170
 Random Number : 902129458
 Random Number : 920445370
 Random Number : 1319072661
 Random Number : 257938873
 Random Number : 1256201101
 Random Number : 580322989
 
13. Program
 
#include 
#include
#include 
using std::setw;
 
int main ()
{
   int n[ 10 ]; 
 
   for ( int i = 0; i < 10; i++ )
   {
      n[ i ] = i + 100;
   }
   cout << "Element" << setw( 13 ) << "Value" << endl;
 
   
   for ( int j = 0; j < 10; j++ )
   {
      cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
   }
 
   return 0;
 
getch();
}
 
Output
 
Element        Value
      0          100
      1          101
      2          102
      3          103
      4          104
      5          105
      6          106
      7          107
      8          108
      9          109
 
14. Program
          
          #include 
            #include
            int main ()
            {
   char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
 
   cout << "Greeting message: ";
   cout << greeting << endl;
 
   return 0;
            getch();
            }
 
Output
          
          Greeting message: Hello
 
15. Program
 
#include 
#include 
 
int main ()
{
   char str1[10] = "Hello";
   char str2[10] = "World";
   char str3[10];
   int  len ;
 
   strcpy( str3, str1);
   cout << "strcpy( str3, str1) : " << str3 << endl;
 
 
   strcat( str1, str2);
   cout << "strcat( str1, str2): " << str1 << endl;
 
   
   len = strlen(str1);
   cout << "strlen(str1) : " << len << endl;
               return 0;
 getch();
}
 
Output
 
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10
 
16. Program
 
#include 
#include
#include 
            int main ()
{
   string str1 = "Hello";
   string str2 = "World";
   string str3;
   int  len ;
 
   str3 = str1;
   cout << "str3 : " << str3 << endl;
 
   str3 = str1 + str2;
   cout << "str1 + str2 : " << str3 << endl;
 
   
   len = str3.size();
   cout << "str3.size() :  " << len << endl;
 
   return 0;
            getch();
}
 
Output
 
str3 : Hello
str1 + str2 : HelloWorld
str3.size() :  10
 
 
17. Program
 
#include 
#include
int main ()
{
   int  var = 20;   
   int  *ip;        
   ip = &var;       
   cout << "Value of var variable: ";
   cout << var << endl;
 
 
   cout << "Address stored in ip variable: ";
   cout << ip << endl;
 
   cout << "Value of *ip variable: ";
   cout << *ip << endl;
 
   return 0;
            getch();
}
 
 
 
Output
 
          Value of var variable: 20
            Address stored in ip variable: 0xbfc601ac
            Value of *ip variable: 20
 
18. Program
 
#include 
#include
int main ()
{
   
   int    i;
   double d;
 
   int&    r = i;
   double& s = d;
   
   i = 5;
   cout << "Value of i : " << i << endl;
   cout << "Value of i reference : " <<<< endl;
 
   d = 11.7;
   cout << "Value of d : " << d << endl;
   cout << "Value of d reference : " <<<< endl;
   
   return 0;
getch();
}
 
 
 
Output
 
Value of i : 5
Value of i reference : 5
Value of d : 11.7
Value of d reference : 11.7
 
19. Program
 
#include 
#include 
 
int main( )
{
   char name[50];
 
   cout << "Please enter your name: ";
   cin >> name;
   cout << "Your name is: " << name << endl;
getch();
         }
 
Output
 
Please enter your name: Ammu
Your name is: Ammu
 
20. Program
 
#include 
#include
#include 
 
            struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   struct Books Book1;        
   struct Books Book2;        
   
 
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan"); 
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;
 
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;
 
   cout << "Book 1 title : " << Book1.title <<endl;
   cout << "Book 1 author : " << Book1.author <<endl;
   cout << "Book 1 subject : " << Book1.subject <<endl;
   cout << "Book 1 id : " << Book1.book_id <<endl;
 
   cout << "Book 2 title : " << Book2.title <<endl;
   cout << "Book 2 author : " << Book2.author <<endl;
   cout << "Book 2 subject : " << Book2.subject <<endl;
   cout << "Book 2 id : " << Book2.book_id <<endl;
 
   return 0;
 getch();
}
 
Output
 
          Book 1 title : Learn C++ Programming
Book 1 author : Chand Miyan
Book 1 subject : C++ Programming
Book 1 id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Yakit Singha
Book 2 subject : Telecom
Book 2 id : 6495700
 
21. Program
 
#include 
#include
 
class Box
{
   public:
      double length;   
      double breadth;  
      double height;   
};
 
int main( )
{
   Box Box1;        
   Box Box2;        
   double volume = 0.0;     
 
   Box1.height = 5.0; 
   Box1.length = 6.0; 
   Box1.breadth = 7.0;
 
   Box2.height = 10.0;
   Box2.length = 12.0;
   Box2.breadth = 13.0;
   
   volume = Box1.height * Box1.length * Box1.breadth;
   cout << "Volume of Box1 : " << volume <<endl;
 
   volume = Box2.height * Box2.length * Box2.breadth;
   cout << "Volume of Box2 : " << volume <<endl;
   return 0;
 getch();
}
 
Output
 
            Volume of Box1 : 210
            Volume of Box2 : 1560
 
22. Program
 
#include 
#include 
                   class Shape 
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
   protected:
      int width;
      int height;
};
 
class Rectangle: public Shape
{
   public:
      int getArea()
      { 
         return (width * height); 
      }
};
 
int main(void)
{
   Rectangle Rect;
 
   Rect.setWidth(5);
   Rect.setHeight(7);
 
   cout << "Total area: " << Rect.getArea() << endl;
 
   return 0;
getch();
}
 
Output
 
                        Total area: 35
 
23. Program
 
#include 
#include 
class Shape 
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
   protected:
      int width;
      int height;
};
 
class PaintCost 
{
   public:
      int getCost(int area)
      {
         return area * 70;
      }
};
 
class Rectangle: public Shape, public PaintCost
{
   public:
      int getArea()
      { 
         return (width * height); 
      }
};
 
int main(void)
{
   Rectangle Rect;
   int area;
 
   Rect.setWidth(5);
   Rect.setHeight(7);
 
   area = Rect.getArea();
   
  
   cout << "Total area: " << Rect.getArea() << endl;
 
   
   cout << "Total paint cost: $" << Rect.getCost(area) << endl;
 
   return 0;
            getch();
}
 
Output
 
Total area: 35
Total paint cost: $2450
 
24. Program
 
#include 
#include 
 
class printData 
{
   public:
      void print(int i) 
     {
        cout << "Printing int: " << i << endl;
      }
 
      void print(double  f) {
        cout << "Printing float: " << f << endl;
      }
 
      void print(char* c) 
    {
        cout << "Printing character: " << c << endl;
      }
};
 
int main(void)
{
   printData pd;
 
   pd.print(5);
   pd.print(500.263);
   pd.print("Hello C++");
 
   return 0;
getch();
}
 
 
Output
 
Printing int: 5
Printing float: 500.263
Printing character: Hello C++
 
25. Program
 
#include 
#include 
 
class Box
{
   public:
 
      double getVolume(void)
      {
         return length * breadth * height;
      }
      void setLength( double len )
      {
          length = len;
      }
 
      void setBreadth( double bre )
      {
          breadth = bre;
      }
 
      void setHeight( double hei )
      {
          height = hei;
      }
            Box operator+(const Box& b)
      {
         Box box;
         box.length = this->length + b.length;
         box.breadth = this->breadth + b.breadth;
         box.height = this->height + b.height;
         return box;
      }
   private:
      double length;      
      double breadth;     
      double height;      
};
int main( )
{
   Box Box1;                
   Box Box2;                
   Box Box3;                
                           double volume = 0.0;     
 
 
   Box1.setLength(6.0); 
   Box1.setBreadth(7.0); 
   Box1.setHeight(5.0);
 
   
   Box2.setLength(12.0); 
   Box2.setBreadth(13.0); 
   Box2.setHeight(10.0);
 
   
   volume = Box1.getVolume();
   cout << "Volume of Box1 : " << volume <<endl;
 
   
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;
 
  
   Box3 = Box1 + Box2;
 
   
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;
 
   return 0;
getch();
}
 
Output
 
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
 
26. Program
 
#include  
#include 
            using namespace std;
            class Shape 
            {
   protected:
      int width, height;
   public:
      Shape( int a=0, int b=0)
      {
         width = a;
         height = b;
      }
      int area()
      {
         cout << "Parent class area :" <<endl;
         return 0;
      }
};
class Rectangle: public Shape{
   public:
      Rectangle( int a=0, int b=0):Shape(a, b) { }
      int area ()
      { 
         cout << "Rectangle class area :" <<endl;
         return (width * height); 
      }
};
class Triangle: public Shape{
   public:
      Triangle( int a=0, int b=0):Shape(a, b) { }
      int area ()
      { 
         cout << "Triangle class area :" <<endl;
         return (width * height / 2); 
      }
};
int main( )
{
   Shape *shape;
   Rectangle rec(10,7);
   Triangle  tri(10,5);
 
   shape = &rec;
   shape->area();
 
   
   shape = &tri;
   shape->area();
   
   return 0;
            getch();
            }
 
 
Output
 
            Parent class area
            Parent class area
 
27. Program
 
#include 
#include 
 
class Adder
{
   public:
      
      Adder(int i = 0)
      {
        total = i;
      }
            void addNum(int number)
      {
          total += number;
      }
            int getTotal()
      {
          return total;
      };
   private:
      
      int total;
};
int main( )
{
   Adder a;
   
   a.addNum(10);
   a.addNum(20);
   a.addNum(30);
 
   cout << "Total " << a.getTotal() <<endl;
   return 0;
getch();
}
 
Output
 
          Total 60
 
28. Program
 
#include 
#include 
class Shape 
{
public:
   
   virtual int getArea() = 0;
   void setWidth(int w)
   {
      width = w;
   }
   void setHeight(int h)
   {
      height = h;
   }
protected:
   int width;
   int height;
};
 
class Rectangle: public Shape
{
public:
   int getArea()
   { 
      return (width * height); 
   }
};
class Triangle: public Shape
{
public:
   int getArea()
   { 
      return (width * height)/2; 
   }
};
 
int main(void)
{
   Rectangle Rect;
   Triangle  Tri;
 
   Rect.setWidth(5);
   Rect.setHeight(7);
   cout << "Total Rectangle area: " << Rect.getArea() << endl;
 
   Tri.setWidth(5);
   Tri.setHeight(7);
   
   cout << "Total Triangle area: " << Tri.getArea() << endl; 
 
   return 0;
getch();
}
 
Output
 
        Total Rectangle area: 35
            Total Triangle area: 17
 
29. Program
 
            #include 
            #include 
            double division(int a, int b)
            {
   if( b == 0 )
   {
      throw "Division by zero condition!";
   }
   return (a/b);
}
 
int main ()
{
   int x = 50;
   int y = 0;
   double z = 0;
 
   try {
     z = division(x, y);
     cout << z << endl;
   }catch (const char* msg) {
     cerr << msg << endl;
   }
 
   return 0;
            getch();
            }
 
Output
 
          Division by zero condition!
 
30. Program
 
#include 
#include 
int main ()
{
   double* pvalue  = NULL;
   pvalue  = new double;   
 
   *pvalue = 29494.99;     
   cout << "Value of pvalue : " << *pvalue << endl;
 
   delete pvalue;         
   return 0;
getch();
}
 
Output
 
               Value of pvalue : 29495
 
31. Program
 
#include 
#include 
 
class Box
{
   public:
      Box() 
{ 
         cout << "Constructor called!" <<endl; 
      }
      ~Box() 
{ 
         cout << "Destructor called!" <<endl; 
      }
};
 
int main( )
{
   Box* myBoxArray = new Box[4];
 
   delete [] myBoxArray; 
 
   return 0;
            getch();
}
 
Output
 
Constructor called!
Constructor called!
Constructor called!
Constructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
 
32. Program
 
#include 
#include 
 
namespace first_space
{
   void func()
{
      cout << "Inside first_space" << endl;
   }
}
namespace second_space
{
   void func()
{
      cout << "Inside second_space" << endl;
   }
}
int main ()
{
 
   first_space::func();
   
   second_space::func(); 
 
   return 0;
getch();
}
 
Output
 
          Inside first_space
            Inside second_space
 
33. Program
 
#include 
#include 
using std::cout;
 
int main ()
{
 
   cout << "std::endl is used with std" << std::endl;
   
   return 0;
   getch();
}
 
Output
 
               std::endl is used with std
 
34. Program
 
#include 
#include 
            namespace first_space
{
   void func()
{
      cout << "Inside first_space" << endl;
   }
}
 
namespace second_space
{
   void func()
{
      cout << "Inside second_space" << endl;
   }
}
using namespace first_space;
int main ()
{
 
             func();
   
   return 0;
            getch();
            }
 
Output
 
                    Inside first_space
 
35. Program
 
#include 
#include 
         namespace first_space
        {
   void func()
{
      cout << "Inside first_space" << endl;
   }
   
   namespace second_space
{
      void func()
{
         cout << "Inside second_space" << endl;
      }
   }
}
using namespace first_space::second_space;
int main ()
{
 
   func();
   
   return 0;
        getch();
        }
 
Output
 
          Inside second_space
 
36. Program
 
#include 
#include 
#include 
           template <typename T>
inline T const& Max (T const& a, T const& b) 
{ 
    return a < b ? b:a; 
} 
int main ()
{
 
    int i = 39;
    int j = 20;
    cout << "Max(i, j): " << Max(i, j) << endl; 
 
    double f1 = 13.5; 
    double f2 = 20.7; 
    cout << "Max(f1, f2): " << Max(f1, f2) << endl; 
 
    string s1 = "Hello"; 
    string s2 = "World"; 
    cout << "Max(s1, s2): " << Max(s1, s2) << endl; 
 
   return 0;
getch();
}
 
Output
 
Max(i, j): 39
Max(f1, f2): 20.7
Max(s1, s2): World
 
 
37. Program
          #include 
            #include
            int sum(int a, int b=20)
            {
                        int result;
 
                        result = a + b;
  
                        return (result);
            }
 
            void main ()
            {
                         int a = 100;
                         int b = 200;
                         int result;
 
                         result = sum(a, b);
                         cout << "Total value is :" << result << endl;
 
  
                        result = sum(a);
                        cout << "Total value is :" << result << endl;
 
                        return 0;
            getch();
            }
 
Output
 
                    Total value is: 300
                        Total value is: 120
 
38. Program
#include
#include
#include
using namespace std;

void signalHandler( int signum )
{
    cout << "Interrupt signal (" << signum << ") received.\n";

    exit(signum); 

}

int main ()
{
    signal(SIGINT, signalHandler); 

    while(1)
{
       cout << "Going to sleep...." << endl;
       sleep(1);
    }

    return 0;
            getch();
           }

Output
Going to sleep....
Going to sleep....
Going to sleep....
 
39. Program
#include 
#include 
#include 
#include 
           #define num_threads     5
            void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   cout << "Hello World! Thread ID, " << tid << endl;
   pthread_exit(NULL);
}
 
int main ()
{
   pthread_t threads[num_threads];
   int rc;
   int i;
   for( i=0; i < num_threads; i++ )
{
      cout << "main() : creating thread, " << i << endl;
      rc = pthread_create(&threads[i], NULL, 
                          PrintHello, (void *)i);
      if (rc)
{
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
       getch();
       }

Output
main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
main() : creating thread, 3
main() : creating thread, 4
Hello World! Thread ID, 0
Hello World! Thread ID, 1
Hello World! Thread ID, 2
Hello World! Thread ID, 3
Hello World! Thread ID, 4


 

No comments:

Post a Comment