Showing posts with label cpp. Show all posts
Showing posts with label cpp. Show all posts

Friday, December 28, 2012

Learn Function...the hardest way

These program help to understand the concept of function.

Program_1.cpp - simple add program
Program_2.cpp - same add program but using 'function'
Program_3.cpp - program to understand why function (this is question)
Program_4.cpp - Program to understand why function (here is answer)
Program_5.cpp - same program via 'Function not returning a value' (same as Program_2.cpp)(invoke_1)
Program_6.cpp - simple add program via 'Function returning a value'
Program_7.cpp - same program via 'Function not returning a value' (same as Program_5.cpp)(invoke_2)
Program_8.cpp - same program via 'Function not returning a value' (same as Program_7.cpp)(invoke_3)
Program_9.cpp - simple add program via call by value 'function returning a value'
Program_10.cpp - simple add program via call by value 'function not returning a value'
Program_11.cpp - simple add program via call by reference 'function returning a value'
Program_12.cpp - simple add program via call by reference 'function not returing a value'

==================================================================
program_1.cpp
// simple program to add 2 number
#include <iostream>
using namespace std;

int main()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

===================================================================
program_2.cpp 
//same add program but using 'function'
#include <iostream>
using namespace std;

void add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    add();          //here we are calling (invoking) the function
}

====================================================================
Program_3.cpp
// program_3.cpp and Program_4.cpp is answer of question why function? 
#include <iostream>
using namespace std;

int main()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
   
    int a1=0;
    int b1=0;
    int c1=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a1 >> b1;
   
    c1 = a1 + b1;
   
    cout << "\n Addition of 2 number is: " << c1 << endl;
   
    int a2=0;
    int b2=0;
    int c2=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a2 >> b2;
   
    c2 = a2 + b2;
   
    cout << "\n Addition of 2 number is: " << c2 << endl;
}

==================================================================
Program_4.cpp
// program_3.cpp and Program_4.cpp is answer of question why function?
#include <iostream>
using namespace std;

void add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    add();
    add();           //here we are calling function 3 time
    add();
}

=========================================================================
 program_5.cpp
// simple program to add 2 number using 'Function not returning a value'
#include <iostream>
using namespace std;

void add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    add();
}
// This program is same as Program_2.cpp (in case you notice that)

====================================================================
Program_6.cpp 
// simple program to add 2 number using 'Function returning a value'
#include <iostream>
using namespace std;

int add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    return (c);
}

int main()
{
    int c;              // Did you notice this
    c = add();         // understand what is going on here
    cout << "\n Addition of 2 number is: " << c << endl;
}

=======================================================================
Program_7.cpp
// This is another way to invoking (calling) a function
// and Program_7.cpp = Program_5.cpp  (please notice the differnce in programs, but both of them doing exact same work.) (but different calling methode)
#include <iostream>
using namespace std;

void add(int a, int b)
{
    int c = 0;
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    add(a, b) ;
}

---------------------------------------------------------------------------------------------
Program_8.cpp
// Yet another ways to calling (invoke) a function
// This Program name is ; by Function not returning a value)
// and Program_8.cpp = Program_7.cpp   (both are not returning a value, but different calling methode)                      
#include <iostream>
using namespace std;

void add(int, int);         //declaration or prototype of function

int main()
{
    int a=0;
    int b=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    add(a, b);
}

