20 February, 2013

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.

Blog Archive