C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Saturday, December 28, 2013

Program Find LCM of Two Numbers

Write a C++ Program to input two integer numbers
and Find LCM.

First of all, here are some basic concepts about LCM and the program logic explaining which statements will be used to solve this problem in C++

Definition of LCM of Two Numbers

The Least Common Multiple of two integers a and b is the smallest positive integer that is divisible by both a and b.
LCM-two-numbers-Program-c

Explanation of LCM

A multiple of a number is the product of that number and an integer. For example, 10 is a multiple of 5 because 5 × 2 = 10.

Example: Find the least common multiple for 3 and 5:


The multiples of 3 are 3, 6, 9, 12, 15, and so on
and the multiples of 5 are 5, 10, 15, 20, and so on
Note that the first common multiple is 15. Therefore, LCM of 3 and 5 is 15 because it is the least common multiple.
Program-find-LCM-two-numbers

Example: Find the Least Common Multiple of 4 and 6?


We write down Multiples of 4 :

    4, 8, 12, 16, 20, 24, 28, 32, 36, 40, ...

and we write down the multiples of 6 :

    6, 12, 18, 24, 30, 36, 42, 48, 54, 60, ...

Here we write down the Common multiples of 4 and 6::

    12, 24, 36, ....

So, it is clear that 12  is the required least common multiple of 4 and 6.

Definition of LCM of Multiple Numbers

The LCM of more than two integers is the smallest integer that is divisible by each of them.


Program Logic For Finding LCM of Two Numbers

 
Easyway C++ Program LCM Logic and Source Code
Easyway C++ Program LCM Logic

  1. First of all we will input two numbers say a and b, with the help of  cout and cin.
  2. Now we check if any one number is 1 then LCM is 1.
  3. If both numbers are greater than 1 then we initialize a loop counter by 2.
  4. Now we start a for loop from 2 to onward until we find a number that is divisible by both numbers a and b. Note that we have no condition expression in for loop. Only initialization and increment expression (increment loop counter by 1) is used in for loop. So, this is an infinite loop. [Note that such loops may be terminated by a break statement upon satisfying some condition.]
  5. In Loop body, we check that if current number may divide a and b both? If so, then this current number is the LCM of a and b. And after displaying the LCM we will use a break statement to terminate the loop.

Program Code: Find LCM of Two Numbers

