28 February, 2013

C++ Program To Find Number Of Odd & Even Numbers.

This Post Contains A C++ Program To Find Number Of Odd & Even Numbers From An Array With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'If...else' & 'For-Loop' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program To Find Number Of Odd & Even Numbers From An Array.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int a[20], Val, Odd_No, Even_No, i;
clrscr();

/* Asking For The Input From User */
cout << " \n Enter Number Of Values : ";
cin >> Val;
cout << " \n Enter " << Val << " Values : ";
for( i=0; i<Val;i ++ )
{
 cin >> a[i];
}

/* Printing The User Entered Values Onto The Screen/Console */
cout << " \n Entered Values Are : ";
for( i=0; i<Val; i++ )
{
 cout << a[i] << " \t ";
}

/* Source Code For Computing No. Of Odd & Even Numbers */
Odd_No = Even_No = 0;
for(i=0; i<Val; i++)
{
 if( a[i] % 2 == 0 )
 {
  Even_No++;
 }
 else
 {
  Odd_No++;
 }
}

/* Printing The Output Onto The Screen/Console */
cout << " \n Total Number Of Odd Numbers : " << Odd_No;
cout << " \n Total Number Of Even Numbers : " << Even_No;

getch();
}
/* End Of Main Program */

Output :

Enter Number Of Values : 5
Enter 5 Values : 1 2 3 4 5
Entered Values Are : 1 2 3 4 5
Number Of Odd Numbers : 3
Number Of Even Numbers : 2

Algorithm :

1. Start.
2. Initialize necessary variables & one integer array.
3. Accept the dimension [Number Of Values] of array, i.e. 'Val' from user.
4. Accept the values for array.
 4.1 for(i=0;i<Val;i++)
 4.2 Accept the value & store it in array, i.e. a[i].
 4.3 End of for loop.
5. Print the values of array.
 5.1 for(i=0;i<Val;i++)
 5.2 Print the value of array, i.e. a[i].
 5.3 End of for loop.
6. Make 'Odd_No' & 'Even_No' variables zero.
7. Find odd or even number from array.
 7.1 for(i=0;i<Val;i++)
 7.2 If value, a[i] is completely divisible by 2, then
 7.2.1 Increment Even_No variable by 1.
 7.3 Else
 7.3.1 Increment Odd_No variable by 1.
 7.4 End of if...else.
 7.5 End of for loop.
8. Print Number of odd numbers, Odd_No.
9. Print Number of even numbers, Even_No.
10. Exit.



25 February, 2013

C Program To Find & Print An Armstrong Number.

This Post Contains A C Program To Find & Print An Armstrong Number With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'While-loop' & 'If...else' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program To Find & Print An Armstrong Number.

/* Declaration Of Header Files */
# include < stdio.h >
# include < conio.h >

/* Start Of Main Program */
void main()
{
   /* Declaration Of Variables */
   int Num = 0, s, r, t;
   clrscr();

   printf ( " \n Enter Any Number : " );
   scanf ( " %d ", &Num );
   t   =   Num;
   s   =   0;
   while ( Num > 0 )
   {
       r = Num % 10;
       s = s + ( r * r * r );
       Num = Num / 10;
   }
   if ( s == t )
   {
      printf ( " \n %d Is An Armstrong Number.", t );
   }
   else
   {
      printf ( " \n %d Is Not An Armstrong Number.", t );
   }
   getch();
}
/* End Of Main Program */

Output :

Enter Any Number : 153

153 Is An Armstrong Number.



23 February, 2013

C Program To Find & Print A Strong Number.

This Post Contains A C Program To Find & Print A Strong Number With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'While-loop', 'If...else' & 'Nested Loops' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program To Find & Print A Strong Number.

/* Declaration Of Header Files */
# include < stdio.h >
# include < conio.h >