void add(int a, int b)         //definition of function
{
    int c = 0;
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

==========================================================
Program_9.cpp
// simple add program via call by value 'function returning a value'
// this is equal to Program_6.cpp

#include <iostream>
using namespace std;

int add(int a, int b)    
{
    int c = 0;
    c = a + b;
  
    return (c);
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    c = add(a, b);   
    cout << "\n Addition of 2 number is: " << c << endl;
}
*/

==========================================================
Program_10.cpp
// simple add program via call by value 'function not-returning a value'
// this is equal to Program_7.cpp

#include <iostream>
using namespace std;

void add(int a, int b)    
{
    int c = 0;
    c = a + b;
  
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    add(a, b);   
}
*/

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

// This is a extra program to understand 'actual' and 'formal' parameter
// which associated with the 'call by value' and 'call by reference'
#include <iostream>
using namespace std;

void add(int a, int b)      //here a, b are formal parameter
{
    int c = 0;
    c = a + b;
  
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    d = a-b+c;          //actual parameter
    cout << " \n substraction is(d): " << d << endl;
  
    add(a, b);     //actual parameter
}


To learn more about 'actual parameter & formal parameter' click here

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

Program_11.cpp
// simple add program via call by reference 'function returning a value'
#include <iostream>
using namespace std;

int add(int &a, int &b)   
{
    int c = 0;
    c = a + b;
  
    return (c);
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    c = add(a, b);   
    cout << "\n Addition of 2 number is: " << c << endl;
}
 

===============================================================
Program_12.cpp
// simple add program via call by reference 'function not-returning a value'
#include <iostream>
using namespace std;

void add(int &a, int &b) 
  
{
    int c = 0;
    c = a + b;
  
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    add(a, b);   
}

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

// why use  'Function returning a value'
#include <iostream>
using namespace std;

int divide()
{
    int a1=0;
    int b1=0;
    int c1=0;
  
    cout << "\n Please Enter 2 number for divide: ";
    cin >> a1 >> b1;
    c1 = a1 / b1;
    return (c1);
}

int add()
{
    int a=0;
    int b=0;
    int c=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
    c = a + b;
    return (c);
}

int main()
{
    int c1;
    c1 = divide();
  

    int c;             
    c = add(); 
  

    int d;
    d = c + c1;
    cout << "\n Addition of 2 number is: " << d << endl;
}

Friday, December 21, 2012

Constructors have to be called before the main function

Answer: yes

#include <stdio.h>
#include <conio.h>
 
/* Apply the constructor attribute to myStartupFun() so that it
    is executed before main() */

void myStartupFun (void) __attribute__ ((constructor));

 
/* Apply the destructor attribute to myCleanupFun() so that it
   is executed after main() */

void myCleanupFun (void) __attribute__ ((destructor));


/* implementation of myStartupFun */
void myStartupFun (void)
{
    printf ("startup code before main()\n");
}

/* implementation of myCleanupFun */
void myCleanupFun (void)
{
    printf ("cleanup code after main()\n");
}

int main (void)
{
    printf ("hello\n");
    return 0;
}



Thursday, December 20, 2012

Program to show static variable not goes in the memory of object

#include <iostream>
using namespace std;

class laptop
{
    int first_int;           //size of int is 4 byte
    int second_int;       // Means size of this class is 8 Byte
};

class car
{
    static int first_int;
            int second_int;
};

int main()
{
    laptop dell;
    car santro;
   
    cout << sizeof(dell) << endl;
    cout << sizeof(santro) << endl;
}


OUTPUT:
8


This Program show static variable not goes in the memory of object static variable is the only belonging of class.

What happen if we have 2 return statement in a function

//Program in cpp using 2 return statement in function
#include <iostream>
using namespace std;

int add ()
{
    return 2;
    return 3;
}

int main()
{
    cout << add();
}
  • If  we compiler this program using GCC compiler, it would not give any error or warning.
  • But when we compile this program in Dotnet it give warning message.
___________________________________________________________________________________
//Program in sql using 2 return statement in function
create function ab()
returns int
as
    begin
        return 100
        return 120
    end
   
print dbo.ab()

This is not give the error because the sql compiler is not enough smart

MORAL of the story: it totally dependent on the compiler, that it gives warning or not, when we use 2 return statement in a function.

Saturday, December 15, 2012

A program for understanding 'Array of pointer'

#include <iostream>
#include <string.h>
using namespace std;

class city
{
    protected:
        int len;
        char*name;
       
    public:
        city()
        {
            len = 0;
            name = new char[len + 1];
        }
       
        void getname()
        {
            char *a;
            a = new char[30];
       
            cout << "\n Enter city name: ";
            cin >> a;
            len = strlen(a);
            name = new char[len + 1];
            strcpy(name, a);
        }
   
        void putname()
        {
            cout << "\n city name: " << name;
        }
};

int main()
{
    city *p[10];
    int n = 1;
    int opt;
   
    do
    {
        p[n] = new city;
        p[n++] -> getname();
        cout << "\n enter 1 for more entry of city name or 0 for exit: ";
        cin >> opt;
    }while(opt);
   
    cout << "\n\n";
    for (int i=1; i<=n; i++)

    {
        p[i] -> putname();

    }
}

Thursday, December 6, 2012

A classic difference between C & C++

In C++ it is strictly enforced that all functions must be declared before they are used. But in C this isn't necessary.

This code is valid C, but it is not valid C++.

#include <stdio.h>
int main()
{
    test();
    return 0;
}

int test()
{
    printf( "Hello C" );
}



     * invalid code *                * Valid code *                  * valid code *

#include <iostream>           #include <iostream>           #include <iostream>
using namespace std;          using namespace std;         using namespace std;
                                                                          
int main()                          int test();                          int test()
{                                                                            {
    test();                           int main()                          printf("hello cpp");
}                                      {                                     }
                                           test();                    
int test()                           }                                     int main()
{                                                                            {
    printf("Hello cpp");           int test()                            test();
}                                      {                                     }
                                           printf("hello CPP");   
                                        }                              

Friday, November 30, 2012

Simple delegate program in c++

#include <iostream>
using namespace std;

int myFunction1(int x)
{
  return x + 10;
}

int myFunction2(int x)
{
  return x * 10;
}

int execFunction(int(*function)(int), int x)
{
  return function(x);
}

int main()
{
  int y = execFunction( myFunction1, 10 );
  cout << y << "\n";


  y = execFunction( myFunction2, 10 );
  cout << y << "\n";
  return 0;
}


OUTPUT: 

Wednesday, November 28, 2012

How classes are implement in real software

Classes are often constructed with two files, and used with a third
 1) Header (.h)
          Code file that contains the class template
 2) Implementation (.cpp)
          Code file that contains the class implementation


     Defines the actions of the methods, etc…
 3) Driver (.cpp)
         Code file that uses these two files to create an instance of the object