/*
Write a C++ Program to input two integer numbers
and Find LCM.

For Free C++ Programming Techniques
(Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

void main()
{

  clrscr();

  int a, b, counter;

  cout<<"\n Enter the first number = ";

  cin>>a;

  cout<<"\n Enter the second number = ";

  cin>>b;

  if ( a == 1 || b == 1)
      {
      cout<<"\n LCM of "<<a<<" and "<<b<<" is 1";
      exit(0);
      }

  for(counter = 2; ;counter++ )

    if ( counter % a == 0 && counter % b == 0 )

         {
         cout<<"\n LCM of "<<a<<" and "<<b<<" is "<<counter;
         break;
         }

  getch();

}
Share:

Sunday, December 22, 2013

Program Find Prime or Composite Number

Today, we will discuss a C++ Program Find Prime or Composite Number. That is to write a program to Input a number and check whether it is prime number or composite number.

What is a Prime Number? 

A prime number is a whole number greater than 1 that only has two factors or divisors) which are itself and one. For example, 2 is a prime number (actually first prime number), because it has only two divisors, that is, 1 and 2. Similarly, 5 is divisible by 1 and 5 (only two divisors), so 5 is also a prime number.While 6 has divisors 1, 2 ,3 and 6 so 6 is not prime, but 6 is composite number.

C++ Program To Find Prime or COMPOSITE Number

What is a Composite Number? 

A composite number has more factors (or divisors)in addition to one and itself. For example, 8 has positive divisors : 1, 2, 4 and 8. So 8 is composite. 

Note:- The numbers 0 and 1 are neither prime nor composite.

Write a C++ Program to input a positive number, then check whether this number is
a prime number or composite number?


Program Logic For Program Find Prime or Composite Number

  1. First of all we will display a message to enter a number and get a number into a variable called "number".
  2. We will check by IF statement that if enetered number is 0 or 1 then display a message that 0 or 1 are neither prime nor composite. 
  3. Now, we know that a number is always divided by 1 and itself. Also we know that a number can be divided by the numbers between 2 and its mid number, in addition to 1 and itself. For example, Let the number is 8. Now 8 can be divided by numbers 1, 2, 4 and 8. So we will check for divisors 2 to 4(mid of 8).  (Note: We ignore 1 and 8 as it is obviously a divisors of  8). Let the number is 13. Now we should check divisors from 2 to 6 (integer mid of 13).
  4. So we calculate mid = number / 2. 
  5. Let the number is 6. Then mid = 6/2 which gives 3.
  6. Now we will loop to check any divisor present from 2 to(mid) 3. If any number between 2 and 3 divides 6 evenly then we will set a boolean variable Primeflag to 0. Which means 6 is not a prime. Note that we have already initialized the boolean variable to 1. So if there is no divisor between 2 and mid number then Primeflag will never be set to 0.
  7. We will break the loop by using break; statemnt.
  8. Now we check the boolean variable Primeflag. If it is 1 then display Prime message else display a Composite Number message.

 Actual Source Code for Program Find Prime or Composite Number

/*
Write a C++ Program to input a positive
number, then check whether this number is
a prime number or composite number?

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>  // for cout / cin etc

#include<conio.h>   // for clrscr() etc

#include<stdlib.h> // for exit() etc

int main()
{

  clrscr();

  long number, counter, mid;
  bool Primeflag =1;
  cout<<"\nEnter a  number greater than 1 to check \nfor Prime or Composite: ";

  cin>>number;

  if ( number == 0 || number ==1 )
      {
      cout<<"\n 0 and 1 are niether prime nor composite\n";
      cout<<"\n Press any key to continue ... Thanks!";
      getch();
      exit(0);
      }

  mid = number / 2;

  for (counter = 2; counter <= mid; counter++)
        if( number % counter == 0 )
           {
           Primeflag =0;
           break;
           }
  if ( Primeflag == 1)
      cout<<"\n The number "<<number<<" is a Prime Number";
  else
      cout<<"\n The number "<<number<<" is a Composite Number";

  cout<<"\n Press any key to continue ... Thanks!";
  getch();
  return 0;
}

Share:

Saturday, December 21, 2013

Program Find GCD or HCF of Two Whole Numbers

Before writing the program GCD in C Plus Plus, let us understand the logic of the GCD program. 

Program to Find GCD of two integer numbers in See Plus Plus C++
Program to Find GCD of two integer numbers in See Plus Plus C++

What is GCD?

GCD stands for  Greatest Common Divisor. GCD of two or more integer numbers is the largest positive integer that divides the given numbers without a remainder. For example,
  • GCD of 8 and 12 is 4
  • GCD of 24 and 36 is 12
  • GCD of 7 and 1 is 1
  • GCD of 50 and 35 is 5
Note: GCD is also known as GCF - Greatest Common Factor or HCF - Highest Common Factor or HGD - Highest Common Divisor.


One Method of Calculating GCD

Question: What is the GCD of (24, 56). 
Factors of 24 are 2 * 2 * 2 * 3,
Factors of 56 are 2 * 2 * 2 * 7.
The Common factors are 2 * 2 * 2 = 8.
Therefore the GCD of (24, 56) is 8.

Question: What is the GCD of (18, 27). 
Factors of 18 are 2 * 3 * 3
Factors of 27 are 3 * 3 * 3
The Common factors are 3 * 3  = 9
Therefore the GCD of (18, 27) is 9

 

Program Logic: GCD of Two Numbers a and b.

  • We will divide the greater number by the smaller number. If it is divided with no remainder then smaller number is GCD. For Example let a = 18 and b = 6. We will divide 18 by 6. It is divided with no remainder. So GCD is 6.
  •  Let a  = 18 and b = 27. Now we divide 27 by 18. It is not divided evenly. So we will divide 27 by 17, 16, 15, 14 and so on to 1. When we will divide 27 by 9, it will be divided evenly. So 9 is the GCD.

 Write a C++ Program to input two integer numbers
and Find the GCD.

 /*
Write a C++ Program to input two integer numbers
and Find the GCD.

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int a, b, counter;

  cout<<"\n Enter the first number = ";

  cin>>a;

  cout<<"\n Enter the second number = ";

  cin>>b;

  if ( a < b )
     counter = a;
     else
     counter = b;

  for(; counter>=1; counter -- )

    if ( a % counter == 0 && b % counter == 0 )
         break;

  cout<<"\n\n -------- Results --------\n";

  cout<<"\n GCD of "<<a<<" and "<<b<<" is = "<<counter;
 
  getch();

}



Share:

Thursday, December 19, 2013

Program Print Number Triangle Reverse 5 To 1

Program Print Number Triangle Reverse 5 To 1
/*
Write a C++ Program to display the following
Number Triangle output by nested for loop.
54321
5432
543
54
5

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int counter1, counter2;

  cout<<"\n\n ----------- Results ------------\n";

  for ( counter1 =1; counter1<=5; counter1++)

     {
        for(counter2=5; counter2>=counter1; counter2--)
        cout<<counter2;

        cout<<endl;
     }  

  getch();

}
Share:

Program Print Number Triangle Downside

C++ Program to print number triangle downside with nested loops
C++ Program to print number triangle downside with nested loops

Write a C++ Program to display the following
Number Triangle output by nested for loop.
12345
1234
123
12
1

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int counter1, counter2;

  cout<<"\n\n ----------- Results ------------\n";

  for ( counter1 =5; counter1>=1; counter1--)

     {
        for(counter2=1; counter2<=counter1; counter2++)
        cout<<counter2;

        cout<<endl;
     }  

  getch();

}
Share:

Program Print Number Triangle Upside

Program in C++ to Print Number Triangle /*
Write a C++ Program to display the following
Number Triangle output by nested for loop.
1
12
123
1234
12345

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/

#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int counter1, counter2;

  cout<<"\n\n ----------- Results ------------\n";

  for ( counter1 =1; counter1<=5; counter1++)

     {
        for(counter2=1; counter2<=counter1; counter2++)
        cout<<counter2;

        cout<<endl;
     }  

  getch();

}
Share:

Program Print Alphabet Triangle Downside

 

/*
Write a C++ Program to display the following
alphabet triangle output by nested for loop.
  ABCDE
  ABCD
  ABC
  AB
  A
For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  char counter1, counter2;

  cout<<"\n\n ----------- Results ------------\n";

  for ( counter1 =69; counter1>=65; counter1--)

     {
        for(counter2=65; counter2<=counter1; counter2++)
        cout<<counter2;

        cout<<endl;
     }

  getch();

}
Share:

Program Print Alphabet Triangle

/*
Write a C++ Program to display the following
alphabet triangle output by nested for loop.
  A
  AB
  ABC
  ABCD
  ABCDE

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Perfect computer notes
visit www.computergap.com
For Good study Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/

c++ program to print alphabet triangle easyway how to C++ Programs
C++ Program to output a right angle triangle of English alphabets Easyway C++


#include<conio.h>

void main()
{

  clrscr();

  char counter1, counter2;

  cout<<"\n\n ----------- Results ------------\n";

  for ( counter1 =65; counter1<=69; counter1++)

     {
        for(counter2=65; counter2<=counter1; counter2++)
        cout<<counter2;

        cout<<endl;
     }

  getch();

}
Share:

Program Display Star Triangle Shape

/*
Write a C++ Program to display the following
output by nested for loop.
  *****
  ****
  ***
  **
  *

c++ program to display star triangle

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/

#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int counter1, counter2;

  cout<<"\n\n ----------- Results ------------\n";

  for ( counter1 =5; counter1>=1; counter1--)

     {
        for(counter2=1; counter2<=counter1; counter2++)
        cout<<"*";

        cout<<endl;
     }  

  getch();

}
Share:

Wednesday, December 18, 2013

Explain Working of Nested For Loop With Example Code

What is a Nested For Loop?

A nested for loop is a for loop within another for loop. Consider the following example code:

     for ( i = 1; i <= 2; i++)
               for ( k = 1; k<= 3; k++)
                     cout<<"Easy C++ Programming"<<endl;

Here, the C++ statement ' cout<<"Easy C++ Programming"<<endl; ' will be executed 2 x 3 = 6 times.
So output of the above C++ code will be:

Easy C++ Programming
Easy C++ Programming
Easy C++ Programming
Easy C++ Programming
Easy C++ Programming
Easy C++ Programming
What is nested for loop, working of nested for loop In above figure, it is obvious that a single iteration of Outer Loop(for value of i = 1), Inner Loop will perform 3 repetitions for value of k=1, k=2 and k=3.
Similarly, it is obvious that a second iteration of Outer Loop(for value of i = 2), Inner Loop will perform 3 repetitions for value of k=1, k=2 and k=3.

 How Nested For Loop Works?


First of all, the variable (loop counter) i is initialized to 1.
Loop condition i<=2 is checked for i = 1, which gives TRUE.
So the control goes to next for loop with loop counter k.

k is initiaized to 1.
Loop condition k<=3 is checked for k=1, which gives TRUE.
So loop body is executed. Easy C++ Programming is printed for first time.

k is incremented by 1.  So now value of k is 2.
Loop condition k<=3 is checked for k=2, which gives TRUE.
So loop body is executed. Easy C++ Programming is printed for second time.

k is incremented by 1.  So now value of k is 3.
Loop condition k<=3 is checked for k=3, which gives TRUE.
So loop body is executed. Easy C++ Programming is printed for third time.

k is incremented by 1.  So now value of k is 4.
Loop condition k<=3 is checked for k=4, which gives FALSE.
So loop is terminated.

The Control will go to  i++ of first for loop, so i is incremented by 1. Now value of i is 2.
Loop condition i<=2 is checked for i = 2, which gives TRUE.
So the control goes to next for loop with loop counter k.

k is initiaized to 1.
Loop condition k<=3 is checked for k=1, which gives TRUE.
So loop body is executed. Easy C++ Programming is printed for fourth time.

k is incremented by 1.  So now value of k is 2.
Loop condition k<=3 is checked for k=2, which gives TRUE.
So loop body is executed. Easy C++ Programming is printed for fifth time.

k is incremented by 1.  So now value of k is 3.
Loop condition k<=3 is checked for k=3, which gives TRUE.
So loop body is executed. Easy C++ Programming is printed for sixth time.

k is incremented by 1.  So now value of k is 4.
Loop condition k<=3 is checked for k=4, which gives FALSE.
So loop is terminated.

The Control will go to  i++ of first for loop, so i is incremented by 1. Now value of i is 3.
Loop condition i<=2 is checked for i = 3, which gives FALSE.
So the Loop is terminated.

This completes the execution of the nested for loop. So the statement '  cout<<"Easy C++ Programming"<<endl; ' will be executed for 6 times.

Share:

Sunday, December 15, 2013

Nested For Loop Programming Star Shapes

Example Program: Display the following output by nested for loop.


*
**
***
****
*****
nested for loop display star shape required

Solution:

/*
Write a C++ Program to display the following
output by nested for loop.


For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int counter1, counter2;

  cout<<"\n\n ----------- Results ------------\n";

  for ( counter1 =1; counter1<=5; counter1++)

     {
        for(counter2=1; counter2<=counter1; counter2++)
        cout<<"*";

        cout<<endl;
     }  

  getch();

}



Share:

Program Calculate Factorial of a Number

Today, we will learn the real logic behind C++ Program Factorial of a Given Number, in details.
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,
    5! = 5 x 4 x 3 x 2 x 1 = 120
Note: The value of 0! is 1.

find factorial of a number C++ Program calculate Factorial of given number C++ program
C++ Program Factorial of given number input by user at run time


Source Code of Factorial of a Number program for DevC++ and other Modern Compilers / IDEs

#include<iostream>

using namespace std;

int main()
{

  int number, counter;
  long int factorial;

  cout<<"\n Enter the Number = ";
  cin>>number;

  factorial = 1;
  for (counter = 1; counter <= number; counter++)
           factorial = factorial * counter;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n The Factorial of the number "<<number<<"\n is "<<factorial;

  return 0;

}

A sample output:


 Enter the Number = 5

 ----------- Results ------------

 The Factorial of the number 5
 is 120
--------------------------------
Process exited after 23.7 seconds with return value 0
Press any key to continue . . .

Source Code for Turbo C++ 3.0 IDE

/*
Write a C++ Program to input a positive number
Calculate and Display Factorial of this number
by For Loop.


For Free C++ Programming Techniques (Example C++ Programs with Explainations), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit www.computergap.com
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int number, counter;
  long int factorial;

  cout<<"\n Enter the Number = ";

  cin>>number;

  factorial = 1;
  for (counter = 1; counter <= number; counter++)
           factorial = factorial * counter;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n The Factorial of the number "<<number<<"\n is "<<factorial;

  getch();

}
C++ Program to find Factorial of given Number
C++ Program to find Factorial of given Number


Explain Logic of The Calculate Factorial of a Number, C++ Program

In the following lines, the above mentioned factorial program is further explained with the help of comments / use of documentation in the factorial program in C++


How Factorial of a Number in C++ Program Works:

#include<iostream.h>  //include header files, iostream is for cin, cout

#include<conio.h>   //include header files, conio.h is for clrscr();

void main()
{

  clrscr();   //clear the output screen

  int number, counter;   // declare variable number for input, 
                                      //counter is loop control counter variable
  long int factorial;       // factorial may be a big number so declared as long

  cout<<"\n Enter the Number = ";    // print message

  cin>>number;       // input number

  factorial = 1;         // intialize factorial to one

 // now since 3!=1 x 2 x 3
// and n! = 1 x 2 x3 ..... x n, therfore factorial calculaton loop may be from 1 to n
// so that we can multiply all numbers from 1 to n 
// and place the result of these products in factorial
  for (counter = 1; counter <= number; counter++)
           factorial = factorial * counter;

  cout<<"\n\n ----------- Results ------------";  //print message

// print result of factorial program
  cout<<"\n\n The Factorial of the number "<<number<<"\n is "<<factorial;

  getch(); // to stay outpput screen as long as we press a key

}    // end

You can download and install Turbo C++ 3.0 Compiler here
You can read the use of Writing, compiling, executing programs in C++ in Turbo C++ here

Download and Install Turbo C++ Full Screen Windows 7 Vista

You can find calculate factorial of a number program with while loop here
You can find calculate factorial of a number program with do while loop here
You can find calculate factorial of a number program using Recursion ( Recursive Factorial Function )


Share:

Program To Check Input for Perfect Number

Write a C++ program to check whether a given input number is a perfect number or not.

Solution:

What is a Perfect Number?


In number theory, a perfect number may be defined as a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.

So we will find all positive divisors(excluding the number itself) of the given number and get sum of these. If the sum is equal to the given number then IT IS A PERFECT NUMBER, otherwise not. For example, 6 is a perfect number. Because, sum of proper divisors of 6 is:
1+2+3 = 6.


perfect number C++ program

Here is the C++ Program Code for PERFECT NUMBER CHECK!


/*
Write a C++ Program to input a positive number, then check
whether this number is a perfect number?

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  long number, sum=0, counter, mid, count=0;

  cout<<"\n Enter the positive number\nto check for Perfect Number= ";

  cin>>number;

  mid = number / 2;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n Proper Positive Divisors of \n that is divisors excluding \nthe number itself "<<number<<" are=";

  for (counter = 1; counter <= mid; counter++)
           {
           cout<<"\n \t"<<counter;
           sum = sum + counter;
           }
           cout<<"\n Sum of Proper divisors ="<<sum;
 if ( number == sum )
 cout<<"\n The number is Perfect Number";
 else
 cout<<"\n the number is not a Perfect Number";


  getch();

}
Share:

Program Sum of All Even Odd Numbers Between Two Numbers


Program sum of all, even, odd numbers between two numbers
/*
Write a C++ Program to input two numbers n1 and n2:

a) display all numbers and their sum between n1 and n2 inclusive
b) display all Even numbers and their sum between n1 and n2 inclusive
c) display all Odd numbers and their sum between n1 and n2 inclusive

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int number1, number2, counter;
  long sumall = 0, sumeven=0, sumodd=0;

  cout<<"\n Enter the first number = ";

  cin>>number1;

  cout<<"\n Enter the second number = ";

  cin>>number2;


  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n All Numbers between \n Number1 and Number2 by For Loop: ";

  for (counter = number1; counter <= number2; counter++)
           {
           cout<<"\n \t"<<counter;
           sumall = sumall + counter;
           }
  cout<<"\n\n The Sum of All Numbers between \n Number1 and Number2 by For Loop ="<<sumall;


  cout<<"\n\n All Even Numbers between \n Number1 and Number2 by For Loop: ";

  if ( number1 % 2 == 0)
      counter = number1;
  else
  counter = number1 + 1;

  for (; counter <= number2; counter+=2)
           {
           cout<<"\n \t"<<counter;
           sumeven = sumeven + counter;
           }
  cout<<"\n\n The Sum of All Even Numbers between \n Number1 and Number2 by For Loop ="<<sumeven;


  cout<<"\n\n All Odd Numbers between \n Number1 and Number2 by For Loop: ";

  if ( number1 % 2 == 1)
      counter= number1;
  else
      counter = number1 + 1;

  for (; counter <= number2; counter+=2)
           {
           cout<<"\n \t"<<counter;
           sumodd = sumodd + counter;
           }
  cout<<"\n\n The Sum of All Odd Numbers between \n Number1 and Number2 by For Loop ="<<sumodd;






  getch();

}
Share:

Program Display SUM All Odd from 1 To N


Program Display SUM All Odd from 1 To N, SUM of All ODD from 1 to N /*
Write a C++ Program to display and sum of All Odd numbers from
1 to n (the entered positive number)by FOR Loop

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int number, counter;
  long sum = 0;
  cout<<"\n Enter a positive number = ";

  cin>>number;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n All Even Numbers between \n 1 to n(entered positive number)\n by For Loop ";

  for (counter = 1; counter <= number; counter+=2)
           {
           cout<<"\n \t"<<counter;
           sum = sum + counter;
           }
  cout<<"\n\n The Sum of All Odd Numbers between \n 1 to n(entered positive number)\n by For Loop ="<<sum;

  getch();

}
Share:

Thursday, December 12, 2013

Program Sum of Even Numbers 1 To N

Program Sum of Even Numbers 1 To N, Calculate and display even all numbers and sum from 1 to N
Easyway how to write C++ Programs sum all even numbers from 1 to N
/*
Write a C++ Program to display and sum of All Even numbers from
1 to n (the entered positive number)by FOR Loop

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   www.ComputerGap.com
Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int number, counter;
  long sum = 0;
  cout<<"\n Enter a positive number = ";

  cin>>number;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n All Even Numbers between \n 1 to n(entered positive number)\n by For Loop ";

  for (counter = 2; counter <= number; counter+=2)
           {
           cout<<"\n \t"<<counter;
           sum = sum + counter;
           }
  cout<<"\n\n The Sum of All Even Numbers between \n 1 to n(entered positive number)\n by For Loop ="<<sum;

  getch();

}
Share:

Program Sum of Numbers 1 To N



/*
Write a C++ Program to display and sum of All numbers from
1 to n (the entered positive number)by FOR Loop

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int number, counter;
  long sum = 0;
  cout<<"\n Enter a positive number = ";

  cin>>number;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n All Numbers between \n 1 to n(entered positive number)\n by For Loop ";

  for (counter = 1; counter <= number; counter++)
           {
           cout<<"\n \t"<<counter;
           sum = sum + counter;
           }
  cout<<"\n\n The Sum of All Numbers between \n 1 to n(entered positive number)\n by For Loop ="<<sum;

  getch();

}
Share:

Program Number Raise To the Power by Loop

Program Number Raise To the Power For Loop, Program to calculate power of a number by for loop
/*
Write a C++ Program to get two numbers from the user,
and Calculates and displays the result of first number
raise to the power of second number. Use For Loop.

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int number, power, counter;
  long result=1;

  cout<<"\n Enter the Number = ";

  cin>>number;

  cout<<"\n Enter the power = ";

  cin>>power;

  for ( counter = 1; counter <= power; counter++)
      result = result * number;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n The Result of "<<number<<" rasie to the power "<<power<<"\nis "<<result;

  getch();

}

Share:

Wednesday, December 11, 2013

Program Factorial by Loop and Recursion Versions

 Program FACTORIAL by RECURSION METHOD
Recursive Function Factorial of number in C++ recursion version
Recursive Function Factorial of number in C++ programming recursion version


What is Recursion

When a function in C++ calls itself, it is called Recursive Function and the process of calling a function itself is called Recursion.
A method of defining a function in terms of its own definition is called recursion.

Advantages of using Recursion in C++

  1. Recursion is a good problem solving technique in programming
  2. It is based on the concept to solve a problem by reducing the problem to smaller sub-problems;
  3. This results in recursive calls. In each successive recursive call the problem is reduced to more smaller sub-problem.
  4. Recursive algorithms are simple to understand
  5. Recursive algorithms are easy to implement


• Example: the Fcatorial of a non negatve number

  Base case in Recursion

   Recursion needs a base-case in order to stop

Recursive case

Recursion needs a recursive case to call a function itself to reduce problem into sub-problems
• fact(n) = n x fact(n-1)       Recursive part / recursive case of recursion
• fact(0) =  1                Terminating condition part also called base case
• In programming recursion is a method call to the same method. In other words, a recursive method     is one that calls itself.
• Why write a method that calls itself?

• But! Recursive calls can result in a an infinite loop of calls
• recursion needs a base-case in order to stop    , example fact(0) = 1, if n=0 then factorial =1
as we know that factorial of 0 is 1.
A recursive function to find factorial of a number works by calling itself
Example: if n>0 the factorial(n) = n x factorial (n-1)
as we know n! = n x (n-1)!

 /*
Write a C++ Program to input a positive number,
Calculate and display factorial of this number
by recursion.

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ComputerGap.com
*/
#include<iostream.h>

