功能问题 [英] problem with functions
问题描述
大家好,如果有一个简单的解决方案,请原谅我。我将通过下面的代码来完成
,这些代码只是计算出一本书中的阶乘代价,但是当我运行它时,无论数字是什么,我得到答案0 >
我输入。有人可以帮忙吗?
提前致谢
严格_mk
#include" stdio.h"
unsigned long long int return_factorial(unsigned int num); //功能
原型
int main(无效){
char输入[3]; //键盘输入
int number; //使用的数字
//提示用户
printf("输入一个正整数,其中阶乘将是
计算:");
fgets(输入,sizeof(输入),stdin); //阅读输入
sscanf(输入,%d,&数字);
//检查输入为有条件的
if(number> 0){
printf("%d的阶乘是%lu。\ n" ,数字,
return_factorial(数字));
}否则{
printf("你必须输入一个正整数!\ n" ;);
}
getchar(); //暂停并等待用户
返回0;
}
//此函数需要一个数字返回其阶乘
unsigned long long int return_factorial(unsigned int num){
unsigned long long int sum = 1;
unsigned int i; //用于计算阶乘的乘数
//遍历每个乘数,包括总和
for(sum = 1, i = 1; i< = num; ++ i){
sum * = i;
}
返还金额;
} // return_factorial函数结束
Hi all, forgive me if there is a simple solution for this. I am going
through the following piece of code which simply calculates factorials
out of a book, but when i run it I get the answer 0 for whatever number
I input. Can anyone help?
Thanks in advance
strictly_mk
#include "stdio.h"
unsigned long long int return_factorial(unsigned int num); //function
prototype
int main (void) {
char input[3]; //keyboard input
int number; // number to work with
//prompt user
printf("Enter a positive integer for which the factorial will be
calculated: ");
fgets (input, sizeof(input), stdin); // read the input
sscanf (input, "%d", &number);
//check the input as conditional
if (number > 0) {
printf ("The factorial of %d is %lu.\n", number,
return_factorial(number));
} else {
printf ("You must enter a positive integer!\n");
}
getchar(); //pause and wait for user
return 0;
}
//This function takes a number and returns its factorial
unsigned long long int return_factorial(unsigned int num) {
unsigned long long int sum = 1;
unsigned int i; //multiplier to be used in calculating factorial
//Loop through every multiplier up to and including sum
for (sum = 1, i = 1; i <= num; ++i) {
sum *= i;
}
return sum;
} // End of return_factorial function
推荐答案
在文章< 11 ********************** @ g10g2000cwb.googlegroups .com> ;,
< st **** *****@hotmail.com>写道:
In article <11**********************@g10g2000cwb.googlegroups .com>,
<st*********@hotmail.com> wrote:
大家好,请原谅我,如果有一个简单的解决方案。我将通过下面的一段代码简单地计算出书中的因子,但是当我运行它时,无论我输入什么数字,我得到答案0。
unsigned long long int return_factorial(unsigned int num);
printf(%d的因子是%lu。\ n,数字,
return_factorial(数字));
Hi all, forgive me if there is a simple solution for this. I am going
through the following piece of code which simply calculates factorials
out of a book, but when i run it I get the answer 0 for whatever number
I input. unsigned long long int return_factorial(unsigned int num); printf ("The factorial of %d is %lu.\n", number,
return_factorial(number));
return_factorial返回一个unsigned long long,然后你用b $ b尝试使用适合
unsigned long的格式说明符进行打印。尝试使用%llu的格式说明符
-
好吧,仅限流行语。两个音节,上衣。 - Laurie Anderson
return_factorial returns an unsigned long long, which you then
attempt to print using a format specifier suitable for an
unsigned long. Try using a format specifier of %llu
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
谢谢,它有效。这就是全部。很抱歉是个傻瓜。
Strictly_mk
Thanks, it worked. Thats all it was. Sorry for being a dumbass.
Strictly_mk
st ********* @ hotmail.com écrit:
大家好,请原谅我,如果有一个简单的话解决方案。我将通过下面的一段代码简单地计算出书中的因子,但是当我运行它时,无论我输入什么数字,我得到答案0。任何人都可以提供帮助吗?
提前致谢
strict_mk
" stdio.h"
unsigned long long int return_factorial( unsigned int num); //功能
原型
int main(void){
char输入[3]; //键盘输入
int号; //使用的数字
//提示用户
printf("输入计算因子的正整数:");
fgets(输入,sizeof(输入),stdin); //阅读输入
sscanf(输入,"%d"& number);
//检查输入为条件
> if(number> 0){
printf("%d的阶乘是%lu。\ n,数字,
return_factorial(数字));
} else {
printf("你必须输入一个正整数!\ n");
getchar(); //暂停并等待用户
返回0;
}
//这个函数需要一个数字并返回它的阶乘
unsigned long long int return_factorial(unsigned int num){
unsigned long long int sum = 1;
unsigned int i; //用于计算阶乘的乘数
//遍历每个乘数,包括总和
(sum = 1,i = 1; i< = num; ++ i){
sum * = i;
}
返回总和;
} // return_factorial结束函数
Hi all, forgive me if there is a simple solution for this. I am going
through the following piece of code which simply calculates factorials
out of a book, but when i run it I get the answer 0 for whatever number
I input. Can anyone help?
Thanks in advance
strictly_mk
#include "stdio.h"
unsigned long long int return_factorial(unsigned int num); //function
prototype
int main (void) {
char input[3]; //keyboard input
int number; // number to work with
//prompt user
printf("Enter a positive integer for which the factorial will be
calculated: ");
fgets (input, sizeof(input), stdin); // read the input
sscanf (input, "%d", &number);
//check the input as conditional
if (number > 0) {
printf ("The factorial of %d is %lu.\n", number,
return_factorial(number));
} else {
printf ("You must enter a positive integer!\n");
}
getchar(); //pause and wait for user
return 0;
}
//This function takes a number and returns its factorial
unsigned long long int return_factorial(unsigned int num) {
unsigned long long int sum = 1;
unsigned int i; //multiplier to be used in calculating factorial
//Loop through every multiplier up to and including sum
for (sum = 1, i = 1; i <= num; ++i) {
sum *= i;
}
return sum;
} // End of return_factorial function
如果我用lcc-win32编译你的代码,它会说:
警告tprintfw.c:23 printf参数格式不匹配。预计
int得到无符号多长
使用%llu
If I compile your code with lcc-win32 it will say:
Warning tprintfw.c: 23 printf argument mismatch for format u. Expected
int got unsigned long long
use %llu
这篇关于功能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!