#WSQ09 – Fahnniest with numbahs

Yes. I know, the “fun/funnier/funniest” pun, It’s done, get over it.

Today’s practice is about factorials. In math, these are expressed as “n!” and the expression equivalent to it is n! = (n)*(n-1)*(n-2)… and so on, until 1. for example 3! = (3)(2)(1) = 6, 4! = (4)(3)(2)(1) = 24, and 5! = (5)(4)(3)(2)(1) = 120. 

So… As you might expect, our code needs to take an input an produce it’s factorial as an output. We can do this with a loop… or recursively, and yes, I’ll go over both.

Factorial using loops is kind of the easy way and… It works best for your computer, let’s just say you’ll save your computer a lot of resources. Plus, we kind of already did this with loops, let’s open up wsq07, remove one of the limits and rename the other one as the user’s input, replace the initial value of c with 1 instead of 0, and inside the loop replace the addition with multiplication. Like this :

————————————————————————–

Editor :

Screen Shot 2016-02-14 at 6.37.11 PM

Terminal :

Screen Shot 2016-02-14 at 6.42.39 PM

Screen Shot 2016-02-14 at 6.42.48 PM

————————————————————————–

Great! it works. What pass does is literally nothing, we just don’t want i to multiply c when it equals 0, range(1, a+1) would work as well. What if we want to use a while loop instead of a for?

Well, we’d have to use c as a counter instead of a printable variable and use the input variable as the printable variable :

————————————————————————–

Editor :

Screen Shot 2016-02-14 at 7.22.39 PM

Terminal :

Screen Shot 2016-02-14 at 7.24.03 PM

————————————————————————–

c -= 1 is the same as c = c -1. We use c > 1 as conditional instead of c > 0, because when c = 1, then the loop’s output would be 0. 

Our time is now. Actually… not our time, it’s more like… recursion time. Python supports recursion, this

Screen Shot 2016-02-14 at 7.40.31 PM
Screen Shot 2016-02-14 at 7.40.12 PM
Screen Shot 2016-02-14 at 7.54.22 PM
Screen Shot 2016-02-14 at 7.54.02 PM

Continue reading “#WSQ09 – Fahnniest with numbahs”

WSQ09 Factorial

In this program we have to make a loop in which we find the factorial number of the one ingress by the user. Then we have to ask if he wants to ingress another number to calculate the factorial.

The factorial function (symbol: !) means to multiply a series of descending natural numbers. Example:

  • 4! = 4 × 3 × 2 × 1 = 24

I´m going to use a For loop to make the factorial inside a Do-While loop to ask if he wants another number.

Captura de pantalla 2016-02-12 a las 9.44.23 a.m.

Captura de pantalla 2016-02-12 a las 9.44.43 a.m.

The code will be here at GitHub

Greetings!

BRk4Gd2_700wa_0

 

TC101

WSQ09

Factorial calculator

The image above is what my computer outputs when I run this program, a factorial calculator. 

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++, you have to use a loop or recursion. On my first try of this program I decided to use a For Loop. I´ll try to do it this weekend with recursion.

For this program to work, I created a function called “facto” with one parameter. The parameter is the number you want to get the factorial from.

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.

int f, n, op =1;
char answer = ‘y’;

int facto(int x){

for (n = 1; n <= x; n++){

op = op * n;
}

return op;
}

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.

Here you can have a look at my program:

WSQ09

And here is the code:

#include <iostream>
using namespace std;

int f, n, op =1;
char answer = ‘y’;

int facto(int x){

for (n = 1; n <= x; n++){

op = op * n;
}

return op;
}