#include<conio.h>

long  factorial(int n);
void main()
{

  clrscr();

  int number, counter;
 

  label1:
 
  cout<<"\n Enter the Number = ";

  cin>>number;

  if ( number < 0)
  {
  cout<<"\n Enter a non negative number, please!";
  goto label1;
  }
  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n The Factorial of the number "<<number<<"\n is "<<factorial(number);

  getch();

}

long factorial(int n)

{

    if ( n == 0 )
        return 1;
    else
        return n * factorial(n-1);
}
 


Factorial of a number in C++ for loop version for understanding Recursion Logic


/*
Write a C++ Program to input a positive number
Calculate and Display Factorial of this number
by For Loop.


For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int number, counter;
  long int factorial;

  cout<<"\n Enter the Number = ";

  cin>>number;

  factorial = 1;
  for (counter = 1; counter <= number; counter++)
           factorial = factorial * counter;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n The Factorial of the number "<<number<<"\n is "<<factorial;

  getch();

}



You can download and install Turbo C++ 3.0 Compiler here
You can read the use of Writing, compiling, executing programs in C++ in Turbo C++ here

Download and Install Turbo C++ Full Screen Windows 7 Vista

You can find calculate factorial of a number program with while loop here
You can find calculate factorial of a number program with do while loop here

You can find calculate factorial of a number program using Recursion ( Recursive Factorial Function )

Share:

Tuesday, December 10, 2013

All Even Numbers Between Two Numbers Inclusive

All Even Numbers Between Two Numbers Inclusive, Program To Display All Even Numbers Between Two Numbers Inclusive

/*
Write a C++ Program to input two numbers
n1 and n2 then display All Even numbers between
these numbers (inclusive) by FOR Loop.

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int number1, number2, counter;

  cout<<"\n Enter the First Number = ";

  cin>>number1;

  cout<<"\n Enter the Second Number = ";

  cin>>number2;

   if ( number1 == number2 )
  {
      cout<<"\n Both Numbers are equal,\nso there are no numbers between them, Exiting!";
          goto label1;
  }

  if ( number1 > number2 )
  {
          int temp = number1;
          number1 = number2;
          number2=temp;
  }

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n All Even Numbers between \n "<<number1<<" and "<<number2<<" (inclusive)are:\n";

  for (counter = number1; counter <= number2; counter++)
           if ( counter % 2 == 0)
           cout<<"\n \t"<<counter;

  label1:
  getch();

}
Share:

Monday, December 9, 2013

Program For Loop Display 1 To N Odd

Program to display all odd numbers between 1 and N
/*
Write a C++ Program to display All Odd numbers from
1 to n (the entered positive number greater than 1)
by FOR Loop.

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int number, counter;

  label1:
  cout<<"\n Enter a positive number = ";

  cin>>number;

  if ( number<=1 )
  {
    cout<<"\n Please Enter a Positive Number Greater than 1";
    goto label1;
  }

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n Display All Odd Numbers between \n 1 to n(entered positive number)\n by For Loop ";

  for (counter = 1; counter <= number; counter+=2)
           cout<<"\n \t"<<counter;

  getch();

}
Share:

Friday, December 6, 2013

Program For Loop Display 1 To n Numbers

/*
Write a C++ Program to display All numbers from
1 to n (the entered positive number)by FOR Loop

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/


Program For Loop Display 1 To n Numbers, C Plus Plus program to display 1 to n numbers
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int number, counter;

  cout<<"\n Enter a positive number = ";

  cin>>number;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n Display All Numbers between \n 1 to n(entered positive number)\n by For Loop ";

  for (counter = 1; counter <= number; counter++)
           cout<<"\n \t"<<counter;

  getch();

}
Share:

Program For Loop Display Odd 1 to 10


/*
Write a C++ Program to display Odd between 1 to 10 numbers by FOR Loop

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com

*/
Program For Loop Display Odd 1 to 10, C Plus Plus Program Display Odd Numbers between 1 to 10
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int counter;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n Display Odd Numbers between \n 1 to 10 by For Loop ";

  for (counter = 1; counter <= 10; counter += 2)
           cout<<"\n"<<counter;

  getch();

}
Share:

