标签:之间 素数 sys pre info 函数 程序 real 整数
Part1
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
float a, b, c, x1, x2;
float delta, real, imag;
printf("Enter a, b, c: ");
while(scanf("%f%f%f", &a, &b, &c)) {
if(a == 0)
printf("not quadratic equation.\n");
else {
delta = b*b - 4*a*c;
if(delta >= 0) {
x1 = (-b + sqrt(delta)) / (2*a);
x2 = (-b - sqrt(delta)) / (2*a);
printf("x1 = %f, x2 = %f\n", x1, x2);
}
else {
real = -b/(2*a);
imag = sqrt(-delta) / (2*a);
printf("x1 = %f + %fi, x2 = %f - %fi\n", real, imag, real, imag);
}
}
printf("Enter a, b, c:\n");
}
system("pause");
return 0;
}
Part2
// 猜数游戏
// 程序运行时自动生成1~100之间的随机数,提示用户猜
// 如果用户猜的数等于随机数,提示用户猜对了,程序结束
// 否则,如果用户猜的数小于随机数,提示用户低了,用户继续猜
// 如果用户猜的数大于随机数,提示用户高了,用户继续猜
// 程序循环运行直到用户猜对为止
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int guessNumber; // 存放程序运行时生成的1~100之间的随机整数
int ans; // 存放用户猜的数字
srand((time(0))); // 以时间函数作为随机种子
guessNumber = 1 + rand()%100; // 生成1~100之间的随机数
do {
printf("your guess number is(1~100): ");
scanf("%d", &ans);
if(ans < guessNumber)
printf("%d is lower than the number.\n", ans);
else if(ans > guessNumber)
printf("%d higher then the number.\n", ans);
}while(ans != guessNumber);
printf("Congratulations. you hit it~\n");
system("pause"); // 在devc中运行时,这一行可以去掉
return 0;
}
Part3
/*
编程找出5个整数的最大数和最小数
《C语言程序设计教程学习指导》p122实验内容(3)
*/
#include <stdio.h>
#include <stdlib.h>
int main() {
int number, max, min, n;
n=1;
printf("输入第%d个数: ", n);
scanf("%d", &number);
max = number;
min = number;
while(number,n<=4) {
n++;
printf("输入第%d个数: ", n);
scanf("%d", &number);
if(number>max)
max = number;
else if(number<min)
min = number;
}
printf("最大数为: %d\n", max);
printf("最小数为: %d\n", min);
system("pause");
return 0;
}
Part4
1:
# include<stdio.h>
# include<math.h>
int isprime(int n);
int main()
{
int x,t;
t=0;
for(x=101;x<=200;x++)
{
if(isprime(x))
{
printf("%24d",x);
t=t+1;
}
}
printf("\n101~200之间共有%d个素数",t);
return 0;
}
int isprime(int n)
{
int k;
for(k=2;k<=sqrt(n);k++)
if(n%k==0)
return 0;
return 1;
}
Part5
#include<stdio.h> #include<math.h> int main() { long n; int t, p, m; t = 0, p = 1; printf("Enter a number:"); scanf("%ld", &n); while (n) { m = n % 10; if (m % 2 != 0) { t = t + m * p; p = p * 10; } n = n / 10; } printf("new number is:%ld\n", t); return 0;
}
标签:之间 素数 sys pre info 函数 程序 real 整数
原文地址:https://www.cnblogs.com/ADSLadl85026262/p/11886369.html