/* Start Of Main Program */
void main()
{
   /* Declaration Of Variables */
   int Num, s, r, f, t;
   clrscr();

   /* Asking For The Input From User */
   printf ( " \n Enter Any Number : " );
   scanf ( " %d ", &Num );

   /* Source Code For Computing Strong Number */   
   t  =  Num;
   s  =  0;
   while ( Num   >   0 )
   {
      r  =  Num  %  10;
      f  =  1;
      while (  r  >  0  )
      {
         f  =  f  *  r;
         r--;
      }
      s  =  s  +  f;
      Num  Num  /  10;
   }
   if (  s  ==  t)
   {
      cout << t << " Is A Strong Number.";
   }
   else
   {
      cout << t << " Is Not A Strong Number.";
   }
   getch();
}
/* End Of Main Program */

Output :

Enter Any Number : 145

145 Is A Strong Number.




C Program To Find & Print A Perfect Number.

This Post Contains A C Program To Find & Print A Perfect Number With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'While-loop' & 'If...else' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program To Find A Perfect Number.

# include < stdio.h >
# include < conio.h >

void main()
{
   int Num  =  0,  k,  i;
   clrscr();
   printf ( " \n Enter Any Number : " );
   scanf ( " %d ", &Num);
   i  =  1,  k  =  0;
   while  (  i  <  Num  )
   {
      if  (  Num  %  i  ==  0  )
         k  =  k  +  i;
         i++;
   }
   if ( k  ==  Num )
   {
      printf ( " \n %d Is A Perfect Number. ", Num );
   }
   else
   {
      printf ( " \n %d Is znot A Perfect Number. ", Num );
   }
   getch();
}

Output :

Enter Any Number : 6
6 Is A Perfect Number.

Enter Any Number : 13
13 Is Not A Perfect Number.




Commenting Style In 'C' & 'C++'.

Writing comments for improving the readability of the code is a good programming practice observed by most of the serious & good programmers. In 'C' for writing comments in the code we used /* comment specified here */ syntax. This syntax is also valid in C++ and is very much used for writing multiline comments. However for writing single line comments, C++ prefers its users to use the // single line comment syntax. This is because many a times the multiline comment syntax has proved to be problematic while writing nested comments.

Example :

void swap ()
{
     ...
     ...
    
     if ( a > b )
     /* int temp = a;      /* swap a & b */
        a = b;
        b = temp;
     */

}

In the above code we have two comments. One is the outer block commenting the code for swapping two integers. Another is a single line comment that is nested in the outer block comment.

Here the /* end of the single line nested comment puts a premature end to the comment that is supposed to comment out the outer block. This results is an error since the compiler is unable to find a matching /* for the */ of the outer block comment. Therefore the correct way to avoid this error is to prefer the C++ style of writing single line comments.

Example :

void swap ()
{
     ...
     ...
    
     if ( a > b )
     /* int temp = a;      // swap a & b
        a = b;
        b = temp;
     */

}

Now the above code works fine.

Please Visit The Following Articles Of This Blog Before Proceeding Further.
These Links Will Be Beneficial For You & Will Guide You For Better Understanding Of The 'C' & 'C++' Language.




Good Coding Practices.

Whenever a programmer has to transform a problem statement into a program which a computer can execute, one should split the activity in the following manner :

1] Analysis Phase : Understand the problem clearly. [In real life this will involve study of problem domain.].

2] Design Phase : Think of right algorithm or steps to be followed to achieve your goal. Draw flow charts if required to understand flow of control of a program. Decide the function prototypes in such a way that one can make use of these functions as 7 when required. [In real life this might involve top to bottom approach which leads to deciding the functions / their responsibilities / thier interface, communication with other intelligent controllers if any, etc.].

3] Formulating Of Testing Strategies : Before you start writing actual code, one should always think how am I going to test it? How can I assure that the program specifications are met?.

4] Writing Of Actual Code : Write the code by using good coding practices as described below.

5] Testing Phase : Test the written code as per your strategies. Confirm User Interface.

6] Backup Activity : Keep proper backup of tested code so that if required one should be in a position to reuse already written functions. Use a properly defined directory structure for storing your data.