Program For Loop Display Even 1 to 10

Program To Display Even Numbers between 1 to 10, Display Even 1 to 10 by FOR Loop 

/*
Write a C++ Program to display Even between 1 to 10 numbers by FOR Loop

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com

*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int counter;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n Display Even Numbers between \n 1 to 10 by For Loop ";

  for (counter = 2; counter <= 10; counter += 2)
           cout<<"\n"<<counter;

  getch();

}
Share:

Program For Loop Display 10 To 1 Numbers

C Plus Plus Program To display numbers from 10 down to 1, Program For Loop Display 10 To 1 Numbers

/*
Write a C++ Program to display 10 down to 1 numbers by FOR Loop

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com

*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int counter;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n Display Numbers from 1 to 10 by For Loop ";

  for (counter = 10; counter >= 1; counter--)
           cout<<"\n"<<counter;

  getch();

}
Share:

Program For Loop Display 1 to 10

Program For Loop Display 1 to 10, Display 1 to 10 numbers by for loop

/*
Write a C++ Program to display 1 to 10 numbers by FOR Loop

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com

*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int counter;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n Display Numbers from 1 to 10 by For Loop ";

  for (counter = 1; counter <= 10; counter++)
           cout<<"\n"<<counter;
 
  getch();

}
Share:

Monday, December 2, 2013

Program To Find Circumference of Circle

/*
Write a C++ Program to input a float number(radius of circle) and
calculate / display the Circumference of the circle.

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com

*/