int main(){

do {
cout << “Enter the number that you want to obtain the factorial from: ” << endl;

Continue reading “Factorial calculator”

WSQ09 factorial

Muy similar al WSQ07, tanto que abrí el wsq07 y practicamente solo le cambié la operación… y lo guardé (por mensa) como WSQ07 :CC byebye wsq07

En fin, opino que los While son muy divertidos 😀

 

NOTAS: Como es factorial, obviamente son multiplicaciones, por lo tanto igual se tiene que tener un acumulador yyyyyyyyyyy no debe estar igualado a cero(obvio el porqué).

qué más mari, qué mas…

El while debe estar al principio de todo para que mientras la orden no sea detener el programa lo corra una y otra y otra vez: se tendrán dos respuestas a la pregunta “quieres continuar?” “si ” “no”.. yo lo pongo como un 1 o 2. “1= sí” , por ésta razón el programa tiene que iniciar con un sí* antes del while. para que se pueda iniciar.

Me gusta hacer las cosas lindas -w- así que imprimí toda la serie, no solo el resultado. C:

wsq09

Fact or Real (yes, it means the same)

Photograph credit

I know, dumb title. I couldn’t come up with anything else and I really did not feel like naming this blog “Factorial”. So it is what it is. Ok This is the last WSQ until now, if any of you guys have not done this program well I hope this helps. I tried to make my to tutorial short and simple to understand.

code:

Factorial.cpp

FactorialCalculator.cpp

If you have any questions, just ask, I’m happy to help. My name is Orlando Lara and I’m in Ken’s TC1017 class at 10:00 Tuesdays and Fridays. Here are some links to help you out:

http://www.cplusplus.com/doc/tutorial/introduction/

http://www.tutorialspoint.com/cplusplus/cpp_templates.htm

In my tutorial I show you how to do the factorial with a loop and function but you can also do it with recursion. Here is a very useful video about that.

Remember that a program can be as complicated as you want. In this tutorial I just show the basics of how it works and one way of successfully making the program but this is too simple. You have to make it more elaborate. Which is why a gave you two links of codes at the beginning of the blog. The first one is the one that appears in the tutorial and the second one is my original code in case you want to take a look at it and get some ideas.

Made by Orlando Lara

Factorial Calculator

Hello everyone,

As you remember I was working on some WHILE LOOPS codes. Now is not the exception.

Instructions:

Create a program that asks the user for a non-negative integer (let’s call that number n) and display for them the value of n! (n factorial).

After showing them the answer, ask them if they would like to try another number (with a simple y/n response) and either ask again (for y) or quit the program and wish them a nice day (if they answered n).

And this is the code:

2016-02-09 (1)

 

We are using two WHILE LOOPS. Why?

  1. The first one is for the answer of the factorial. Here we are using a logical process so be careful and aplpy the data I showed to you in the last WSQ.
  2. The second WHILE LOOP is because we want to question the user if he/she want to type another number and when he/she type N the loop is going to end. (IT´S IMPORTANT TO DECLARE THE COUNTER AND ACUMULATOR INSIDE THE TOP OF THE FIRST LOOP BECAUSE IF YOU DIDN’T THE LOOP IS GOING TO SAVE THE LAST VARIABLES’S VALUES.

The rest is just COUT. It is not difficult so you just need to practice and you can be an expert in this topic. This time I didn’t consult any info because I knew how to made it I just think logically and resolve the code.

Here is my code.

See you in my next post!

Top image by: https://www.flickr.com/photos/franganillo/3676227162/in/photolist-6ARCfW-3KUEYX-r12qc4-8EQHGq-2m2qz-bjMTNU-qDYNjU-BTs6PY-hFghFE-9kMCpd-biaBRX-5479uK-CXS95v-7vBmWZ-9Pcixd-8uiEZh-biaCV6-cGfaG5-57R2QV-bnZLkt-62kgKT-shxyY9-ucnpR-4H1juM-6yvjme-jusv8v-tpkfA-bkJRub-9VwGaa-4nffv9-nhpFZA-u1sWi-qDd1aC-rrLy7G-9VCot1-5WKb9m-f3MmR6-8LFbhE-5Xcyed-gumt7v-2WdaSy-FP86B-9biQY6-9Ajs9g-bgfFKc-o95B4i-7ASToy-8nuB5W-6DtkQL-98yw4q

#WSQ09

#include <iostream>

using namespace std;
int factorial(int n1){
int r=1;
for (int c=n1; c>0; c–){
r=r*c;
}
return (r);
}

int main(){
int n;
char ans;
do{
cout << “Give me a non negative integer to give you its factorial: ” << endl;
cin >> n ;
cout << “The factorial of that number is : ” << factorial(n) << endl;
cout << “Do you want to know the factorial of another number? y/n ” << endl;
cin >> ans;
}while(ans==’y’);

cout << “Ok, have a nice day.” << endl;

return 0;

}

#WSQ09 Factorial Calculator

In case you might need it, here is the code

And I used this web page to help me out: http://fahad-cprogramming.blogspot.mx/2013/02/program-to-find-factorial-in-C-Programming.html

 

Well now to begin with this awsome factorial calculator we shall start as we had before

#include<iostream>

using namespace std;

Then we show that the numbers we will be using are “int” for num, factorial, else we could use “float” but “int” is the one we choose for this program.

So we ask for the number we will be getting the factorial

Screenshot8.1

Then we proceed to show what “a” would be in order to make this factorial calculator work a=1;a<=num;a++ sayin that a would be 1 until it’s lower or equal to the number you introduce at the beginning, and saying that a would increase 1 each time until it’s equal to your first number

In order to get the factorial the formula we would be using is ” factorial=factorial*a;  ” showing that the factorial will multiply “a” in order to get the right factorial number.

Screenshot8.2.png

Finally you will be giving out the result of the factorial of the number the user wrote in, and never forget ” return 0; ” otherwise your program might not work.

 

 

Factorial Calculator

This program asks the user for a non-negative integer (let’s call that number n) and display for them the value of n! (n factorial).

After showing them the answer, ask them if they would like to try another number (with a simple y/n response) and either ask again (for y) or quit the program and wish them a nice day (if they answered n).

For factorial value we use cilcle for and a do-while for the question and to return.

WSQ09