{"id":31093,"date":"2016-02-12T09:25:00","date_gmt":"2016-02-12T15:25:00","guid":{"rendered":"http:\/\/netosanchezb.wordpress.com\/?p=267"},"modified":"2016-02-12T09:28:38","modified_gmt":"2016-02-12T15:28:38","slug":"factorial-calculator-5","status":"publish","type":"post","link":"https:\/\/kenscourses.com\/tc101winter2016\/2016\/02\/factorial-calculator-5\/","title":{"rendered":"Factorial calculator"},"content":{"rendered":"<p><strong>The image above is what my computer outputs when I run this program, a factorial calculator.\u00a0<\/strong><\/p>\n<p>It is a basic factorial calculator. The factorial of a number, lets say of number 5 (written 5!) is the multiplication of 5*4*3*2*1. The result of that operation would be 120.<\/p>\n<p>In order to do this operation in C++, you have to use a loop or recursion. On my first try of this program I decided to use a For Loop. I\u00b4ll try to do it this weekend with recursion.<\/p>\n<p>For this program to work, I created a function called &#8220;facto&#8221; with one parameter. The parameter is the number you want to get the factorial from.<\/p>\n<p>So I declared an int variable called op and initialized it to 1. The operation inside the for loop is op = op * n, which is a number between 1 and a number less or equal to the required by the user, as you can see in my for loop.<\/p>\n<p><strong>int f, n, op =1;<\/strong><br \/>\n<strong>char answer = &#8216;y&#8217;;<\/strong><\/p>\n<p><strong>int facto(int x){<\/strong><\/p>\n<p><strong>for (n = 1; n &lt;= x; n++){<\/strong><\/p>\n<p><strong>op = op * n;<\/strong><br \/>\n<strong> }<\/strong><\/p>\n<p><strong>return op;<\/strong><br \/>\n<strong>}<\/strong><\/p>\n<p>A cool thing about this program is that it asks the user if he wants to repeat the process with another number, or if he wnats to end the program. I achieved this doing a do while loop.<\/p>\n<p>Here you can have a look at my program:<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-268\" src=\"http:\/\/kenscourses.com\/tc101winter2016\/wp-content\/uploads\/2016\/02\/d45caebec389e0c1c82237562976f915.jpg\" alt=\"WSQ09\"   \/><\/p>\n<p>And here is the code:<\/p>\n<p><strong>#include &lt;iostream&gt;<\/strong><br \/>\n<strong>using namespace std;<\/strong><\/p>\n<p><strong>int f, n, op =1;<\/strong><br \/>\n<strong>char answer = &#8216;y&#8217;;<\/strong><\/p>\n<p><strong>int facto(int x){<\/strong><\/p>\n<p><strong>for (n = 1; n &lt;= x; n++){<\/strong><\/p>\n<p><strong>op = op * n;<\/strong><br \/>\n<strong> }<\/strong><\/p>\n<p><strong>return op;<\/strong><br \/>\n<strong>}<\/strong><\/p>\n<p><strong>int main(){<\/strong><\/p>\n<p><strong>do {<\/strong><br \/>\n<strong> cout &lt;&lt; &#8220;Enter the number that you want to obtain the factorial from: &#8221; &lt;&lt; endl;<\/strong><br \/>\n<strong> <\/p>\n<div class=\"post-limited-image\"><img decoding=\"async\" alt=\"\" border=\"0\" src=\"http:\/\/kenscourses.com\/tc101winter2016\/wp-content\/uploads\/2016\/02\/67473829388d9ff97aa2d5473cb77bf9.jpg\" \/><\/div>\n<div class=\"post-limited-image\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" border=\"0\" src=\"http:\/\/kenscourses.com\/tc101winter2016\/wp-content\/uploads\/2016\/02\/74e0961471fea4e722a7e10a4c02450c.jpg#038;blog=105214593&#038;%23038;post=267&#038;%23038;subd=netosanchezb&#038;%23038;ref=&#038;%23038;feed=1\" width=\"1\" height=\"1\" \/><\/div>\n<p><!--more--> &gt;&gt; f;<\/strong><br \/>\n<strong> cout &lt;&lt; &#8220;The factorial of &#8221; &lt;&lt; f &lt;&lt; &#8220;is equal to: &#8221; &lt;&lt; facto(f) &lt;&lt; endl;<\/strong><br \/>\n<strong> cout &lt;&lt; &#8220;Do you want to try another number? (type y if you do, type anything else if not): &#8221; &lt;&lt; endl;<\/strong><br \/>\n<strong> cin &gt;&gt; answer;<\/strong><\/p>\n<p><strong>} while (answer == &#8216;y&#8217;);<\/strong><\/p>\n<p><strong>cout &lt;&lt; &#8220;Thanks for using my program!&#8221;;<\/strong><br \/>\n<strong>}<\/strong><\/p>\n<p>Thanks for reading!<\/p>\n<p>  <a rel=\"nofollow\" href=\"http:\/\/feeds.wordpress.com\/1.0\/gocomments\/netosanchezb.wordpress.com\/267\/\"><img decoding=\"async\" alt=\"\" border=\"0\" src=\"http:\/\/kenscourses.com\/tc101winter2016\/wp-content\/uploads\/2016\/02\/67473829388d9ff97aa2d5473cb77bf9.jpg\" \/><\/a> <img loading=\"lazy\" decoding=\"async\" alt=\"\" border=\"0\" src=\"http:\/\/kenscourses.com\/tc101winter2016\/wp-content\/uploads\/2016\/02\/74e0961471fea4e722a7e10a4c02450c.jpg#038;blog=105214593&#038;%23038;post=267&#038;%23038;subd=netosanchezb&#038;%23038;ref=&#038;%23038;feed=1\" width=\"1\" height=\"1\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The image above is what my computer outputs when I run this program, a factorial calculator.\u00a0 It is a basic factorial calculator. The factorial of a number, lets say of number 5 (written 5!) is the multiplication of 5*4*3*2*1. The result of that operation would be 120. In order to do this operation in C++, &hellip; <a href=\"https:\/\/kenscourses.com\/tc101winter2016\/2016\/02\/factorial-calculator-5\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Factorial calculator<\/span><\/a><\/p>\n","protected":false},"author":5,"featured_media":31095,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[68,127],"class_list":["post-31093","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blogs","category-syndicated","tag-sincategoria","tag-wsq09"],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101winter2016\/wp-json\/wp\/v2\/posts\/31093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kenscourses.com\/tc101winter2016\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kenscourses.com\/tc101winter2016\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101winter2016\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101winter2016\/wp-json\/wp\/v2\/comments?post=31093"}],"version-history":[{"count":2,"href":"https:\/\/kenscourses.com\/tc101winter2016\/wp-json\/wp\/v2\/posts\/31093\/revisions"}],"predecessor-version":[{"id":31100,"href":"https:\/\/kenscourses.com\/tc101winter2016\/wp-json\/wp\/v2\/posts\/31093\/revisions\/31100"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101winter2016\/wp-json\/wp\/v2\/media\/31095"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101winter2016\/wp-json\/wp\/v2\/media?parent=31093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101winter2016\/wp-json\/wp\/v2\/categories?post=31093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101winter2016\/wp-json\/wp\/v2\/tags?post=31093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}