Program To Find Circumference of Circle, How To Find Circumference of Circle While Area is given
#include<iostream.h>

#include<conio.h>

void main()
{
                                  
  clrscr();

  const float PI = 3.14;

  float radius, circumference;

  cout<<"\n Enter Radius of the Circle (to find its Circumference): ";

  cin>>radius;

  // required formula
  // Circumference of Circle = 2 x PI x R

  circumference = 2 * PI * radius;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n Circumference of Circle = "<<circumference;

  getch();

}
Share:

Program To Find Area of Circle


/*
Write a C++ Program to input a float number(radius of circle) and
calculate / display the Area of the circle.

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com

*/

Find Area of Circle, Input radius and find area of circle, program area of circle

#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  const float PI = 3.14;

  float radius, area;

  cout<<"\n Enter Radius of the Circle (to find its area): ";

  cin>>radius;

  // required formula
  // Area of Circle = PI x R(Squared)

  area = PI * radius * radius;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n Area of Circle = "<<area;

  getch();

}
Share:

Sunday, December 1, 2013

Program Find Quotient and Remainder of Division


/*
Program to input a number(dividend) and second number(divisor), and display the Qutient
and remainder of the division.
For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com

*/

Free C++ Program to find qutient and Remainder of Division,Program Find Out Quotient and Remainder, Program Division, Program Dividend Divisor Quotient Remainder

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();

  int dividend, divisor, q, r;

  cout<<"\n Enter a number (Devidend) =";

  cin>>dividend;

  cout<<"\n Enter a number (divisor) =";

  cin>>divisor;

  q = dividend / divisor;

  r = dividend % divisor;

  cout<<"\n ----------- Results ---------------";

  cout<<"\n Dividend = "<<dividend;

  cout<<"\n divisor = "<<divisor;

  cout<<"\n Quotient = "<<q;

  cout<<"\n Remainder = "<<r;

  getch();

}
Share:

Thursday, October 31, 2013

Program Find Out Square Cube of Number




/*

Program to input a number, then display its square and cube.
(c) Www.EasyCppProgramming.Blogspot.Com
*/


#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
     int num, square, cube;

     clrscr();

     cout<<"Enter a number=";
     cin>>num;

     cout<<"\n Method 1: By Multiplication operator *\n";
    
     square = num * num;
     cube   = num * num * num;
     cout<<"\n The number="<<num<<"    Square="<<square<<"   Cube="<<cube;


     cout<<"\n\n Method 2: By pow function of math.h \n";
    
     square = pow(num,2);
     cube   = pow(num,3);
     cout<<"\n The number="<<num<<"    Square="<<square<<"   Cube="<<cube;

     getch();

     }

Output of the program execution:


Share:

Tuesday, October 29, 2013

Program Exchange Values Without Third Variable


#include<iostream.h>
#include<conio.h>
void main()
{
     int v1, v2;

     cout<<"Enter value of first variable:";
     cin>>v1;
  
     cout<<"Enter value of second variable:";
     cin>>v2;

     cout<<"The values before swapping are: \n Varible 1="<<v1<<" Variable 2="<<v2;
     v2 = v1+v2;
     v1 = v2-v1;
     v2 = v2-v1;

     cout<<"\n The values after swapping are: \n Varible 1="<<v1<<" Variable 2="<<v2;  
     
      getch();

     }
