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:

EasyCPPprogramming.blogspotcom

Labels