___________________________________________________________________

// 1) Header (.h)
//Filename:name.h
class name
{
  private:
    char *_firstName;
    char *_lastName;
  public:
    char *getFirstName();
    char *getLastName();
   
    void setFirstName(char*);
    void setLastName(char*);
   
};

____________________________________________________________________________________

// 2) Implementation (.cpp)
//Filename: name.cpp
#include "name.h"

void name :: setFirstName(char *firstName)
{
  _firstName = firstName;
}

void name :: setLastName(char *lastName)
{
  _lastName = lastName;
}

char* name :: getFirstName()
{
  return _firstName;
}

char* name :: getLastName()
{
  return _lastName;
}

___________________________________________________________________________________

//  3) Driver (.cpp)
//Filename: NameDriver.cpp
#include <iostream>
#include "name.cpp"
using namespace std;

int main()
{
  name myName;
  myName.setFirstName("Honey");
  myName.setLastName("singh");
 
  cout << "Hello "
     << myName.getFirstName() << " "
     << myName.getLastName() << endl;
 
  return 0;
}

___________________________________________________________________
OUTPUT:
Hello Honey singh

Monday, November 19, 2012

Simple Thread program in c++

//Filename: SimpleThreadTest.cpp
#include <iostream>
#include <windows.h>
#include <process.h>
#include <time.h>

using namespace std;

void myThreadFunction(void *p)
{
  int n = (INT_PTR)p;
  Sleep(n * 1000);
  cout << "Ending Thread At: " << time(NULL) << endl;
}

