Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Basic C++ program

Example 1 - C++ hello world program

/* A very simple c++ program printing a string on screen*/
#include <iostream.h>
 
main()
{
    cout<<"Hello World\n";
    return 0;
}
Output of above program:
"Hello World"
Example 2 - c++ program to take input from user using cin
#include <stdio.h>
 
main()
{
   int number;
 
   cout<<"Enter an integer\n";
   cin>>number;
 
   cout<<"Integer entered by you is\n"<< number;
 
   return 0;
}
Output:
Enter a number
5
Number entered by you is 5

Example 3 - using if else control instructions
#include <stdio.h>
 
main()
{
   int x = 1;
 
   if ( x == 1 )
      cout<<"x is equal to one.\n";
   else
      cout<<"For comparison use == as = is the assignment operator.\n";
 
   return 0;
}
Output:
x is equal to one.

Example 4 - loop example
#include <iostream.h>
 
main()
{
   int value = 1;
 
   while(value<=3)
   {
      cout<<"Value is \n", value);
      value++;
   }
 
   return 0;
}
Output:
Value is 1
Value is 2
Value is 3

Example 5 - c++ program for prime number
#include <iostream.h>
 
main()
{
   int n, c;
 
   cout<<"Enter a number\n";
   cin>>n;
 
   if ( n == 2 )
      cout<<"Prime number.\n";
   else
   {
       for ( c = 2 ; c <= n - 1 ; c++ )
       {
           if ( n % c == 0 )
              break;
       }
       if ( c != n )
           cout<<Not prime.\n";
       else
          cout<<"Prime number.\n";
   }
   return 0;
}

Example 7 - Array program
#include <iostram.h>
 
main() 
{
    int array[100], n, c;
 
    cout<<"Enter the number of elements in array\n";
    cin>>n;
 
    cout<<"Enter  elements\n";
 
    for ( c = 0 ; c < n ; c++ ) 
        cin>>array[c];
 
    cout<<"Array elements entered by you are:\n";
 
    for ( c = 0 ; c < n ; c++ ) 
        cout<<array[c];
 
    return 0;
}

Example 8 - function program
#include <iostream.h>
 
void my_function();
 
main()
{
   cout<<"Main function.\n";
 
   my_function();
 
   cout<<"Back in function main.\n";
 
   return 0;
}
 
void my_function()
{
   cout<<"Welcome to my function. Feel at home.\n";
}

Example 9 - Using comments in a program
#include <iostream.h>
 
main()
{
   // Single line comment in c source code
 
   cout<<"Writing comments is very useful.\n";
 
   /*
    * Multi line comment syntax
    * Comments help us to understand code later easily.
    * Will you write comments while developing programs ?
    */
 
   cout<<"Good luck c++ programmer.\n"; 
 
   return 0;
}

Example 10 - using structures in c++ programming
#include <iostream.h.h>
 
struct programming
{
    float constant;
    char *pointer;
};
 
main()
{
   struct programming variable;
   char string[] = "Programming in Software Development.";   
 
   variable.constant = 1.23;
   variable.pointer = string;
 
   cout<<variable.constant;
   cout<<variable.pointer;
 
   return 0;
}

Example 11 - c++ program for Fibonacci series
#include <iostream.h>
 
main()
{
   int n, first = 0, second = 1, next, c;
 
   cout<<"Enter the number of terms\n";
   cin>>n;
 
   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      cout<<next;
   }
 
   return 0;
}

Example 12 - c++ graphics programming
#include <graphics.h>
#include <iostream.h>
 
main()
{
    int gd = DETECT, gm;
 
    initgraph(&gd, &gm,"C:\\TC\\BGI");
 
    outtextxy(10,20, "Graphics source code example.");
 
    circle(200, 200, 50);
 
    setcolor(BLUE);
 
    line(350, 250, 450, 50);
 
    getch();
    closegraph( );
    return 0;
}

3 comments:

Blogger Widgets

Find Us On Google, Just type - way2cplusplus -

If you have any questions on implementing or understanding this C++ Program , shoot me a comment and I'll be glad to help! :) :)