C 编写一个程序,将用户输入的数字相乘,直到这些数字的乘积达到1000
我已经试了大约一个小时了,但我似乎做不好。怎么做的 我目前掌握的代码是:C 编写一个程序,将用户输入的数字相乘,直到这些数字的乘积达到1000,c,C,我已经试了大约一个小时了,但我似乎做不好。怎么做的 我目前掌握的代码是: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(){ int j=-1; while(j<0){ printf("Enter a number: \n"); scanf("%d", &j); } int i=j; for
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int j=-1;
while(j<0){
printf("Enter a number: \n");
scanf("%d", &j);
}
int i=j;
for(i=j; i<=100; i++){
printf("%d \n", i);
}
return 0;
}
#包括
#包括
#包括
int main(){
int j=-1;
虽然(j原始规范(添加代码之前)有点模糊,但就要遵循的过程而言,这是不相关的。让我们假设它们如下所示:
- 从用户处获取两个数字
- 如果他们的产品大于1000,请打印并停止
- 否则,打印产品并返回到第一个要点
(如果这不是你想要的,那么过程还是一样的,你只需要调整各个步骤)
在开发时,将其转换为伪代码通常是一个好的第一步
def program:
set product to -1
while product <= 1000:
print prompt asking for numbers
get num1 and num2 from user
set product to num1 * num2
print product
print "target reached"
def程序:
将产品设置为-1
虽然product我不是100%清楚你想要什么,所以我假设你想让用户继续输入数字(我假设为正整数),直到所有数字相乘大于或等于1000)
这里的代码以值1开始(因为从0开始意味着它将永远不会得到除0以外的任何值),并将正整数乘以它,而所有正整数的乘积保持在1000以下。最后,它打印总数(可能超过1000)以及用户输入的值的数量
我希望这有帮助
#include <stdio.h>
#include <stdlib.h>
int main()
{
char input[10];
unsigned currentTotal = 1;
unsigned value;
unsigned numEntered = 0;
while( currentTotal < 1000 )
{
printf( "Enter a number: \n" );
fgets( input, sizeof(input), stdin );
value = atoi( input );
if( value > 0 )
{
currentTotal *= value;
numEntered += 1;
}
else
{
printf( "Please enter a positive integer value\n" );
}
}
printf( "You entered %u numbers which when multiplied together equal %u\n", numEntered, currentTotal );
return 0;
}
#包括
#包括
int main()
{
字符输入[10];
无符号currentTotal=1;
无符号值;
无符号numEntered=0;
而(当前总计<1000)
{
printf(“输入一个数字:\n”);
fgets(输入,sizeof(输入),标准输入);
值=atoi(输入);
如果(值>0)
{
currentTotal*=值;
数值+=1;
}
其他的
{
printf(“请输入一个正整数值\n”);
}
}
printf(“您输入了%u个数字,这些数字相乘后等于%u\n”,数字化,currentTotal);
返回0;
}
试试这个:
#include <stdio.h>
int main()
{
int input,output=1;
while(1)
{
scanf("%d",&input);
if(input<=0)
printf("Please enter a positive integer not less than 1 :\n");
else if(input>0)
output*=input;
if(output>1000)
{
printf("\nThe result is: %d",output);
break;
}
}
return 0;
}
#包括
int main()
{
int输入,输出=1;
而(1)
{
scanf(“%d”,输入(&I));
如果(输入0)
输出*=输入;
如果(输出>1000)
{
printf(“\n结果为:%d”,输出);
打破
}
}
返回0;
}
我无法正确回答问题。请清楚地解释您的问题。编写一个程序,将用户输入的数字相乘,直到这些数字的乘积达到1000。问题是输入一个数字,然后让它自己相乘,直到达到1000。这是我写的,但它不对!#include#include#include int main(){int j=-1;而(j@user5435016,这很好,但我的答案中的步骤仍然有效。用伪代码写下来,直到你满意它能做正确的事情,然后将其翻译成C。
int i = 1;
while (i < 1000) {
i = i * j;
printf ("%n\n", i);
}
#include <stdio.h>
int main (void) {
// Get starting point, two or more.
int start = 0;
while (start < 2) {
printf("Enter a number greater than one: ");
if (scanf("%d", &start) != 1) {
// No integer available, clear to end of input line.
for (int ch = 0; ch != '\n'; ch = getchar());
}
}
// Start with one, continue while less than a thousand.
int curr = 1;
while (curr < 1000) {
// Multiply then print.
curr *= start;
printf ("%d\n", curr);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
char input[10];
unsigned currentTotal = 1;
unsigned value;
unsigned numEntered = 0;
while( currentTotal < 1000 )
{
printf( "Enter a number: \n" );
fgets( input, sizeof(input), stdin );
value = atoi( input );
if( value > 0 )
{
currentTotal *= value;
numEntered += 1;
}
else
{
printf( "Please enter a positive integer value\n" );
}
}
printf( "You entered %u numbers which when multiplied together equal %u\n", numEntered, currentTotal );
return 0;
}
#include <stdio.h>
int main()
{
int input,output=1;
while(1)
{
scanf("%d",&input);
if(input<=0)
printf("Please enter a positive integer not less than 1 :\n");
else if(input>0)
output*=input;
if(output>1000)
{
printf("\nThe result is: %d",output);
break;
}
}
return 0;
}