Share:

Friday, October 25, 2013

Program to Swap Values of Two Variables




#include<iostream.h>
#include<conio.h>
void main()
{
     int v1, v2, temp;

     cout<<"Enter value of first variable:";
     cin>>v1;
   
     cout<<"Enter value of second variable:";
     cin>>v2;

     cout<<"The values before swapping are: \n Varible 1="<<v1<<" Variable 2="<<v2;
     temp = v1;
     v1 = v2;
     v2 = temp;

     cout<<"\n The values after swapping are: \n Varible 1="<<v1<<" Variable 2="<<v2;   
      
      getch();

     }
   
Share:

Wednesday, July 24, 2013

Learn Algorithm Writing, Flowcharting By Example

Algorithm 1: ADDITION
  This algorithm inputs two numbers then calculates and shows addition result of these two numbers. This algorithm uses three variables N1, N2, and ADDITION.

1. Start
2. Input N1, N2
3. Calculate ADDITION = N1 + N2
4. Display ADDITION
5. End





Algorithm 2: AGE CONVERSION
                                 This algorithm inputs age in years, converts it into dayas and shows result. This algorithm uses two variables YEARS and DAYS.

1. Start
2. Input YEARS
3. Calculate DAYS = YEARS x 365
4. Display DAYS
5. End



Algorithm 3: TEMPERATURE CONVERSION
                                 This algorithm inputs temperature  in Fahrenheit, converts it into Centigrade and shows result. This algorithm uses two variables F and C.

1. Start
2. Input F
3. Calculate C = 5/9 x (F-32)
4. Display C
5. End




Algorithm 4: PAY CALCULATOR
                                 This algorithm inputs name and basic pay of an employee, calculates and shows Net Pay. Where House Rent= 40% of basic pay, Medical Allowance 30%, Deduction = 5%.This algorithm uses 6 variables NAME, BASIC, NETPAY, HR, MA and DF.

1. Start
2. Input NAME, BASIC
3. Calculate HR = BASIC x 40/100
4. Calculate MA = BASIC x 30/100
5. Calculate D = BASIC x 5/100 
6. Calculate NETPAY = BASIC + HR + MA - D
7. Display NAME, NETPAY
8. End




Share:

EasyCPPprogramming.blogspotcom

Blog Archive

Labels