Theme images by Storman. Powered by Blogger.

Friday 26 August 2016

Program for to check Armstrong number or not.

- No comments

Armstrong number

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

 Program for to check Armstrong number or not.

int main()
{
    int number, originalNumber, remainder, result = 0;

    printf("Enter a three digit integer: ");
    scanf("%d", &number);

    originalNumber = number;

    while (originalNumber != 0)
    {
        remainder = originalNumber%10;
        result += remainder*remainder*remainder;
        originalNumber /= 10;
    }

    if(result == number)
        printf("%d is an Armstrong number.",number);
    else
        printf("%d is not an Armstrong number.",number);

    return 0;
}

Output:

Enter an integer: 1634
1634 is an Armstrong number.

Another Example

Program for to check Armstrong number or not

 

#include <stdio.h>
#include <math.h>

int main()
{
    int number, originalNumber, remainder, result = 0, n = 0 ;

    printf("Enter an integer: ");
    scanf("%d", &number);

     originalNumber = number;
   
    while (originalNumber != 0)
    {
        originalNumber /= 10;
        ++n;
    }

    originalNumber = number;

    while (originalNumber != 0)
    {
        remainder = originalNumber%10;
        result += pow(remainder, n);
        originalNumber /= 10;
    }

    if(result == number)
        printf("%d is an Armstrong number.", number);
    else
        printf("%d is not an Armstrong number.", number);

    return 0;
}


 

C++ Identifiers

- No comments
C++has strict rules for variable names. A variable name is one example of an identifier.  An identifier is a word used to name things. One of the things an identifier can name is a variable.We will see in later chapters that identifiers name other things such as functions and classes.  Identifiers have the following form:

  • Identifiers must contain at least one character.
  •  The first character must be an alphabetic letter (upper or lower case) or the underscore
               ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_ 
  • The remaining characters (if any) may be alphabetic characters (upper or lower case), the underscore,or a digit
                                    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789
  •  No other characters (including spaces) are permitted in identifiers 
  • A reserved word cannot be used as an identifier

Here are some examples of valid and invalid identifiers:

  • All of the following words are valid identifiers and so qualify as variable names:
    x,x2,total,port_22, and FLAG
C++ Identifiers

This table .C++ reserved words. C++reserves these words for specific purposes in program construction. None of the words in this list may be used as an identifier; thus, you may not use any of these words to name a variable.
 

Saturday 20 August 2016

Compile & Execute C++ Program

- No comments

 Let's look at how to save the file, compile and run the program. 

Please follow the steps given below:

  • Open a text editor and add the code as above.
  • Save the file as: hello.cpp
  • Open  a  command  prompt  and  go  to  the  directory  where  you  saved  the file.

  • Type 'g++ hello.cpp' and press enter to compile your code. If there are no errors  in  your  code  the  command  prompt  will  take  you  to  the  next  line and would generate a.out executable file.

  • Now, type 'a.out' to run your program.
  • You will be able to see ' Hello World ' printed on the window
       $ g++hello.cpp
       $ ./a.out
       HelloWorld
  • Make sure that g++ is in your path and that you are running it in the directory containing file hello.cpp

C++ Program Structure

- No comments

Let us look at a simple code that would print the words Hello World


#include<iostream>
usingname space std;
// main() is where program execution begins.
int
main()
{
       cout <<"Hello World";
       // prints Hello World

       return 0;
}


Explanation:- 

  • The  C++  language  defines  several  headers,  which  contain  information that is either necessary or useful to your program. For this program, the header<iostream>is needed
  • The   lineusing   namespace   std;tells   the   compiler   touse   the   std namespace. Namespaces are a relatively recent addition to C++
  • The  next  line ‘//  main()  is  where  program  execution  begins.’is  a single-line comment available in C++. Single-line comments begin with // and stop at the end of the line.


  • The line int main()is the main function where program execution begins.
  • The  next  linecout  <<  "This  is  my  first  C++  program.";causes  the message "This is my first C++ program" to be displayed on the screen.
  • The next linereturn 0;terminates main()function and causes it to return the value 0 to the calling process

Wednesday 17 August 2016

Uses of C++

- No comments

uses of c++

Use of C++:

  • C++  is  used  by  hundreds  of  thousands  of  programmers  in  essentially  every application domain.
  • C++ is being highly used to write device drivers and other softwarethat rely on direct manipulation of hardware under realtime constraints.
  • C++  is  widely  used  for  teaching  and  research  because  it  is  clean  enough  for successful teaching of basic concepts.
  • Anyone  who  has  used  either  an  Apple  Macintosh  or  a  PC  running  Windows  has indirectly  used  C++  because  the  primary  user  interfaces  of  these  systems  are written in C++ 
  • The most important thing while learning C++ is to focus on concepts.
  • The  purpose  of  learning  a  programming  language  is  to  become  a  better programmer;  that  is,  to  become  more  effective  at  designing  and  implementing new systems and at maintaining old ones.
  • C++  supports  a  variety  of  programming  styles.  You  can  write  in  the  style  of Fortran,  C,  Smalltalk,  etc.,  in  any  language.  Each  style  can  achieve  its  aims effectively while maintaining runtime and space efficiency.

Standard Libraries

- No comments

Standard Libraries C++ consists of three important parts:


  • The  core  language  giving  all  the  building  blocks  including  variables,  data types and literals, etc.
  • The C++ Standard Library giving a rich set of functions manipulating files, strings, etc.
  • The  Standard  Template  Library  (STL)  giving  a  rich  set  of  methods manipulating data structures, etc.

The ANSI Standard Libraries:

  • The ANSI standard is an attempt to ensure that C++ is portable;that code you write for Microsoft's compiler will compile without errors, using a compiler on a Mac, UNIX, a Windows box, or an Alpha.
  • The ANSI standard has been stable for a while, and all the major C++ compiler manufacturers support the ANSI standard

Saturday 13 August 2016

Object Oriented Programming

- 1 comment



  • C++  is  a  statically  typed,  compiled,  general-purpose,  case-sensitive,  free-form programming  language  that  supports  procedural,  object-oriented,  and  generic programming.
  • C++  is  regarded as  amiddle-levellanguage, as  it comprises  a combination of both high-level and low-level language features.
  • C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill, New Jersey, as an enhancement to the C language and originally named C with Classes but later it was renamed C++ in 1983.
  • C++  is  a  superset  of  C,  and  that  virtually  any  legal  C  program  is  a  legal  C++ program.
  • A programming language is said to use static typing when type checking is performed during compiletime as opposed to runtime.
      Object-Oriented Programming:


       oriented development:
  • Encapsulation
  • Data hiding
  • Inheritance
  • Polymorphism