Follow the above tips thoroughly. A Programmer needs to follow the above steps if one is building up huge software for any application. Some can be made a
habit even through your assgiments, because this is the first step towards climbing up the mountain.

Please Visit The Following Articles Of This Blog Before Proceeding Further.
These Links Will Be Beneficial For You & Will Guide You For Better Understanding Of The 'C' & 'C++' Language.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Why one should follow good coding practices?


In a software life cycle around 80% of time of completion of cycle, the software is in maintenance / upgrade mode. in that case people involved at various stages are different. One might have refer to actual code even after years. Therefore, anybody should be in a position to make the required changes & finish off the activity. This is possible only if code is well documented. Actually writing a right kind of well-documented software is an art with your technical skills. Enough necessary documentation should be made.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

What Are These 'Good Coding Practices'?


1] Use logical names for variables, functions, file.

2] Use uniform notation through out your code. For Ex. Hungarian Notation.

3] Create user defined header file to include commonly required variable declarations, function prototypes.

4] Code should be properly indented.

5] While documenting your code...
  
     A] Write the purpose of your piece of code.

     B] For 'functions' :

          I] What is task assigned to function, What are the arguments passed to it, What does it return.

     C] Write a clear comment if you have added any statements for testing purpose.
  
     D] While writing actual code distribute it in following sections :

           I] Include files, #defined constants, global variable declarations, global function prototypes, main function, local variable declarations, local function prototypes, user inputs, algorithm processing, function calls, user outputs, end of main, function definitions.




20 February, 2013

C Program To Print Pyramid Of Numbers.

This Post Contains A C Program To Print Pyramid Of Numbers With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'For-loop' & 'Nested Loops' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C Program To Print Pyramid Of Numbers.

/* Declaration Of Header Files */

# include < stdio.h >
# include < conio.h >

/* Start Of Main Program */
void main()
{
            
                /* Declaration Of Variables */
                int   i,   j,   k,   t,   r;
                clrscr();

                /* Asking For The Input From User */
                printf ( " \n Enter No. Of Rows : " );
                scanf ( " %d " , &r );

                /* Source Code For Computing Pyramid Of Numbers */
                t  =  1;
                for (  i  = 1;  i  <=  r;  i++  )
                {
                           for ( j=0; j < ( r - i ); j++ )
                          {
                                        printf ( " " );
                          }
                          k = ( i * 2 ) - 1;
                          for (  j  =  0;  j  <  k;  j++  )
                         {
                                        if (  t  >  99  )
                                                printf ( " %d ", t );
                                        else
                                                if (  t  <  10  )
                                                          printf ( " %d ", t );
                                                else
                                                          printf ( " %d ", t );
                                                           t++;
                         }
                         printf ( " \n\n " );
                }
                getch();
}
/* End Of Main Program */ 

Output  :





C++ Program To Display Object Details Using Access Modifiers.

This Post Contains A C++ Program To Display Object Details Using Access Modifiers With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'Access Modifiers' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program To Display Object Details Using Access Modifiers.

# include < iostream.h >
# include < conio.h >

class ABC
{
        private : int Num;
     
        protected : int data;

        public : void getdata()
                    {
                           cout  <<  "  Enter 1st Number  :  ";
                           cin  >>  Num;
                           cout  <<  "  Enter 2nd Number  :  ";
                           cin  >>  data;
                    }
                    void putdata()
                    {
                           cout  <<  "  The 1st Number Entered Is  :  "  <<  Num;
                           cout  <<  "  The 2nd Number Entered Is  :  "  <<  data;
                    }
};


void main()
{
       clrscr();

       ABC obj;
       obj.getdata();
       obj.putdata();

       getch();
}

Output :

Enter 1st Number  :  10

Enter 2nd Number  :  20

The 1st Number Entered Is  :  10

The 2nd Number Entered Is  :  20
      




C Program To Find & Print Twin Prime Numbers.

