! Factorial !

Este este programa muestra el numero factorial del numero que el usuario desee!

para este código, tienes primeramente que preguntarle al usuario el numero factorial deseado, una vez de recibir el numero, entonces se abre un ciclo FOR para ir multiplicando todos los números que se están incrementando hasta el numero que el usuario puso de limite.

sin embargo y para una forma mas estética, insertamos un condicional if-else, para que al momento de mostrar en pantalla la respuesta puedan salir todos los números multiplicados hasta el limite.

ahora por otro lado solo queda preguntarle al usuario que si desea ver el factorial de otro numero, y esperar la respuesta. para resolver esto, es muy sencillo, todo el código debe de estar en un ciclo do-while con la condición de que pueda salir del ciclo hasta que el usuario diga que no quiere que se le muestre otro numero.

Es importante mencionar que para que el usuario responda a esta última pregunta, se debe de declarar una variable con formato string para recibir un “si” o “no”

Para hacer este código necesite la ayuda del profesor Ken Bauer y la de mis compañeros dentro del aula de clases.

el link por esta ocasión lo dejaré de nuevo en Dropbox, los proximos WSQ0’s serán en GitHub.

Captura de pantalla 2016-02-18 22.33.39

Factorial Calculator (WSQ09)

This code will be about creating a factorial calculator and we’ll keep practicing with the “while” loop.

Before I show you how to do the program, let’s review what a factorial is. According to this webpage, isthe product of a given positive integer multiplied by alllesser positive integers: The quantity four factorial (4!) = 4321 =24. Symbol: n!, where n is the given integer.”

Now we’re ready to start programming.

factorial1

First, I’ll make the main code. It’ll make the calculator’s work. As always, we ask the user for the number with an input. Then we convert the variable to an integer so the program is able to use it. The counter is designed with the value “1” just like the R variable.

Is necessary to do this before the while loop because if we just start using variables without specify their initial value, Python won’t recognize them and the program won’t run.

What the loop does is simple, it just multiplies the consecutive numbers from 1 to the number that the user gave us. For that reason the “cont” variable increments one unit each time the code runs. The loop will end when the counter gets bigger than the variable that the user typed.

Up to here, when we execute the program,  the terminal window will show this:

factorial2

And NOOOOW we’ll insert another loop to ask the user if he/she would like to use the program again.

factorial3

You can notice that I added another variable with the “yes” assignation. This means that WHILE the answer to the question in line 11 is “yes” the program will keep running. If the user types something different, the loop will end, and the word “thanks” will be printed.

You can be aware that the

factorial4.png

Continue reading “Factorial Calculator (WSQ09)”

WSQ09 – Factorial Calculator

el código:

#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int num,resul,x,fin;
char sig;
do
{
resul=1;
cout<<“nDamen un numero para hacer factorial: “<<endl;
cin>>num;
fin=num;
for(x=1; x<=fin; x++)
{
resul=resul*x;
}
cout <<“nEl resultado es “<<resul;
cout <<“n¿Quieres otro numero? si=s, no=n”<<endl;
cin>>sig;
}while(sig==’s’);
cout <<“nGracias, que pases buenas noches.”;
getchar();
return 0;
}

n!

To do this WSQ I had to research what a factorial is and how it works, the page that I found useful is THIS ONE.

So the rule is:

n! = n × (n−1)!

Which just says “the factorial of any number is that number times the factorial of (1 smaller than that number)“.

The first attempt was a failure because every single one of the factorials gave me the number 1.

Captura de pantalla 2016-02-17 a las 2.42.19 p.m.

Then I found my mistake and here is the code:

Haga click para ver el pase de diapositivas.

The link of my code ☺

Factorial calculator

In this project the program has to ask if the user want to use de calculator, and if the answer is yes (y), the program will ask the user wich number want to do the factorial number. At last the program ask again if the user want to try again .

Here is the first part of the program in Atom

factorial1

Here is the last part in Atom

factorial11

And here is the Cywing part where it shows how the program works.

factorial

Here you can check out my code on Github

 

#WSQ09, CALCULAR UN FACTORIAL

WSQ09
Este es un programa para calcular el factorial de un numero, donde el contador inicializa en el numero que indique el usuario y seguira mientras sea mayor a cero e ira en decremento de uno en uno. En el main se hace un do-while, porque no sabes cuantas veces se repetirá el ciclo.
factorial
En este caso, el factorial de 5 es 120. Ahí se ve compilado el resultado que nos da es debido a esto: 1x2x3x4x5=120.

CÓDIGO

#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<<“PROGRAMA PARA CALCULAR EL FACTORIAL DE UN NUMERO.”<<endl<<endl;
cout << “Dame un numero para calcular su factorial: ” << endl;
cin >> n ;
cout << “El factorial del numero es: ” << factorial(n) << endl;
cout << “Quieres saber el factorial de otro numero?” << endl;
cin >> ans;
}while(ans==’y’);

cout << “O.K. Ten un buen día” << endl;

return 0;
}

#WSQ09 Factorial Calculator.

logo-factorial

Hello again people!, this post is about my new program “Factorial Calculator”, and as the name says, it calculates the factorial of the number that you choose. But what is a factorial?

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

  • 4! = 4 × 3 × 2 × 1 = 24
  • 7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040
  • 1! = 1

(Information taken from  https://www.mathsisfun.com/numbers/factorial.html).

Here is the code of the program:

fc

As you can see, here we have to use the for loop, and that’s new for me, so we have some problems with it, but finally I learned how to use it and it’s so easy.

The for loop has the next sequence.

for ( the variable that are you going to use as a counter; the condition; and finally the variable with ++ to add 1 when the cycle is repeating)

{

The proccess of the loop

}

It seems like that.

for (int m=1; m<=num1; m++)
{
result=m*result;
}

 

So it was the new thing that i learned with this program, the for loop.

Now, check my program running:

fcgg.png

If you have problems with it, you can see this information:

http://www.tutorialspoint.com/cprogramming/c_for_loop.htm

and if you dont understand clearly, dont feel affraid and ask me!

 

Here my code on GitHub, hope it will be useful for you!.

https://github.com/eduardomrlsg/TC101/blob/master/FactorialCalculator

Regards!

EdM