int main(int argc, const char* argv[])
{
  cout << "Start Time: " << time(NULL) << endl;


  HANDLE myThread = (HANDLE)_beginthread(myThreadFunction, 0, (void*)5);


  WaitForSingleObject(myThread, 100000);
  return 0;
}


______________________________________________________________________________________

//Filename: SimpleMultiThreadTest.cpp

#include <iostream>
#include <windows.h>
#include <process.h>
#include <time.h>

using namespace std;

void myThreadFunction(void *p)
{
  int n = (INT_PTR)p;
  Sleep(n * 1000);
  cout << "Ending Thread At: " << time(NULL) << endl;
}

int main(int argc, const char* argv[])
{
  srand(time(NULL));
  const int threadSize = 5;
  HANDLE myThreads[threadSize];
  for(int i = 0; i < threadSize; i++)
      myThreads[i] = (HANDLE)_beginthread(myThreadFunction, 0, (void*)(rand() % 10 + 1));

  for(int i = 0; i < threadSize; i++)
      WaitForSingleObject(myThreads[i], 100000);
  return 0;
}

Friday, November 9, 2012

program to understand actual parameter and formal parameter

  • Formal and actual parameter are associated with the concept of 'call by value' and 'call by reference'
  • see the effect of 'call by value' and 'call by reference'
====================================================
//this program to illustrate call_by_reference
#include <iostream>
using namespace std;

int add(int &);

int main()
{
    int a=2;
    int square;
    square = add(a);
    cout << "\n" << a;
    cout << "\n" << square;
}

int add(int &a)
{
    a=a+1;
    return(a*a);
}


OUTPUT:
3
9


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

// this program to illustrate call_by_value
#include <iostream>
using namespace std;

int add(int);

int main()
{
    int a=2;
    int square;
    square = add(a);
    cout << "\n" << a;
    cout << "\n" << square;
}

int add(int a)
{
    a=a+1;
    return(a*a);
}


OUTPUT:
2
9 

======================================================
// This is a extra program to understand 'actual' and 'formal' parameter
// which associated with the 'call by value' and 'call by reference'

#include <iostream>
using namespace std;

void add(int a, int b)      //here a, b are formal parameter
{
    int c = 0;
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    d = a-b+c;          //actual parameter
    cout << " \n substraction is(d): " << d << endl;
   
    add(a, b);     //actual parameter
}

Wednesday, October 31, 2012

What is Template

Template helps us to create the generic functions and classes. In the generic function or class, the type of data upon which the function or class operates and specified as a parameters.

//Function Template
#include <iostream>
using namespace std;

template <class T>
T GetMax (T a, T b)
{
    T result;
    result = (a>b)?a:b ;
    return (result);
}


int main()
{
    int i = 5, j = 6, k;
    long l = 10, m = 5, n;

    k = GetMax<int>(i,j);
    n = GetMax<long>(l,m);

   
    cout << k << endl;
    cout << n << endl;
}

OUTOUT:
6
10 

int x, y;
GetMax <int> (x,y);
when the compiler encounters this call to a template function, it uses the template to automatically generate a function replacing each appereance of mytype by the type passed as the actual template parameter (int in this case) and then calls it. this process is automatically performed by the compiler and is invisible to the programmer.
______________________________________________________________________

// function Template 2
#include <iostream>
using namespace std;

template <class T>
T GetMax(T a, T b)
{
    T result;
    result = (a>b)?a:b ;
    return (result);
}

int main()
{
    int i = 5, j = 6, k;
    long l = 10, m = 5, n;
    k = GetMax(i,j);
    n = GetMax(l,m);
         //Notice here we don't declare the type
   
    cout << k << endl;
    cout << n << endl;
}

OUTOUT:
6
10

Notice how in this case, we called our function template Getmax() without explicitly specifying the type between angle-brackets <>. The compiler automatically determine what type is needed on each call.  

Sunday, October 21, 2012

Setter & Getter in c++

  1. A property, is a special sort of class member; or intermediate between a field (or data member) and a method.
  2. Properties are read and written like fields, but property reads and writes are (usually) translated to get and set method calls.
  3. Property allows us data validation.