This Post Contains A C Program To Find & Print Twin Prime Numbers With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'If...else', 'For-loop' & 'While-loop' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple
& Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program To Find & Print Twin Prime Numbers.

/* Declaration Of Header Files */

# include < stdio.h >
# include < conio.h >

/* Start Of Main Program */

void main()
{

   /* Declaration Of Variables */
   int   i,   j,   k,   r,   t,   a[ 10 ] ;
   clrscr();

   /* Asking For The Input From User */
   printf ( "  \n Enter Range   :   " );
   scanf ( " %d ",  &r );

   /* Source Code For Finding & Printing Twin Prime Numbers */
   i    =    1 ;  t    =    0 ;
   while   (   i   <=    r    )
   {
              j  =  1 ;  k  =  0 ;
              while   (   j  <=  i   )
              {
                       if   (   i   %   j   ==   0   )
                                k++ ;
                                j++ ;
              }
               if   (   k   ==   2   )
              {
                        a[ t ]  =  i;
                         t++ ;
              } 
              i++ ;
   }
   for    (   i  =  0;    i   <   t;    i++  )
   {
            if   (   a[ i + 1 ]  -  a[ i ]   ==   2    )
                     printf ( "  \n %d & %d Are Twin Prime Nos. ",  a[i],  a[i+1] );

   }  
   getch();
}
/* End Of Main Program */ 


Output :

Enter Range  :  10

3  &  5  Are  Twin  Prime  Nos.

7  &  9  Are  Twin  Prime  Nos. 




C Program To Find Average Of Odd & Even Numbers Upto A User-Specified Range.

This Post Contains A C Program To Find Average Of Odd & Even Numbers Upto A User-Specified Range With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'While-loop' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple
& Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C Program To Find Average Of Odd & Even Numbers Upto A User-Specified Range.

#  include  < stdio.h >
#  include  < conio.h >

void main()
{
     int  range,  i  =  0,  count  =  0,  sum  =  0;
     clrscr();
     printf ( "  \n  Enter The Range  :  " );
     scanf ( " %d ", &range );

     /*  Printing & Finding Avg Of Even No.  */
     i  =  1,  count  =  0,  sum  =  0;
     while  (  i  <=  range  )
     {
         if  (  i  %  2  ==  0  )
        {
             printf ( "  \n Even Nos. Upto %d Are  :  %d  ",  range,  i );
        }
        i++ ;
     }
     while  (  i  <=  range  )
     {
         if  (  i  %  2  ==  0  )
        {
             sum  =  sum  +  i;
             count++ ;
         }
         i++ ;
      }
      printf ( "  \n Avg Of All Even Nos. Upto %d  :  %d  ",  range,  sum  /  count );
     
      /*  Printing & Finding Avg Of Odd No.  */
     i  =  1,  count  =  0,  sum  =  0;
     while  (  i  <=  range  )
     {
         if  (  i  %  2  !=  0  )
        {
             printf ( "  \n Odd Nos. Upto %d Are  :  %d  ",  range,  i );
        }
        i++ ;
     }
     while  (  i  <=  range  )
     {
         if  (  i  %  2  !=  0  )
        {
             sum  =  sum  +  i;
             count++ ;
         }
         i++ ;
      }
      printf ( "  \n Avg Of All Odd Nos. Upto %d  :  %d  ",  range,  sum  /  count );
     
      getch();
}
/* End Of Main Program */ 

Output :

Enter The Range  :  4

Even Numbers Upto 4 Are  :  2   4
Avg Of All Even Nos. Upto 4  :  3

Odd Numbers Upto 4 Are  :  1   3
Avg Of All Odd Nos. Upto 4  :  2





C Program To Print Even Numbers Upto A User-Specified Range.

This Post Contains A C Program To Print Even Numbers Upto A User-Specified Range With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'While-loop' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program To Print Even Numbers Upto A User-Specified Range.

/* Declaration Of Header Files */
#  include  < stdio.h >
#  include  < conio.h >

