在C中循环时,如何在阶乘内找到一个数的平方?

在C中循环时,如何在阶乘内找到一个数的平方?,c,visual-c++,factorial,C,Visual C++,Factorial,我正在尝试创建一个程序,不仅计算一个数字的阶乘,而且还显示阶乘小于该数字平方的输出 这些是要求: 输入一个介于3到9之间的数字。 它在输出中显示其阶乘和平方。 如果插入的数字的阶乘小于数字的平方,则输出字符串“Gotcha!” 这是到目前为止我的代码。多谢各位 #pragma once #include <stdio.h> #define MIN 3 #define MAX 9 #define _CRT_SECURE_NO_WARNINGS int main() { int i,

我正在尝试创建一个程序,不仅计算一个数字的阶乘,而且还显示阶乘小于该数字平方的输出

这些是要求: 输入一个介于3到9之间的数字。 它在输出中显示其阶乘和平方。 如果插入的数字的阶乘小于数字的平方,则输出字符串“Gotcha!”

这是到目前为止我的代码。多谢各位

#pragma once
#include <stdio.h>
#define MIN 3
#define MAX 9
#define _CRT_SECURE_NO_WARNINGS

int main()
{
int i, fact = 1, num;
printf("Enter a number:\n");
scanf_s("%d", &num);

while (num >= MIN || num <= MAX)
{
    for (i = 1; i <= num; i++)
    {
        fact = fact*i;
    }
    if (num < MIN || num > MAX)
    {
        printf("Out of range. Please try again.\n");
        scanf_s("%d", &num);
    }
    else
    {
        printf("Factorial of %d is: %d\n", num, fact);
        return 0;
    }
}
}
#pragma一次
#包括
#定义最小值3
#定义最大值9
#定义\u CRT\u安全\u无\u警告
int main()
{
int i,fact=1,num;
printf(“输入一个数字:\n”);
scanf_s(“%d”和&num);

而(num>=MIN | | num只需将数字的平方乘以它本身即可

square = num * num;
printf("Square of %d is: %d\n", num, square);

if (fact < square)
    printf("Gotcha!\n");
square=num*num;
printf(“%d的平方是:%d\n”,num,Square);
如果(事实<平方)
printf(“明白了!\n”);

r.e.务实的方法,我同意博丹格利的观点

r、 你问题背后的理论,让我们再深入一点

编辑:没有深入研究,而不是“从不”,我的回答应该是“基本上从不(对于n=2和n=3,只有两次)

概括一下你的问题: 给定“n”,找出“n的阶乘”是否小于“n的平方”

我们可以总结如下:

n! < n^2
所以问题就变成了:n的什么值是n>(n-1)? (扰流板警报:不经常出现)

让我们看看什么时候n>(n-1)

考虑一下这张桌子

+------------+----+-----+----------+-------------------------+
| n > (n-1)! | n  | n-1 |  (n-1)!  |  notes...               |
+------------+----+-----+----------+-------------------------+
|       no   | 0  |  -1 |  undef   | (-1)! is undefined      |
|       no   | 1  |   0 |     1    | 0! is 1 by definition   |
|      yes   | 2  |   1 |     1    | 1! is 1 by definition   |
|      yes   | 3  |   2 |     2    | ( or 2 * 1 )            |
|       no   | 4  |   3 |     6    | ( or 3 * 2 * 1 )        |
|       no   | 5  |   4 |    24    | ( or 4 * 3 * 2 * 1 )    |
+------------+----+-----+----------+-------------------------+

这意味着n>(n-1)!仅适用于n=2和n=3。

尝试以下方法:

#include <stdio.h>

#define MIN 3
#define MAX 9
#define _CRT_SECURE_NO_WARNINGS

enum {NO_ERRORS, ERROR};
enum {FALSE, TRUE};


int main()
{
    int i, fact = 1, num, valid = FALSE, sqr;

    while(!valid){

        printf("Enter a number:\n");
        scanf("%d", &num);

        if (num < MIN && num > MAX){
            printf("Out of range. Please try again.\n");            
        }else{
            valid = TRUE;                       
        }
    }

    /* get the factorial */
    for (i = 1; i < num; i++){
        fact = fact * i;
    }

    /* get the square */
    sqr = num * num;

    /* output */
    if( fact < sqr){
        printf("%s\n", "Gotcha!");
    }else{
        printf("Factorial of %d is: %d\n", num, fact);
    }

    return NO_ERRORS;
}
#包括
#定义最小值3
#定义最大值9
#定义\u CRT\u安全\u无\u警告
枚举{无错误,错误};
枚举{FALSE,TRUE};
int main()
{
int i,fact=1,num,valid=FALSE,sqr;
while(!valid){
printf(“输入一个数字:\n”);
scanf(“%d”和&num);
如果(numMAX){
printf(“超出范围。请重试。\n”);
}否则{
有效=真;
}
}
/*得到阶乘*/
对于(i=1;i

谢谢

编辑你的源代码并在那里添加标题。谢谢你的解释!我真的很感激。我会把它作为注释:)
+------------+----+-----+----------+-------------------------+
| n > (n-1)! | n  | n-1 |  (n-1)!  |  notes...               |
+------------+----+-----+----------+-------------------------+
|       no   | 0  |  -1 |  undef   | (-1)! is undefined      |
|       no   | 1  |   0 |     1    | 0! is 1 by definition   |
|      yes   | 2  |   1 |     1    | 1! is 1 by definition   |
|      yes   | 3  |   2 |     2    | ( or 2 * 1 )            |
|       no   | 4  |   3 |     6    | ( or 3 * 2 * 1 )        |
|       no   | 5  |   4 |    24    | ( or 4 * 3 * 2 * 1 )    |
+------------+----+-----+----------+-------------------------+
#include <stdio.h>

#define MIN 3
#define MAX 9
#define _CRT_SECURE_NO_WARNINGS

enum {NO_ERRORS, ERROR};
enum {FALSE, TRUE};


int main()
{
    int i, fact = 1, num, valid = FALSE, sqr;

    while(!valid){

        printf("Enter a number:\n");
        scanf("%d", &num);

        if (num < MIN && num > MAX){
            printf("Out of range. Please try again.\n");            
        }else{
            valid = TRUE;                       
        }
    }

    /* get the factorial */
    for (i = 1; i < num; i++){
        fact = fact * i;
    }

    /* get the square */
    sqr = num * num;

    /* output */
    if( fact < sqr){
        printf("%s\n", "Gotcha!");
    }else{
        printf("Factorial of %d is: %d\n", num, fact);
    }

    return NO_ERRORS;
}