That is, properties are intermediate between member code (methods) and member data  (instance variables) of the class, and properties provide a higher level of encapsulation than public fields.

//property in c++

#include <iostream>
using namespace std;

class student
{
    public:
        void setName(string x)
        {
            name = x;
        }
        string getName()
        {
            return name;
        }
       
    private:
        string name;
};

int main()
{
    student bio_stud;


    bio_stud.setName("sir Virender");
    cout << bio_stud.getName();   
}


OUTPUT:
sir Virender


NOTE:
  1. C++ does not have first class properties, but there exist several ways to emulate properties to a limited degree.
  2. C++ require the programmer to define a pair of accessor and mutator methods.  
Further reading property and indexer in cpp

Wednesday, October 17, 2012

Program to understand STATIC variable

Static is multipurpose keyword, we can use static in context of variable, function and classes.
Here we discuss about the static variable.
__________________________________________________________________
//In this program we are not using static variable 
 #include<stdio.h>
#include<conio.h>

void show()
{
    int i=0;          //Note, we are not declaring 'i' as a static variable here
    i++;               //variable 'i' increased by 1


    printf("output is %d\n" ,i);
}

void main()
{
    show();            //here we are calling show 3 times
    show();
    show();
}


output:
       output is 1
       output is 1

       output is 1

You can see that  output is 1, although we are calling show function 3 time.
That because the variable i not retaining the value.

  •  So to retain the value we use this variable as a static variable. (see next program)
  •  Static variable is useful when we want to count the no of attempt performed by the client. (see last program)
__________________________________________________________________
//using STATIC variable
#include<stdio.h>
#include<conio.h>

int show()
{
    static int i=0;            //here we use static variable
             i++;
             printf("output is %d\n" ,i);
}

void main()
{
    show();
    show();
    show();
}

output:
          output is 1
          output is 2
          output is 3

__________________________________________________________________
//In this program we want that user have only 4 chance to enter right password
#include<iostream>
#include<string>
#include<stdlib.h>              //for exit(0);
using namespace std;

int show()
{
     static int i=0;            //here we use static variable
           // int i=0;


    
string password;           
     i++;
                      
            if(i<=3)
            {
                cout << "\nenter your password: ";
                cin >> password;
               
                if (password == "foobar")
                    {
                        cout << "\nwelcome you log in!\n";
                        exit(0);
                    }
            }
  
            else
            {
                cout << "recover your password";
            }
           
}

int main()
{
    show();              //here we can use the loop also
    show();
    show();
    show();
}

Tuesday, October 16, 2012

Difference and making of namespace in c++ and c#

//How to write a program for namespace in visual c++

#include"StdAfx.h"
#include<iostream>
using namespace std;

namespace uptu
{
    class abes
    {
        private:
            int x;
           
        public:
          void assign() {
            x=100;
          }
         
          void show() {
            cout<<x; 
          }
    };
   
    class akeg
    {
        private:
            int x;
           
        public:
            void assign() {
                x=121;
            }
           
            void show() {
                cout<<x;
            }
    };
}

int  main()

    {
        uptu::abes a;
        a.assign();
       
        uptu::akeg b;
        b.assign();
        b.show();

        return 0;
    }
___________________________________________________________________
 // namespace in csharp
// must be added at beginning


using System;
using y=india.uptu;

namespace india
{
    namespace uptu
    {
        public class abes
        {
            public void show()
            {
                Console.WriteLine("This is alias");
            }
 
            public static void Main()
            {
                y.abes a=new y.abes();
                a.show();
            }
        }
    }
}

Friday, September 14, 2012

What is library and List of c++ libraries

What is a library:
  • A library is a collection of implementations of behavior, written in terms of a language, that has a well-defined interface  by which the behavior is invoked.
  • Behavior is provided for reuse by multiple independent programs.
  • A program invokes the library-provided behavior via a mechanism of the language.
 