/* Start Of Main Program */
void main()
{
          /* Variable Declaration */
          int  range;
          clrscr();
      
          /* Asking For The Input From User */
          printf ( "  \n Enter Range  :  " );
          scanf ( " %d ", &range );

          /* Source Code For Computing Even Numbers Upto A User-Specified Range */   
          int  i  =  1;
          while  (  i  <=  range  )
          {
                    if  (  i  %  2  ==  0  )
                    {
                           printf ( "  %d  \t  ",  i );
                           i++;
                    }
          }
          getch();
}
/* End Of Main Program */

Output  :

Enter Range  :  6

2   4   6  




C Program To Print Odd Numbers Upto A User-Specified Range.

This Post Contains A C Program To Print Odd Numbers Upto A User-Specified Range With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'While-loop' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C Program To Print Odd Numbers Upto A User-Specified Range.

/* Declaration Of Header Files */
#  include  < stdio.h >
#  include  < conio.h >

/* Start Of Main Program */
void main()
{
          /* Declaration Of Variables */
          int  range;
          clrscr();

          /* Asking For The Input From User */
          printf ( "  Enter Range  :  " );
          scanf ( " %d ", &range );

          /* Source Code For Computing Odd Numbers Upto A User-Specified Range */
          int  i  =  1;
          while  (  i  <=  range  )
          {
                    if  (  i  %  2  !=  0  )
                    {
                           printf ( "  %d  \t  ",  i );
                           i++;
                    }
          }
          getch();
}
/* End Of Main Program */ 

Output  :

Enter Range  :  5

1   3   5




19 February, 2013

C Program For Printing A Floyd's Triangle.

This Post Contains A C Program For Printing A Floyd's Triangle With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'For-loop', 'If...else' & 'Nested Loops' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


Floyd's triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner:

1
2   3
4   5   6
7   8   9   10

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program For Printing A Floyd's Triangle.

/* Declaration Of Header Files */
# include < stdio.h >
# include < conio.h >

/* Start Of Main Program */
void main()
{
   /* Declaration Of Variables */
   int a, b, c, rows;
   clrscr();
 
   /* Asking For The Input From User */ 
   printf ( " Enter No. Of Rows To Print : " );
   scanf ( " %d ", &rows );
 
   /* Source Code For Computing Floyd's Triangle */
   b  =  1;
   for ( a = 0; a < rows; a++ )
   {
       for ( c = 0; c <= a; c++ )
       {
           if (  b  <  10  )
              printf ( " %d ", b );
          else
              printf ( " %d ", b );
              b++;
       }
       printf ( " \n  \n " );
   }
   getch();
}
/* End Of Main Program */ 

Output :

Enter No. Of Rows : 4
1
2   3
4   5   6
7   8   9   10




C Program To Print Multiplication Tables.

This Post Contains A C Program To Print Multiplication Tables With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'For-loop' & 'Nested Loops' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.
 


 # Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program To Print Multiplication Tables.

/* Declaration Of Header Files */
# include < stdio.h >
# include < conio.h >

/* Start Of Main Program */
void main()
{
   /* Declaration Of Variables */
   int i,  j,  k = 0;
   clrscr();
 
    /* Source Code For Computing Multiplication Tables */
    for ( i = 1; i <= 10; i++ )
   {
      for ( j = 1; j <= 10; j++ )
     {
            k  =  i  *  j;
            printf ( "  \n  %d  *  %d  =  %d  ",  i,  j,  k );
     }
      printf ( " \n " );
      getch();
   }
}
/* End Of Main Program */




C Program For Printing Fibonacci Series.

This Post Contains A C Program For Printing Fibonacci Series With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'While-loop' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple
& Easy-To-Understand Way.


What Is A Fibonacci Series ?.

The Fibonacci Sequence Is The Series Of Numbers :

0,   1,   1,   2,   3,   5,   8,   13,   21,   34,   .....

