用C语言编写猜数字的程序
时间: 2023-05-24 21:02:35 浏览: 50
A:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int num, guess, attempts = 0;
srand(time(NULL));
num = rand() % 101; // 生成 0~100 的随机数
printf("我想了一个0~100之间的数字,你来猜猜吧!\n");
// 猜数字循环
do {
printf("请输入你猜测的数字:");
scanf("%d", &guess);
attempts++;
if (guess > num) {
printf("太大了!\n");
} else if (guess < num) {
printf("太小了!\n");
}
} while (guess != num); // 猜中时跳出循环
printf("恭喜你!你猜对了!\n");
printf("你共猜了 %d 次\n", attempts);
return 0;
}
相关问题
C语言编写比较数字大小的程序
你好,可以回答你的问题。C语言可以使用比较运算符(>、<、>=、<=、==、!=)来比较数字的大小,例如:if(a > b) { // a大于b的情况 } else if(a < b) { // a小于b的情况 } else { // a等于b的情况 }。希望能够帮到你。
C语言编写程序数字等腰三角形图案
Sure, I can definitely help you with that! Here's some C code for a program that prints an equilateral triangle made of numbers:
```
#include <stdio.h>
int main()
{
int rows, i, j, number = 1;
printf("Enter the number of rows you want to print: ");
scanf("%d", &rows);
for(i=1; i<=rows; i++)
{
for(j=1; j<=i; j++)
{
printf("%d ", number);
number++;
}
printf("\n");
}
return 0;
}
```
I hope you find this helpful! Is there anything else you'd like me to help you with?