1
I already know how to declare a variable in the traditional way using the scanf()
, but I’m thinking of a way that I can use fewer lines and make the code more organized (that’s what I tried with the variable valor
).
It turns out that when I run the program the value 1 is always printed on the screen. Why the value 1 is printed on the screen?
There is a more organized way to use the scanf()
without me using another line (similar to what I’m trying to do)?
#include<stdio.h>
int main()
{
int resultado = 1;
printf("Coloque um valor: ");
int valor = scanf("%d", &valor);
while (valor > 0){
resultado = resultado * valor;
valor = valor - 1;}
printf("O exponencial é: %d\n", resultado);
}
If the problem is the number of lines: Know that you can write everything in one.
– Francisco
The
scanf
returns the number of entries entered in the argument, in your case the"%d"
reads a value so always returns 1. Has here the reference to confirm.– Isac
I just realized that what is being calculated is the factorial, not the exponential... it would be appropriate to correct the wording of the question for those who come here in the future to consult this question and their answers?
– Jefferson Quesado