The next number is found by adding up the two numbers before it.
  • The 2 is found by adding the two numbers before it (1+1)
  • Similarly, the 3 is found by adding the two numbers before it (1+2),
  • And the 5 is (2+3), & so on.
Following Is The Simple & Easy To Understand Algorithm Of Fibonacci Series, Which Will Help You To Analyse The Source Code Of This Program.

Algorithm :

1.  Assign sum=0,  A=0,  B=1,  i=1.
2.  Get the no. of terms upto which u want to generate the Fibonacci no,  i.e.,  n.
3.  Add A and B to get the next Fibonacci number. 4. Assign the value of B to A i.e. A=B
5.  Assign the value of sum to B  i.e.  B = sum.
6.  Write the value of su to get next Fibonacci number in the series.
7.  Increment  i  with  1  i.e.  i = i + 1 and repeat step 3, 4, 5, 6 with the last value of i = n ( n is the no. of terms which u  want to generate Fibonacci no. series. )
8. Stop.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program For Printing A Fibonacci Series.

/* Declaration Of Header Files */
# include < stdio.h >
# include < conio.h >

/* Start Of Main Program */
void main()
{
  
   /* Declaration Of Variables */ 
   int a, b, c, value;
  
   clrscr();

   /* Accepting Input From User */
   printf ( " Enter Any Value : " );
   scanf ( " %d ", &value );

   /* Source Code For Computing Fibonacci Series */
   a  =  0; 
   b  =  1;
   c  =  0;
   printf ( " %d  \t  %d ", a, b );
   c  =  a  +  b;
   while ( c   <=   value )
   {
      printf ( " %d  \t ", c );
      a  =  b;
      b  =  c;
      c  =  a  +  b;
   }

   getch();

}
/* End Of Program */

Output :

Enter Any Value  :  55

0   1   1   2    3    5     8   13    21     34     55




11 February, 2013

C++ Program For Defining A Class [Class Definition] Including It's Members.


This Post Contains A C++ Program For Defining A Class [Class Definition] Including It's Members With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'Classes' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.



# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



Classes is said to be user-defined data type; which is defined by the user. Users of object-oriented technology usually think of classes as containing the information necessary to create instances, i.e the structure & capabilities of an instances is determined by its corresponding class. Classes serve as 'Templates' for the creation of objects. Class provides entire set of data & related functions to be made user-defined. Thus programmer can build number of classes according to requirements.

'Class' Declaration [ i.e Syntax Of Class Definition ]:~

class class_name
{
       access-specifier :
                                   data members & member functions
        
       access-specifier :
                                   data members & member functions
                                   .....
                                   .....
                                   .....

       access-specifier :
                                   data members & member functions

}object-list;

-----------------------------------------------------------------------------------------------------------------------------                      

C++ Program For Defining A Class [Class Definition] Including It's Members.

# include < iostream.h >
class XYZ
{
       private :  int data;

       public :  void getdata()
                    {
                           cout  <<  "  Enter Number  :  ";
                           cin  >>  data;
                           cout  <<  "  The Number Is  :  "  <<  data;
                     }
};
void main()
{
                XYZ obj;
              
                obj.getdata();
}

Output  :

Enter Number  :  10

The Number Is  :  10

Explanation Of Program  :
In Here we have declared a 'Class' definition having class name XYZ. In this class we have declared one variable as data which is called as data member of the class, while function getdata() is accessing this data member, it is called as member function.
Class definition ends by giving semicolon after closing curly brace. Then we write main() function [which is non-member function], in which you initiate a class by creating a object. Thus a class is loaded in memory, in turn giving memory to its respected members [including data members & member functions]. This Process is known as Instantiation or Object Creation. Then you give call to member function via object.






10 February, 2013

C Program To Print Prime Numbers Upto A User-Specified Range.

This Post Contains A C Program To Print Prime Numbers Upto A User-Specified Range With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'For-loop', 'While-loop' & 'Nested Loops' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program To Print Prime Numbers Upto A User-Specified Range.