What is the advantage of library:
  • A library provide the reuse of the behavior. When a program invokes a library, it gains the behavior implemented inside that library without having to implement that behavior itself. 
  • Libraries encourage the sharing of code in a modular fashion, and  ease the distribution of the code. 

There are two type of library:
 1. Static library

  • If the code of the library is accessed during the build of the invoking program, then the library is call a static library.
 2. Dynamic library
  • The library behavior is connected after the executable has been invoked to be executed, either as part of the process of starting the execution, or in the middle of execution. In this case the library is called a dynamic library. 
  • A dynamic library can be loaded and linked as part of preparing a program for execution, by the linker.
                  
What is linker:
A computer program that takes one or more objects generated by a compiler and links them to standard library function and hence making them executable program.
   The linking process is usually automatically done by a linker or binder program that searches a set of libraries and other modules in a given order. Usually it is not considered an error if a link target can be found multiple times in a given set of libraries. Linking  may be done when an executable file is created, or whenever the program is used at run time.


For more detail on What is linker click here


      Free portable C++ libraries that complement the C++ Standard Library. Many Boost libraries may become part of the next C++ Standard.


             A comprehensible list of open source C++ libraries. So that you doesn't need to waste time searching on Google. 

Tuesday, September 11, 2012

How to make abstract class in c++

C++ has no abstract keyword or equivalent, to make a class abstract is to declare a pure virtual function in it (which prevents instantiation).

For this same reason, in C++ there is not much difference between an  interface and an abstract class - a C++ interface is simply a class containing only pure virtual functions.

C++ has no keyword abstract. However, you can write pure virtual functions; that's the C++ way of expressing abstract classes.

What is a Pure Virtual Function:
A Pure Virtual Function is a Virtual function with no body.

Declaration  of Pure Virtual Function:
Since pure virtual function has no body, the programmer must add the notation =0 for declaration of the pure virtual function in the base class.

Syntax of Pure Virtual Function:
class class_name //This denotes the base class of C++ virtual function
{
    public:
    virtual void virtualfunctioname() = 0 //This denotes the pure virtual function in C++
};

___________________________________________________________________
//Full code example
#include <iostream>
using namespace std;
class Sandeep
{
public:
        virtual void example()=0; //Denotes pure virtual Function Definition
};

class Demo1:public Sandeep
{
public:
        void example()
        {
                cout << " \n Welcome";
        }

};

class Demo2:public Sandeep
{
public:
        void example()
        {
                cout << " To My blog \n";
        }

};

int main()
{
        Sandeep* arra[2];
        Demo1 d1;
        Demo2 d2;
        arra[0]=&d1;
        arra[1]=&d2;
        arra[0]->example();
        arra[1]->example();
}


OUTPUT:

Thursday, September 6, 2012

Program to check ARMSTRONG number in c++

// program to check the armstrong number

#include <iostream>
using namespace std;

int main()
{
   int num, sum = 0, temp, remainder;

   cout << "Enter a number to check: " << endl;     
   cin >> num;

   temp = num;

   while( temp != 0 )
   {
      remainder = temp%10;
      sum = sum + remainder*remainder*remainder;
      temp = temp/10;

   }

   if ( num == sum )
      cout << "Your Number is an armstrong number." << endl;
   else
      cout << "Your Number is not an armstrong number." << endl;      
}

___________________________________________________________________________________

//program to generate armstrong number

#include <iostream>
using namespace std;

int main()
{
   int r;
   long long number = 0, c, sum = 0, temp;

   cout << "Enter the maximum number upto which you want to find armstrong numbers ";
   cin >> number;

   cout << "Following armstrong numbers are found from 1 to " << number << endl;

   for( c = 1 ; c <= number ; c++ )
   {
      temp = c;
      while( temp != 0 )
      {
         r = temp%10;
         sum = sum + r*r*r;
         temp = temp/10;
      }


      if ( c == sum )
         cout << c << endl;
      sum = 0;
   }
}