tag:blogger.com,1999:blog-9214919403286601442024-03-13T20:07:48.525-07:00C++ CodesLearn C++ Through Sample ProgramsVinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-921491940328660144.post-8606999594022693812016-02-08T20:53:00.002-08:002016-02-08T21:14:26.393-08:00Best C++ Programming Language Books For BeginnersThe best way of starting to learn a programming language is by reading a book.<br />
<br />
So if you are still a beginner in learning the C++ programming language, here's a list of the best C++ books you can get started reading.<br />
<br />
<h4>
1. <b><a href="http://amzn.to/1K8Sw8v" rel="nofollow" target="_blank">C++: The Ultimate Guide To Learn C Language</a></b></h4>
<div>
This C++ book by Peter Hoffman which is aimed for beginners. The book has 66 pages which is indeed a short book which a beginner can go through within one day. </div>
<div>
<br /></div>
<div>
This book teaches the basic of the C++ language. Through this book, you will learn how to make your first program in less than 10 minutes. It also teaches about variables, operators, loops, conditions and pointers. This is a good starting point for a true beginner.</div>
<div>
<br /></div>
<div style="text-align: center;">
<a href="http://amzn.to/1K8Sw8v" rel="nofollow" target="_blank"><b><span style="color: orange;">Grab a copy of this book here.</span></b></a></div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://amzn.to/1K8Sw8v" rel="nofollow" target="_blank"><img alt="Best C++ Books for Beginners" border="0" height="320" src="https://2.bp.blogspot.com/-hVPrTy-TBic/Vrluf-PG_ZI/AAAAAAAAAW0/DT3c26Ge3bc/s320/C%252B%252B%2BThe%2BUltimate%2BGuide%2Bto%2BLearn%2BC%2BLanguage.jpg" title="" width="212" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<h4 style="clear: both; text-align: left;">
<b><a href="http://amzn.to/1nZhKwf" rel="nofollow" target="_blank">2. Learn C++ In A Day</a></b></h4>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
This book is a crash course for learning the basics of C++ programming language.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
What this books is offering to beginners includes the uses of C++ language and the basics features of the language. The lessons will include on program structure, comments, functions, variables, data types, operators, advance data types, loops, conditional statements, arrays, pointers and object oriented concepts in C++ like inheritance.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
This book has a total of 136 pages for its paperback version.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<b><span style="color: orange;"><span style="color: orange;"><a href="http://amzn.to/1nZhKwf" rel="nofollow" target="_blank"><span style="color: orange;">Grab a copy of this book here</span></a>.</span></span></b></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://amzn.to/1nZhKwf" rel="nofollow" target="_blank"><img border="0" height="320" src="https://3.bp.blogspot.com/-24lYPq8i39I/VrlxjyoSeeI/AAAAAAAAAXA/G-drlvJkboE/s320/Learn%2BC%252B%252B%2Bin%2Ba%2BDay.jpg" width="213" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<h4 style="clear: both; text-align: left;">
<b><a href="http://amzn.to/1nZiocY" rel="nofollow" target="_blank">3. C++: The Ultimate Beginner's Guide</a></b></h4>
<div class="separator" style="clear: both; text-align: left;">
This book has 84 pages which offers almost the similar basic lessons about the C++ language. </div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Through this book, you will learn the basic syntax of C++ language and its basic features. There are lessons about data types, variables, operators, loops, conditional statements and functions. </div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
This is a short book and it offers you a clear overview of what C++ language is and the opportunity of learning more. </div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
What's good in reading a short book is the feeling of fulfillment once you read the whole book. It's one good milestone towards learning.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<b><a href="http://amzn.to/1nZiocY" rel="nofollow" target="_blank"><span style="color: orange;">Grab a copy of this book here.</span></a></b></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://amzn.to/1nZiocY" rel="nofollow" target="_blank"><img border="0" height="320" src="https://3.bp.blogspot.com/-9akngcxr9nM/Vrl0HN6FeRI/AAAAAAAAAXM/9y6ZAj25uAo/s320/C%252B%252B%2BThe%2BUltimate%2BBeginners%2BGuide.jpg" width="213" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
This books are from Amazon and one good thing is that there are reviews of customer who already bought any of these books. You can check out their reviews and see if you can also benefit based on your current learning needs.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Hope that this will be helpful to you in your journey towards learning C++ programming language. </div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Keep on reading C++ books because that will truly help you.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<br />
<br />Vinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com4tag:blogger.com,1999:blog-921491940328660144.post-4631790817985668692013-12-09T07:42:00.001-08:002014-03-07T17:08:55.366-08:00Learn C++ From Scratch Series<div style="text-align: justify;">
In every thing, a beginner would always want to know what he needs in order to start learning, where to start and how to start. In the same way, becoming a software developer or programmer, one has to start learning from the start. Knowing this need, this page will help a beginner of C++ to take every step learning how to program C++. This is a series of topics and tutorials which will help a beginner to take learning in a step by step process.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Listed are the topics and posts which are set in order so that a C++ student will know which to topic to learn and take first. This is a path to learning C++ through explanations and examples.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
We will leave to you the research about the C++ programming language as there are plenty of resources in the web about C++ and software development in general. So in this tutorial, we will help you create your first program, run it up to learning how to program in C++ by yourself.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The most important thing is that to enjoy what you're doing. </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
What do you need to start coding your program? You will need to have a development environment and for this, we will use Microsoft's Visual Studio. There is the Microsoft Visual Studio Express version if you want a free, lightweight version which is intended for students and hobbyist like you. You can always buy for a premium version anytime if you want.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Happy coding!</div>
<div style="text-align: justify;">
<br />
For easy navigation of the lessons, I have placed them in the left side panel of this page - the BEGINNER's GUIDE will contains the topics to learn from top to bottom arrangement.<br />
<br />
I will add lessons as I go. I'm still starting this page, so I hope you will bear with me. Your suggestions are welcome. Please subscribe if you want to be notified whenever, I post a new tutorial lesson or a new sample program.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
I hope that this guide helps you as a learner of C++ language. Please feel free to contact me if you have any suggestions, clarifications and questions, see contact page.</div>
<div style="text-align: justify;">
<br />
Thank you very much! Enjoy and happy learning!</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<br /></div>
Vinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com1tag:blogger.com,1999:blog-921491940328660144.post-11436393709061396112013-11-22T18:27:00.001-08:002014-03-07T17:00:26.425-08:00How To Create A C++ Win32 Application Project in Visual StudioAs a beginner, one has to start learning a programming language through reading a tutorial and to try out himself the examples as he goes through the tutorial. This way he can experience how a program runs and he can see what it looks like. Thus, it is extremely important that as a beginner, he should know where to write the programs. The application, where someone use to write a program, is called an editor. But for one to write and run his programs, programmers use the applications called an IDE (Intergrated Development Environment) which contains an editor, compiler and other sets of useful software development tools.<br />
<br />
When someone begins to study a programming language, the most fulfilling achievement and inspiration is to be able to run a program himself. This will help add up to the excitement and yearning for learning the programming language and software development in general.<br />
<br />
Let take the first steps of learning C++. There are plenty of IDEs but let's take Microsoft's Visual Studio here and learn how to create a Visual C++ Win32 Console Application and start writing the first programs as a C++ programmer.<br />
<br />
First, you should be able to download and install a Microsoft Visual Studio Express Version that you can use, just go to google and search for it in Microsoft's site. After you have downloaded and installed MS Visual Studio, then you can now proceed.<br />
<br />
Run your MS Visual Studio and then follow the steps below.<br />
<br />
For our tutorials, let us create a Win32 Console application which when we run the program, we can see the output in the console window. Don't worry about this for now, we will see the output anyway in our programs.<br />
<br />
These are the steps on how to create a Visual C++ Win32 Console Application project:<br />
<br />
1. Open your Visual Studio.<br />
2. In the Menu, click to <b>FILE</b>.<br />
3. Click <b>New Project</b>.<br />
4. In the New Project window, make sure the template is <b>Visual C++</b>.<br />
5. Then click <b>Win32 Console Application</b> for the project. <br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-BlIBD75_x0w/UpAOROPkM2I/AAAAAAAAAPQ/v-uIdykuk4w/s1600/New+Project.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-BlIBD75_x0w/UpAOROPkM2I/AAAAAAAAAPQ/v-uIdykuk4w/s640/New+Project.png" height="392" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-z25R7mfyDfU/UpANcGLYwZI/AAAAAAAAAPI/Fpc3p85qgLg/s1600/New+Project.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><br /></a></div>
<br />
<br />
6. Change the Name, Location, Solution and Solution Name as desired. But changing the Name and Solution Name would be okay.<br />
7. Click <b>OK</b><br />
8. A new window opens, the Win32 Application Wizard.<br />
9. Click the <b>Application Settings</b>.<br />
10. Choose <b>Console Application</b> and check <b>Empty Project</b>. <br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-05D9umjEDSQ/UpARC_9ZkSI/AAAAAAAAAPk/g6z6uDc9-EI/s1600/Application+wizard.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-05D9umjEDSQ/UpARC_9ZkSI/AAAAAAAAAPk/g6z6uDc9-EI/s640/Application+wizard.png" height="493" width="640" /></a></div>
<br />
<br />
Now, you have a new empty project solution. <br />
<br />
Now let us proceed to the next lesson, creating our first program - the famous <b><a href="http://cppstudent.blogspot.com/2011/06/our-first-program-hello-world.html" target="_blank">HELLO WORLD </a></b>program.Vinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com1tag:blogger.com,1999:blog-921491940328660144.post-64897392746709031512013-09-12T00:16:00.000-07:002015-11-17T14:56:50.910-08:00Multiplication Table (10 x 10)<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-zRxcmza8Wrk/VkuwgntnGmI/AAAAAAAAAU0/JRsDtn-0CW4/s1600/C%252B%252B%2BProgram%2Bmultiplication%2Btable.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="C++ program for Multiplication table (10 x 10)" border="0" height="320" src="http://3.bp.blogspot.com/-zRxcmza8Wrk/VkuwgntnGmI/AAAAAAAAAU0/JRsDtn-0CW4/s400/C%252B%252B%2BProgram%2Bmultiplication%2Btable.jpg" title="Multiplication table (10 x 10)" width="400" /></a></div>
<br />
<br />
Problem:<br />
<blockquote class="tr_bq">
Write a multiplication table ( 10 x 10)</blockquote>
The following program shows a 10 x 10 multiplication table.<br />
It uses the escape sequence for horizontal tab (\t) to properly align the table.<br />
<br />
<!--INFOLINKS_OFF-->
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green;">/*</span>
<span style="color: green;">Multiplication Table (10 * 10)</span>
<span style="color: green;">*/</span>
<span style="color: blue;">#include <iostream></span>
<span style="color: blue;">using</span> <span style="color: blue;">namespace</span> std;
<span style="color: #2b91af;">int</span> main(<span style="color: #2b91af;">void</span>)
{
<span style="color: #2b91af;">int</span> x,y;
<span style="color: #2b91af;">char</span> waitInput;
<span style="color: blue;">for</span>(x=1; x<=10; x++)
{
<span style="color: blue;">for</span>(y=1; y<=10; y++)
{
<span style="color: green;">//use horizontal tab (\t) to align properly</span>
cout << <span style="color: #a31515;">"\t"</span> << (x*y);
}
<span style="color: green;">//newlines</span>
cout<<endl<<endl;
}
cin>>waitInput;
<span style="color: blue;">return</span> 0;
}
</pre>
</div>
Vinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com1tag:blogger.com,1999:blog-921491940328660144.post-37453634669022436222013-09-11T23:54:00.002-07:002015-11-17T15:17:41.997-08:00Reverse The Number<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-XNkT05lIWDk/Vku1fjUMVII/AAAAAAAAAVE/qrHSRbsK1oM/s1600/C%252B%252B%2Bprogram%2Bto%2Breverse%2Bthe%2Bnumber.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="C++ program to reverse the number" border="0" height="320" src="http://1.bp.blogspot.com/-XNkT05lIWDk/Vku1fjUMVII/AAAAAAAAAVE/qrHSRbsK1oM/s400/C%252B%252B%2Bprogram%2Bto%2Breverse%2Bthe%2Bnumber.jpg" title="Reverse the number" width="400" /></a></div>
<br />
<br />
Problem:<br />
<blockquote class="tr_bq">
Write a C++ program which ask for a number and reverse its digits.<br />
Ex. Number = 1234 , the answer will be 4321</blockquote>
In order to reverse the number, we have to get the every digit starting from the rightmost or the last digit. In the above example it is number 4, then next is 3, then 2 until 1.<br />
<br />
Steps to extract each digit and construct the reversed value of the number:<br />
<br />
<blockquote class="tr_bq">
1) Extract the last digit through the use of modulus operator (%) to get the remainder when the a number is divided by 10.<br />
2) Add the extracted digit to a reversed number variable (ex. reversedNum) which will be constructed. The variable should be initialized to 0 and must be multiplied with 10 before the extracted digit is added.<br />
3) After the last digit is extracted, divide the number with 10 to eliminate the extracted digit and start again to Step 1. The cycle continues until there is number is already 0.</blockquote>
<br />
The following program shows how to reverse the digits of a number.<br />
<br />
<!--INFOLINKS_OFF-->
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green;">/*</span>
<span style="color: green;">Reverse the number</span>
<span style="color: green;">*/</span>
<span style="color: blue;">#include <iostream></span>
<span style="color: blue;">using</span> <span style="color: blue;">namespace</span> std;
<span style="color: #2b91af;">int</span> main(<span style="color: #2b91af;">void</span>)
{
<span style="color: #2b91af;">long</span> number, remainder, reverseNumber = 0;
<span style="color: #2b91af;">char</span> hang;
cout<< <span style="color: #a31515;">"Enter the number: "</span>;
cin>>number;
<span style="color: blue;">while</span>(number)
{
<span style="color: green;">//extract the last digit from the number</span>
remainder = number % 10;
<span style="color: green;">//build the reverseNumber digit by digit</span>
reverseNumber = (reverseNumber * 10) + remainder;
<span style="color: green;">//exclude the last digit from the number</span>
<span style="color: green;">//ready for the next evaluation</span>
number = number / 10;
}
cout<< <span style="color: #a31515;">"Reversed number = "</span> << reverseNumber <<endl;
<span style="color: green;">//to let console stay</span>
cin>>hang;
<span style="color: blue;">return</span> 0;
}
</pre>
</div>
<!--INFOLINKS_ON-->
Output of the program:<br />
<br />
<!--INFOLINKS_OFF-->
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green;">//Sample 1</span>
Enter the number: 1235
Reversed number = 5321
<span style="color: green;">//Sample 2</span>
Enter the number: 1023
Reversed number = 3201
<span style="color: green;">//Sample 3</span>
Enter the number: 5007
Reversed number = 7005
<span style="color: green;">//*Sample 4</span>
Enter the number: 012
Reversed number = 21
<span style="color: green;">//*Sample 5</span>
Enter the number: 210
Reversed number = 12
</pre>
</div>
<!--INFOLINKS_ON-->
<br />
<blockquote class="tr_bq">
*Note that for inputs having 0 in the beginning and in the end, 0 will not be include in the output, See below:<br />
Input = 012 Output = 21 ( 012 is considered as 12 because the 0 is not significant)<br />
Input = 210 Output = 12 ( instead of 012, 0 is not significant in the answer)</blockquote>
<div>
<br /></div>
Vinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com6tag:blogger.com,1999:blog-921491940328660144.post-47440524333841932952013-09-10T08:04:00.001-07:002015-11-17T14:34:44.994-08:00Check If A Number Is A Prime Number <br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-YmlEYy3gZhA/VkurFtSmlPI/AAAAAAAAAUc/HVf15_406ps/s1600/Check%2BPrime%2BNumber.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="C++ program to check if a number is a prime number" border="0" height="320" src="http://1.bp.blogspot.com/-YmlEYy3gZhA/VkurFtSmlPI/AAAAAAAAAUc/HVf15_406ps/s400/Check%2BPrime%2BNumber.jpg" title="Check if a number is a prime number" width="400" /></a></div>
<br />
<br />
Problem:<br />
<div>
<blockquote class="tr_bq">
Write a C++ program which checks if a number is a prime number or not?</blockquote>
<div>
A prime number is any natural number greater than 1 with no other positive factors other than 1 and itself. </div>
</div>
<div>
<br />
Let's take 5 as an example:<br />
<blockquote class="tr_bq">
1 and 5 (the number itself) can evenly divide 5 so they are factors of 5.<br />
2, 3, 4 are cannot evenly divide 5 so they are not factors of 5.<br />
Therefore, 5 is a Prime Number.</blockquote>
The program below shows a number is asked and it is evaluated if a prime number or not.<br />
<br />
<a name='more'></a><br />
Logic:<br />
Let us start checking for its factors from 2 until the number itself. A factor is a number which can evenly divide the number thus it is checked if the remainder (modulus result) is 0. Every time a factor is seen, it is counted through a counter name countFactors.<br />
<br />
When the loop ends, countFactors is checked.<br />
If countFactors is equal to 1, meaning no other factor is found except for the number itself, it is a prime number. If countFactors is greater than 1, then it is not a prime number. <br />
<br /></div>
<div>
<!--INFOLINKS_OFF-->
<!-- HTML generated using hilite.me -->
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green;">/*</span>
<span style="color: green;">Prime Number</span>
<span style="color: green;">Check if a number is a prime number or not.</span>
<span style="color: green;">*/</span>
<span style="color: blue;">#include <iostream></span>
<span style="color: blue;">using</span> <span style="color: blue;">namespace</span> std;
<span style="color: #2b91af;">int</span> main(<span style="color: #2b91af;">void</span>)
{
<span style="color: green;">//variables</span>
<span style="color: #2b91af;">int</span> number,i,countFactors;
<span style="color: #2b91af;">char</span> hang;
cout << <span style="color: #a31515;">"Enter the number: "</span>;
cin >> number;
countFactors = 0;
<span style="color: blue;">for</span> (i = 2; i <= number; i++)
{
<span style="color: green;">//if a factor exists from 2 up to the number, count Factors</span>
<span style="color: blue;">if</span> (number % i == 0)
{
countFactors++;
}
}
<span style="color: green;">//a prime number has only itself as a factor other than 1</span>
<span style="color: blue;">if</span> (countFactors == 1)
{
cout << number << <span style="color: #a31515;">" is a Prime Number."</span> <<endl;
}
<span style="color: blue;">else</span>
{
cout << number << <span style="color: #a31515;">" is not a prime number but a composite number."</span> << endl;
}
<span style="color: green;">//to let console stay</span>
cin >> hang;
<span style="color: blue;">return</span> 0;
}
</pre>
</div>
<!--INFOLINKS_ON-->
<br />
<!--INFOLINKS_OFF-->
<!-- HTML generated using hilite.me -->
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">Enter the number: 6
6 is not a prime number but a composite number.
</pre>
</div>
<!--INFOLINKS_ON-->
</div>
<br />
If you want to have a logic to minimize the loop, you can check if the factor is lesser than the number. If the factor is lesser than the number, break the loop to minimize processing time. It will not continue checking for other factors because it is already found that there is other factor other than the number,thus it is not a prime number.<br />
<br />
<!--INFOLINKs_OFF-->
<!-- HTML generated using hilite.me -->
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">countFactors = 0;
<span style="color: blue;">for</span> (i = 2; i <= number; i++)
{
cout << i << <span style="color: #a31515;">","</span>;
<span style="color: green;">//if a factor exists from 2 up to the number, count Factors</span>
<span style="color: blue;">if</span> (number % i == 0)
{
countFactors++;
<span style="color: green;">//break loop if a factor is lesser than the number</span>
<span style="color: blue;">if</span> (i < number)
{
countFactors = 2;
<span style="color: blue;">break</span>;
}
}
}
</pre>
</div>
<!--INFOLINKs_ON-->
Vinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com5tag:blogger.com,1999:blog-921491940328660144.post-32805292052713202402013-09-09T17:24:00.002-07:002015-11-17T15:22:26.130-08:00Prime Numbers Between 1-1000<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-qb5t6RuDayc/Vku2gTxRJ_I/AAAAAAAAAVM/jKqZiVEpkto/s1600/C%252B%252B%2Bprogram%2Bto%2Bfind%2Bprime%2Bnumbers%2Bbetween%2B1%2Bto%2B1000.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="C++ program to find prime numbers between 1 to 1000" border="0" height="320" src="http://2.bp.blogspot.com/-qb5t6RuDayc/Vku2gTxRJ_I/AAAAAAAAAVM/jKqZiVEpkto/s400/C%252B%252B%2Bprogram%2Bto%2Bfind%2Bprime%2Bnumbers%2Bbetween%2B1%2Bto%2B1000.jpg" title="Prime numbers between 1 to 1000" width="400" /></a></div>
<br />
<br />
Problem:<br />
<blockquote class="tr_bq">
Write a C++ program which prints all the prime numbers from 1-1000. </blockquote>
<br />
Prime numbers are numbers greater than 1 and which has 1 and the number itself as positive factors or divisors. It doesn't have a positive factor or divisors other than 1 and the number itself.<br />
<br />
<blockquote class="tr_bq">
Let's take 5 for example:<br />
1 and 5 (the number itself) can evenly divide 5 so they are factors of 5.<br />
2, 3, 4 are cannot evenly divide 5 so they are not factors of 5.<br />
Therefore, 5 is a Prime Number. </blockquote>
<br />
This is a program which shows the Prime Numbers from 1-1000.<br />
<br />
<a name='more'></a><br />
<!--INFOLINKS_OFF-->
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green;">/*</span>
<span style="color: green;">Prime Numbers (from 1 - 1000)</span>
<span style="color: green;">*/</span>
<span style="color: blue;">#include <iostream></span>
<span style="color: blue;">using</span> <span style="color: blue;">namespace</span> std;
<span style="color: #2b91af;">int</span> main(<span style="color: #2b91af;">void</span>)
{
<span style="color: #2b91af;">int</span> num,i,countFactors;
<span style="color: #2b91af;">char</span> hang;
cout << <span style="color: #a31515;">"Prime numbers (1-1000) : "</span> << endl;
<span style="color: blue;">for</span> (num = 1; num <= 1000; num++)
{
countFactors = 0;
<span style="color: blue;">for</span> (i = 2; i <= num; i++)
{
<span style="color: green;">//if a factor exists from 2 up to the number, count Factors</span>
<span style="color: blue;">if</span> (num % i == 0)
{
countFactors++;
}
}
<span style="color: green;">//a prime number has only itself as a factor</span>
<span style="color: blue;">if</span> (countFactors == 1)
{
cout << num << <span style="color: #a31515;">", "</span>;
}
}
<span style="color: green;">//to let console stay</span>
cin >> hang;
<span style="color: blue;">return</span> 0;
}
</pre>
</div>
<!--INFOLINKS_ON-->
Vinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com5tag:blogger.com,1999:blog-921491940328660144.post-13950772403056712242013-09-08T19:19:00.001-07:002015-11-17T14:47:50.410-08:00Fibonacci Numbers using Recursion<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-8ysIQTn8K1w/VkuuX2U5AZI/AAAAAAAAAUo/6UFkMn7jC3U/s1600/Fibonacci%2Bnumbers%2Busing%2Brecursion.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="C++ program - Fibonacci numbers using recursion" border="0" height="320" src="http://2.bp.blogspot.com/-8ysIQTn8K1w/VkuuX2U5AZI/AAAAAAAAAUo/6UFkMn7jC3U/s400/Fibonacci%2Bnumbers%2Busing%2Brecursion.jpg" title="Fibonacci numbers using recursion" width="400" /></a></div>
<br />
<br />
Problem:<br />
<blockquote class="tr_bq">
Write a C++ program which prints the Fibonacci sequence or the Fibonacci numbers.</blockquote>
Fibonacci sequence is named after Leonardo Fibonacci. Fibonacci numbers starts with the first two numbers 0 and 1, and each subsequent numbers takes as the sum of its previous two numbers.<br />
<br />
<blockquote class="tr_bq">
Ex. 0, 1, 1, 2, 3, 5 <br />
The first two numbers in the sequence are 0 and 1.<br />
The 3rd number which is 1 is the sum of its previous two numbers - 0 and 1.<br />
The 4th number which is 2 is the sum of its previous two numbers - 1 and 1.<br />
So on and so forth...</blockquote>
In mathematical equation, Fibonacci sequence can be represented as:<br />
<br />
<blockquote class="tr_bq">
<b>Fn = F(n-1) + F(n-2)</b> where F<span style="font-size: x-small;">0</span> = 0, F<span style="font-size: x-small;">1</span> = 1</blockquote>
<div>
<br /></div>
The following program shows the Fibonacci sequence using recursion.<br />
<br />
<a name='more'></a><br />
<!--INFOLINKS-OFF-->
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green;">/*</span>
<span style="color: green;">Fibonacci series using Recursion</span>
<span style="color: green;">*/</span>
<span style="color: blue;">#include <iostream></span>
<span style="color: blue;">using</span> <span style="color: blue;">namespace</span> std;
<span style="color: #2b91af;">int</span> fibonacci(<span style="color: #2b91af;">int</span>);
<span style="color: #2b91af;">int</span> main(<span style="color: #2b91af;">void</span>)
{
<span style="color: #2b91af;">int</span> count;
<span style="color: #2b91af;">char</span> waitInput;
cout << <span style="color: #a31515;">"How many numbers in the Fibonacci sequence do you want to show? "</span>;
cin >> count;
<span style="color: blue;">for</span> (<span style="color: #2b91af;">int</span> i = 0; i < count; i++)
{
cout << <span style="color: #a31515;">" "</span> << fibonacci(i) << <span style="color: #a31515;">" "</span>;
}
cin>>waitInput;
<span style="color: blue;">return</span> 0;
}
<span style="color: green;">//fibonacci function</span>
<span style="color: #2b91af;">int</span> fibonacci(<span style="color: #2b91af;">int</span> num)
{
<span style="color: blue;">if</span> (num == 1)
{
<span style="color: blue;">return</span> 1;
}
<span style="color: blue;">else</span> <span style="color: blue;">if</span> (num == 0)
{
<span style="color: blue;">return</span> 0;
}
<span style="color: blue;">else</span>
{
<span style="color: blue;">return</span> fibonacci(num - 1) + fibonacci(num - 2);
}
}
</pre>
</div>
<!--INFOLINKS_ON-->
<br />
<br />Vinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com4tag:blogger.com,1999:blog-921491940328660144.post-39914458414226950602011-07-26T05:05:00.000-07:002015-11-17T15:30:36.686-08:00Perfect Numbers<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-S5aeQI0Wgoc/Vku4dsKL-jI/AAAAAAAAAVY/4fJaLJZedkk/s1600/C%252B%252B%2Bprobram%2Bto%2Bfind%2Bperfect%2Bnumbers%2Bbetween%2B1%2Bto%2B1000.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="C++ program to find Perfect Numbers between 1 to 1000" border="0" height="320" src="http://2.bp.blogspot.com/-S5aeQI0Wgoc/Vku4dsKL-jI/AAAAAAAAAVY/4fJaLJZedkk/s400/C%252B%252B%2Bprobram%2Bto%2Bfind%2Bperfect%2Bnumbers%2Bbetween%2B1%2Bto%2B1000.jpg" title="Perfect Numbers between 1 to 1000" width="400" /></a></div>
<br />
<br />
This program prints all the perfect numbers from 1 to 1000. Perfect numbers are those numbers in which the sum of the number's proper positive factors (excluding the number itself) is equal to that number.<br />
Let's take the number 6 for example having the factors 1,2,3 and adding these factors will equate to the number itself (1+2+3 = 6).<br />
<br />
<!--INFOLINKS_OFF-->
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green;">/*</span>
<span style="color: green;">Perfect Numbers</span>
<span style="color: green;">show perfect numbers from 1-1000</span>
<span style="color: green;">*/</span>
<span style="color: blue;">#include <iostream></span>
<span style="color: blue;">using</span> <span style="color: blue;">namespace</span> std;
<span style="color: #2b91af;">int</span> main(<span style="color: #2b91af;">void</span>)
{
<span style="color: green;">//variables</span>
<span style="color: #2b91af;">int</span> i,j,sumOfFactors;
cout << <span style="color: #a31515;">"These are the perfect numbers:"</span> << endl;
<span style="color: green;">//check numbers from 1-1000</span>
<span style="color: blue;">for</span> (i = 1; i <= 1000; i++)
{
sumOfFactors = 0;
<span style="color: green;">//look for the factors of a number</span>
<span style="color: blue;">for</span> (j = 1; j < i; j++)
{
<span style="color: green;">//check for factors and add them up</span>
<span style="color: blue;">if</span> (0 == (i%j))
{
sumOfFactors += j;
}
}
<span style="color: green;">//if perfect number, print</span>
<span style="color: blue;">if</span> (sumOfFactors == i)
{
cout << i << endl;
}
}
getchar();
<span style="color: blue;">return</span> 0;
}
</pre>
</div>
<!--INFOLINKS_ON-->
<br />
<a name='more'></a>The program shows a for loop of range 1 to 1000.<br />
<div style="color: #0b5394;">
<br /></div>
<div>
In each loop, it evaluates the number by looking for its factors. And every time a factor is seen, it adds it to the variable sumOfFactors which will contain the sum of the all factors. But it should always have an initial value of 0 so it is initialized before it enters the loop which evaluates for the factors. Notice that this loop is only up to the value of the number less than 1 (j < i) because the number itself should not be part of the summed up factors. </div>
<br />
And a factor is determined if it has no remainder thus modulus operator (%) is used. If there is no remainder <span style="color: black;">then j is a factor of the number ( i ). </span><span style="color: black;"> </span><br />
<br />
Now after the the loop which sums up all the factors of the number, it checks if that sum is equal to the number and prints it if it is a perfect number. Then the loop continues with the new value of the number until all the numbers from 1 - 1000 are evaluated.Vinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com2tag:blogger.com,1999:blog-921491940328660144.post-28482972447233972592011-07-19T18:34:00.000-07:002015-11-17T16:49:47.577-08:00Division and Remainder Without Using the "\" ,"%" Operators<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-xkC_nKQ4G00/Vku-whx1TjI/AAAAAAAAAVo/IYnftKq96-g/s1600/C%252B%252B%2Bprogram%2Bfor%2Bdivision%2Band%2Bgetting%2Bthe%2Bremainder%2Bwithout%2Busing%2Boperators.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="C++ program for Division and remainder without using the / and % operators" border="0" height="320" src="http://1.bp.blogspot.com/-xkC_nKQ4G00/Vku-whx1TjI/AAAAAAAAAVo/IYnftKq96-g/s400/C%252B%252B%2Bprogram%2Bfor%2Bdivision%2Band%2Bgetting%2Bthe%2Bremainder%2Bwithout%2Busing%2Boperators.jpg" title="Division and remainder without using the / and % operators" width="400" /></a></div>
<span class="Apple-style-span" style="font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15px; line-height: 21px;"><br /></span>
<span class="Apple-style-span" style="font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15px; line-height: 21px;"><br /></span>
<span class="Apple-style-span" style="font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15px; line-height: 21px;"><br /></span><br />
<span class="Apple-style-span" style="font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15px; line-height: 21px;">Division, just l</span><span class="Apple-style-span" style="font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15px; line-height: 21px;">ike multiplication,</span><span class="Apple-style-span" style="font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15px; line-height: 21px;"> can be implemented without using its own division operator which is the character "/". This can be done through subtraction since division can be implemented as a series of subtraction.</span><br />
<span class="Apple-style-span" style="font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15px; line-height: 21px;">In the code below, the while loop checks if the dividend is still greater than or equal to the divisor. If so, dividend will continue subtracting from itself the value of the divisor while quotient (initialized as 0) will increment by 1 each time. What remains for the dividend (lDividend) after the loop is the remainder of the division. </span><br />
<span class="Apple-style-span" style="font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15px; line-height: 21px;"><br /></span>
<br />
<!--INFOLINKS_OFF-->
<!-- HTML generated using hilite.me -->
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green;">/*</span>
<span style="color: green;">Division without using / operator</span>
<span style="color: green;">Remainder without using % operator</span>
<span style="color: green;">*/</span>
<span style="color: blue;">#include <iostream></span>
<span style="color: blue;">using</span> <span style="color: blue;">namespace</span> std;
<span style="color: #2b91af;">int</span> main(<span style="color: #2b91af;">void</span>)
{
<span style="color: green;">//variables</span>
<span style="color: #2b91af;">int</span> dividend, divisor, quotient, remainder;
<span style="color: #2b91af;">char</span> stay;
cout << <span style="color: #a31515;">"Input dividend: "</span>;
cin >> dividend;
cout <<endl << <span style="color: #a31515;">"Input divisor: "</span>;
cin >> divisor;
quotient = 0;
<span style="color: green;">// always subtract dividend with the divisor</span>
<span style="color: green;">//until dividend is lesser than the divisor</span>
<span style="color: blue;">while</span> (dividend >= divisor)
{
dividend -= divisor;
<span style="color: green;">//quotient = count every time subtraction is done</span>
quotient++;
}
cout << endl << <span style="color: #a31515;">"The qoutient = "</span> << quotient << endl;
<span style="color: green;">//remainder = the remaining value after a series of substraction</span>
remainder = dividend;
cout << <span style="color: #a31515;">"The remainder = "</span> << remainder << endl;
<span style="color: green;">//wait for an input before exit </span>
cin >> stay;
<span style="color: blue;">return</span> 0;
}
</pre>
</div>
<!--INFOLINKS_ON-->Vinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com2tag:blogger.com,1999:blog-921491940328660144.post-19628050870891719822011-07-19T17:50:00.000-07:002015-11-17T16:27:21.555-08:00Multiplication Without Using '*' Operator<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-bkwJR-36VcI/VkvFzY-efqI/AAAAAAAAAV4/LbWQccTdCeE/s1600/C%252B%252B%2Bprogram%2Bfor%2Bmultiplication%2Bwithout%2Busing%2Bthe%2Boperator.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="C++ program for Multiplication without using the * operator" border="0" height="320" src="http://1.bp.blogspot.com/-bkwJR-36VcI/VkvFzY-efqI/AAAAAAAAAV4/LbWQccTdCeE/s400/C%252B%252B%2Bprogram%2Bfor%2Bmultiplication%2Bwithout%2Busing%2Bthe%2Boperator.jpg" title="Multiplication without using the * operator" width="400" /></a></div>
<br />
<br />
Multiplication can be implemented without using the multiplication operator which is asterisks ( * ). This can be done through addition since multiplication is a series of addition.<br />
In the code below, the first number is used as loop counter as to how many times the second number is added to the product variable initialized to 0.<br />
<br />
<!--INFOLINKS_OFF-->
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green;">/*</span>
<span style="color: green;">Multiplication without using * operator</span>
<span style="color: green;">*/</span>
<span style="color: blue;">#include <iostream></span>
<span style="color: blue;">using</span> <span style="color: blue;">namespace</span> std;
<span style="color: #2b91af;">int</span> main(<span style="color: #2b91af;">void</span>)
{
<span style="color: green;">//variables</span>
<span style="color: #2b91af;">int</span> number1, number2, product;
<span style="color: #2b91af;">char</span> stay;
cout << <span style="color: #a31515;">"Input first number: "</span>;
cin >> number1;
cout << endl << <span style="color: #a31515;">"Input second number: "</span>;
cin >> number2;
cout <<endl << number1 << <span style="color: #a31515;">" * "</span> << number2 << <span style="color: #a31515;">" = "</span>;
product = 0;
<span style="color: green;">// choose either of the two numbers for the addition</span>
<span style="color: green;">// choose the other number as a counter</span>
<span style="color: blue;">while</span> (number2 > 0)
{
<span style="color: green;">//add the number</span>
product += number1;
<span style="color: green;">//decrement counter</span>
number2--;
}
cout << product <<endl;
<span style="color: green;">//wait for an input before exit </span>
cin >> stay;
<span style="color: blue;">return</span> 0;
}
</pre>
</div>
<!--INFOLINKS_ON-->Vinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com1tag:blogger.com,1999:blog-921491940328660144.post-49014406630846764492011-06-23T06:18:00.000-07:002015-11-17T16:31:35.858-08:00If, Else-If and Else Statements<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-_FUhyXgxChE/VkvGzJEm-YI/AAAAAAAAAWA/0DpU3BVlvMI/s1600/C%252B%252B%2Bprogram%2Bfor%2Bif%252C%2Bif%2Belse%2Band%2Belse%2Bstatements.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="C++ program for if, if-else and else statements" border="0" height="320" src="http://3.bp.blogspot.com/-_FUhyXgxChE/VkvGzJEm-YI/AAAAAAAAAWA/0DpU3BVlvMI/s400/C%252B%252B%2Bprogram%2Bfor%2Bif%252C%2Bif%2Belse%2Band%2Belse%2Bstatements.jpg" title="If, if-else and else statements" width="400" /></a></div>
<br />
<br />
Let me present to you a program using the selection statements: if, if-else and else statements. These kind of statements are used when there's a condition or set of conditions that needs to be evaluated first in order to proceed and execute certain statements within a program.<br />
<br />
<!--INFOLINKS_OFF-->
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green;">/*</span>
<span style="color: green;">If, Else-If and Else Selection</span>
<span style="color: green;">*/</span>
<span style="color: blue;">#include <iostream></span>
<span style="color: blue;">using</span> <span style="color: blue;">namespace</span> std;
<span style="color: #2b91af;">int</span> main(<span style="color: #2b91af;">void</span>)
{
<span style="color: #2b91af;">int</span> age;
<span style="color: #2b91af;">char</span> pause;
cout << <span style="color: #a31515;">"Enter your age: "</span>;
cin >> age;
<span style="color: green;">//check the age entered </span>
<span style="color: blue;">if</span> (age == 18)
{
cout << <span style="color: #a31515;">"You are 18 years old!"</span> << endl;
}
<span style="color: blue;">else</span> <span style="color: blue;">if</span> (age > 18)
{
cout << <span style="color: #a31515;">"You are older than an 18 year old!"</span> << endl;
}
<span style="color: blue;">else</span>
{
cout << <span style="color: #a31515;">"You are below 18 years old!"</span> << endl;
}
cin >> pause;
<span style="color: blue;">return</span> 0;
}
</pre>
</div>
<!--INFOLINKS_ON-->
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
Selection statements are very essential and are commonly used within a program because they provide a very important way of implementing an algorithm. They are used for choosing which statements are to be executed or what will the program do or behave within certain sets of conditions.<br />
<br />
<br />
<br />Vinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com2tag:blogger.com,1999:blog-921491940328660144.post-79676420881018808592011-06-23T02:56:00.000-07:002015-11-17T16:34:27.920-08:00Mathematical Calculations ( MDAS )<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-atIbIb-ezqg/VkvHe6kWf7I/AAAAAAAAAWI/Y4FMK1Aa_vk/s1600/C%252B%252B%2Bprogram%2Bmathematical%2Bcalculations%2BMDAS.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="C++ program for Mathematical calculations (MDAS)" border="0" height="320" src="http://3.bp.blogspot.com/-atIbIb-ezqg/VkvHe6kWf7I/AAAAAAAAAWI/Y4FMK1Aa_vk/s400/C%252B%252B%2Bprogram%2Bmathematical%2Bcalculations%2BMDAS.jpg" title="Mathematical calculations (MDAS)" width="400" /></a></div>
<br />
<br />
This is a simple console application program which shows how we can do simple mathematical calculations like addition, subtraction, multiplication, division and getting the remainder using C++ programming language.<br />
<br />
<!--INFOLINKS_OFF-->
<!-- HTML generated using hilite.me -->
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green;">/*</span>
<span style="color: green;">Basic Mathematics program</span>
<span style="color: green;">*/</span>
<span style="color: blue;">#include <iostream></span>
<span style="color: blue;">using</span> <span style="color: blue;">namespace</span> std;
<span style="color: #2b91af;">int</span> main(<span style="color: #2b91af;">void</span>)
{
<span style="color: green;">//variables</span>
<span style="color: #2b91af;">int</span> number1, number2, sum, difference, product, quotient, remainder;
<span style="color: #2b91af;">char</span> pause;
cout << <span style="color: #a31515;">"Enter first number = "</span>;
cin >> number1;
cout << <span style="color: #a31515;">"Enter second number = "</span>;
cin >> number2;
sum = number1 + number2;
product = number1 * number2;
<span style="color: green;">//consider first which number is bigger</span>
<span style="color: blue;">if</span> (number1 > number2)
{
difference = number1 - number2;
quotient = number1 / number2;
remainder = number1 % number2;
}
<span style="color: blue;">else</span>
{
difference = number2 - number1;
quotient = number2 / number1;
remainder = number2 % number1;
}
cout << endl
<< <span style="color: #a31515;">"Addition = "</span> << sum << endl
<< <span style="color: #a31515;">"Difference = "</span> << difference << endl
<< <span style="color: #a31515;">"Product = "</span> << product << endl
<< <span style="color: #a31515;">"Quotient = "</span> << quotient << endl
<< <span style="color: #a31515;">"Remainder = "</span> << remainder << endl;
<span style="color: green;">//don't close windows console yet</span>
cin >> pause;
<span style="color: blue;">return</span> 0;
}
</pre>
</div>
<!--INFOLINKS_ON-->
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
Before we proceed, noticed that instead of always using<br />
<br />
<span class="Apple-style-span" style="color: blue;">std::cin, std::cout </span>and<span class="Apple-style-span" style="color: blue;"> std::endl</span><br />
<br />
as in our previous examples, now, each of them is already declared once through the <b><span class="Apple-style-span" style="color: blue;">using </span>directive</b> before (outside) the main function like<br />
<br />
<span class="Apple-style-span" style="color: blue;">using </span>std::cin;<br />
<br />
however what we did here is specific to using only cin,cout and endl. The purpose is to include only what are specifically used objects which are cin, cout and endl of the namespace std.<br />
However, we could also have it this way:<br />
<br />
<span class="Apple-style-span" style="color: blue;">using namespace</span> std; <br />
<br />
which uses the whole std namespace that contains cin, cout and endl. Either way you have the choice whatever implementation you like.<br />
<br />
<span class="Apple-style-span" style="color: blue;"><b><span class="Apple-style-span" style="background-color: white;"><u>THE ALGORITHM</u></span></b> : </span><br />
Now, let's take a look at the program, we can see that the user is informed to enter two numbers then the sum and product are calculated first. Then for the difference, quotient and the remainder, we first considered which of the two number has greater value so that we could get a positive result for the difference and for their quotient and remainder through the use of the if-else statement.<br />
<br />
The operators we used:<br />
<br />
+ //used for addition<br />
- //used for subtraction<br />
* //used for multiplication<br />
/ //used for division<br />
% //modulus operation used to get the remainder of two numbers dividedVinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com1tag:blogger.com,1999:blog-921491940328660144.post-83616657275460731672011-06-20T07:30:00.000-07:002015-11-17T16:36:27.317-08:00Add and Multiply 2 Numbers<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-X4LAflRCTHY/VkvH9Csv1TI/AAAAAAAAAWU/Cb-kUPO83K4/s1600/C%252B%252B%2Bprogram%2Bto%2Badd%2Band%2Bmultiply%2Btwo%2Bnumbers.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="C++ program to Add and Multiply two numbers" border="0" height="320" src="http://3.bp.blogspot.com/-X4LAflRCTHY/VkvH9Csv1TI/AAAAAAAAAWU/Cb-kUPO83K4/s400/C%252B%252B%2Bprogram%2Bto%2Badd%2Band%2Bmultiply%2Btwo%2Bnumbers.jpg" title="Add and Multiply two numbers" width="400" /></a></div>
<br />
<br />
Let me show you a simple C++ console application program which will add two numbers and show their sum and product.<br />
<br />
<!--INFOLINKS_OFF-->
<!-- HTML generated using hilite.me -->
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green;">/*</span>
<span style="color: green;">Addition and Multiplication of two numbers</span>
<span style="color: green;">*/</span>
<span style="color: blue;">#include <iostream></span>
<span style="color: blue;">using</span> <span style="color: blue;">namespace</span> std;
<span style="color: #2b91af;">int</span> main(<span style="color: #2b91af;">void</span>)
{
<span style="color: green;">//variables</span>
<span style="color: #2b91af;">int</span> number1, number2, sum, product;
<span style="color: #2b91af;">char</span> stay;
cout << <span style="color: #a31515;">"Input first number: "</span>;
cin >> number1;
cout << endl << <span style="color: #a31515;">"Input second number: "</span>;
cin >> number2;
<span style="color: green;">// add the two numbers</span>
sum = number1 + number2;
cout << endl << <span style="color: #a31515;">"The sum is "</span> << sum <<endl;
<span style="color: green;">// multiply the two numbers directly within cout</span>
cout << endl << <span style="color: #a31515;">"The product is "</span> << number1 * number2 << endl;
<span style="color: green;">//let console wait for an input before exit </span>
cin >> stay;
<span style="color: blue;">return</span> 0;
}
</pre>
</div>
<!--INFOLINKS_ON-->
<br />
<div style="color: #3d85c6;">
<br /></div>
Now let us go through the implementation. As we have seen we defined 4 variables:<br />
<ol>
<li>number1 and number2 - to hold the values of the two numbers</li>
<li>sum - to hold the value of the sum of number1 and number2</li>
<li>stay - to let the program ask for an input before the console window closes. </li>
</ol>
Now maybe you would notice that I did not defined a variable which will hold the value of the product of these two numbers. The answer is because I would like you to know or notice that aside from saving the product into another variable like what we did with the sum.<br />
<br />
We can also directly process the calculation within cout and have it processed directly for output. But this would depend on the implementation the program would require and for this one this can just be applied as what we wanted was directly print the results of multiplying two numbers.<br />
<br />
Output:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-xmKIrVjddYQ/UifGSX4vNkI/AAAAAAAAAOM/UIfMvOhmiA0/s1600/Add+mutiply+numbers.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-xmKIrVjddYQ/UifGSX4vNkI/AAAAAAAAAOM/UIfMvOhmiA0/s1600/Add+mutiply+numbers.jpg" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
Vinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com1tag:blogger.com,1999:blog-921491940328660144.post-86045480296075726082011-06-20T07:22:00.000-07:002015-11-17T16:47:37.021-08:00Hello World!<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-nA3om8h3zEE/VkvKcsYkYyI/AAAAAAAAAWg/jon366nRgTY/s1600/C%252B%252B%2Bfirst%2Bprogram%2BHello%2Bworld.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Hello World! Our first C++ program" border="0" height="320" src="http://1.bp.blogspot.com/-nA3om8h3zEE/VkvKcsYkYyI/AAAAAAAAAWg/jon366nRgTY/s400/C%252B%252B%2Bfirst%2Bprogram%2BHello%2Bworld.jpg" title="Hello World!" width="400" /></a></div>
<br />
<br />
The best way to start learning C++ is to write our first program - Hello World. We will present the basic parts of a running program.<br />
<div>
<br /></div>
<div>
<!--INFOLINKS_OFF-->
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green;">/*</span>
<span style="color: green;">Our first C++ program: Hello World</span>
<span style="color: green;">Print "Hello World!"</span>
<span style="color: green;">*/</span>
<span style="color: blue;">#include <iostream></span>
<span style="color: blue;">using</span> <span style="color: blue;">namespace</span> std;
<span style="color: #2b91af;">int</span> main(<span style="color: #2b91af;">void</span>)
{
<span style="color: green;">//print "Hello World!"</span>
cout << <span style="color: #a31515;">"Hello World!"</span> << endl;
getchar();
<span style="color: blue;">return</span> 0;
}
</pre>
</div>
<!--INFOLINKS_ON-->
</div>
<div>
<br />
<a name='more'></a><br />
<br />
<b><u>I. Comments:</u></b><br />
<b><u><br /></u></b>
<br />
<pre style="line-height: 16px;"><span style="background-color: #0f140f; color: #008800; font-style: italic;">/*</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">Our first C++ program: Hello World</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">Print "Hello World!"</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">*/</span></pre>
<pre style="line-height: 16px;"><pre><span style="background-color: #0f140f; color: #008800; font-style: italic;">//print "Hello World!"</span></pre>
</pre>
Lines 1-4 denotes a comment multiline comment (enclosed inside /* */ symbols ) while Line 12 shows a single line comment (starts with // symbol). Although a program runs without a comment, it is very important to have them as documentation and guide. Comments will be very helpful in documentation and tracing especially for complex programs and software project. Some of its uses are to document what a program is all about, what a function does and what a certain variable is intended for.<br />
<br />
<b><u>II. The preprocessor directive:</u></b><br />
<b><u><br /></u></b>
<span style="background-color: white; color: #ff0007; font-style: italic; font-weight: bold; line-height: 16px;">#include <iostream></span></div>
<div>
<br />
The line which starts with a hash sign (#) are the preprocessor directives. They are indication for the compilers' preprocessor to include the iostream standard file. The iostream standard file specifically contains the basic standard input and output library of C++. They are other files which can be included in a program which depends if it is used within that program. In this program, iostream is included because we are using "cout", the standard output stream which is declared in iostream.<br />
<br />
<b><u>III. Namespace:</u></b><br />
<br />
<pre style="line-height: 16px;"><span style="background-color: black;"><span style="color: #fb660a; font-weight: bold;">using</span> <span style="color: #fb660a; font-weight: bold;">namespace</span> <span style="color: white;">std;</span></span></pre>
<br />
This part is the declaration of usage of a namespace. C++ standard library elements are declared within a namespace called std.<br />
<br />
<u><b>IV. Main function:</b></u><br />
<br />
<pre style="line-height: 16px;"><span style="background-color: black;"><span style="color: #cdcaa9; font-weight: bold;">int</span> <span style="color: #ff0086; font-weight: bold;">main</span><span style="color: white;">(</span><span style="color: #cdcaa9; font-weight: bold;">void</span><span style="color: white;">){}</span></span></pre>
<br />
This is main function. All C++ programs starts executing with this function. Where ever this function is placed, even if it is in between other functions, a C++ program will always start with this function. Inside the curly brackets are all contained with the main function.<br />
<div>
The declaration indicates that it returns an int (integer) value and accept no arguments (void).</div>
<div>
<br /></div>
<div>
<u><b>VI. C++ Statements:</b></u></div>
<div>
<div>
<br /></div>
<div>
<pre style="line-height: 16px;"><span style="background-color: black;"><span style="color: white;">cout</span> <span style="color: white;"><<</span> <span style="color: #0086d2;">"Hello World!"</span> <span style="color: white;"><<</span> <span style="color: white;">endl;</span></span>
</pre>
</div>
<div>
<span style="color: white;"><br /></span></div>
<div>
This is an example of a statement. A statement is an executable unit within a program that does something. The statement above indicates to print the string Hello World! into the screen through the use of "cout" which inserts a sequence of characters into the standard C++ output stream." endl" indicates a new line so the cursor will be position in the next line.</div>
</div>
<div>
<br /></div>
<div>
<pre style="line-height: 16px;"><span style="background-color: black; color: white;">getchar();</span></pre>
</div>
<div>
<br /></div>
<div>
<div>
This line will ask for a character input. You can use this function when you want to ask for a character input and process the character. But in here, it is added just to ask a character input and let the screen stay before the output window will close. </div>
<div>
<br /></div>
<div>
<pre style="line-height: 16px;"><span style="background-color: black;"><span style="color: #fb660a; font-weight: bold;">return</span> <span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">;</span></span></pre>
</div>
<div>
<br /></div>
<div>
This statement indicates the return value of the main function as indicated in its declaration that it will return an int (integer) value, in this case, 0. </div>
<div>
<br /></div>
<div>
Check out the output of the Hello World program:</div>
</div>
<div>
<pre style="line-height: 16px;"><a href="http://1.bp.blogspot.com/-4ORiWdqwBV0/UiUZMuEQ50I/AAAAAAAAAN8/BR2kdInXQIM/s1600/C+++Hello+World.png" imageanchor="1" style="clear: left; display: inline !important; font-family: 'Times New Roman'; margin-bottom: 1em; margin-right: 1em; text-align: center; white-space: normal;"><img border="0" height="393" src="http://1.bp.blogspot.com/-4ORiWdqwBV0/UiUZMuEQ50I/AAAAAAAAAN8/BR2kdInXQIM/s640/C+++Hello+World.png" width="640" /></a></pre>
</div>
<br />
<br /></div>
Vinsterhttp://www.blogger.com/profile/00316054785887321051noreply@blogger.com1