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





Subscribe To:

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

Blog Archive