/* Declaration Of Header Files */
# include <iostream.h>
# include <conio.h>

/* Start Of Main Program */
void main()
{

   /* Declaration Of Variables */
   int range = 0, k, i, j;
   clrscr();

   /* Asking For The Input From User */
   printf ( " \n Enter Range : " );
   scanf ( " %d ",  &range );
   printf ( " \n Prime Nos. Upto Range %d Are As Follows : \n ", range );

   /* Source Code For Computing Prime Numbers Upto The Given Range */
   for ( i=1; i<=range; i++ )
   {
      j = 1, k = 0;
      while ( j <= i )
      {
          if ( i % j == 0 )
          k++;
          j++;
      }
      if ( k == 2 )
      {
         printf ( " %d  \t ", i );
      }
    }
    getch();
}
/* End Of Main Program */

Output :

Enter Range : 5

Prime Nos. Upto Range 5 Are As Follows :

2        3        5




C Program To Find A Prime Number.

This Post Contains A C Program To Find A Prime Number With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'While-loop' & 'If...else' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C Program To Find A Prime Number.

/* Declaration Of Header Files */
# include <stdio.h>
# include <conio.h>

/* Start Of Main Program */
void main()
{

   /* Declaration Of Variables */
   int Num = 0, k, i;
   clrscr();

   /* Asking For The Input From User */
   printf ( " \n Enter Any Number : " );
   scanf ( " %d ", &Num );

   /* Source Code For Computing Prime Number */
   i = 1, k = 0;
   while ( i <= Num )
   {
      if ( Num % i == 0 )
         k++;
         i++;
   }
   if ( k == 2 )
   {
         printf ( " \n %d Is A Prime Number. " , Num );
   }
   else
   {
     printf ( " \n %d Is Not A Prime Number. " , Num );
   }
   getch();
}
/* End Of Main Program */

Output :

Enter Any Number  :  5

5 Is A Prime Number.

Enter Any Number  :  9

9 Is Not A Prime Number.




C++ Program To Print Prime Numbers Upto A User-Specified Range.

This Post Contains A C++ Program To Print Prime Numbers Upto A User-Specified Range With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'For-loop', 'While-loop' & 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program To Print Prime Numbers Upto A User-Specified Range.

# include <iostream.h>
# include <conio.h>

void main()
{
   int range = 0, k, i, j;
   clrscr();
   cout << " Enter Range : ";
   cin >> range;
   cout << " Prime Nos. Upto Range " << range << " Are As Follows : \n ";
   for ( i=1; i<=range; i++ )
   {
      j = 1, k = 0;
      while ( j <= i )
      {
          if ( i % j == 0 )
          k++;
          j++;
      }
      if ( k == 2 )
      {
         cout << i << "\t";
      }
    }
    getch();
}

Output :

Enter Range : 5

Prime Nos. Upto Range 5 Are As Follows :

2        3        5




05 February, 2013

C Program To Find Odd Or Even Number.

This Post Contains A C Program To Find Odd Or Even Number With Correct Source Code & Output.
This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'If..else' & 'Neasted If...else' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note :  You Can Simply Copy-Paste The Following Code Into Compiler For Direct Result. 



C Program To Find Odd Or Even Number.

# include <stdio.h>
# include <conio.h>

void main()
{
                int  Num  =  0;
                clrscr();
                printf ( "  Enter Any Number :-  " );
                scanf( " %d ", &Num );
                if  (  Num  ==  0  )
                {
                       printf ( "  Enter Number Other Than Zero. " );
                }
                else
                {
                       if  (  Num  %  2  ==  0  )
                       {
                              printf ( "  Is Even Number. " );
                       }
                       else
                       {
                              printf ( "  Is An Odd Number.  " );
                       }
               }
               getch();
}

Output  :

Enter Any Number  :-  0

Enter Number Other Than Zero.

Enter Any Number  :-  1

1  Is An Odd Number.

Enter Any Number  :-  2

2  Is Even Number.




Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.