一、for和while等价替换
二、从键盘输入一个数字,如果该数字是回文数,则返回yes,否则返回no
- int i = 1;
- for (i; i<=100; i++){
- sum = sum + 1;
- }
- int i = 1;
- while(i<=100){
- sum = sum + 1;
- i++;
- }
三、斐波拉契数列回文数:正着写倒着写一样的数
- # include<stdio.h>
- int main(){
- int val, m, sum=0;
- printf("请输入您需要判断的数字:");
- scanf("%d", &val);
- m = val;
- while(m){
- sum = sum * 10 + m%10;
- m /= 10;
- }
- if(sum==val){
- printf("yes\n");
- }else{
- printf("no\n");
- }
- return 0;
- }
1 2 3 5 8 13 21 34.....
从第三项开始每一项是前两项的和
- # include <stdio.h>
- int main(void){
- int n;
- int f1, f2, f3;
- int i;
- f1 = 1;
- f2 = 2;
- printf("请输入您需要求得项的序列:\n");
- scanf("%d", &n);
- if(1==n){
- f3 = 1;
- }else if(2==n){
- f3 = 2;
- }else{
- for(i=3; i<=n; i++){
- f3 = f1 + f2;
- f1 = f2;
- f2 = f3;
- }
- }
- printf("%d\n", f3);
- return 0;
- }
四、do....while....实现一元二次方程
- # include <stdio.h>
- # include <math.h>
- int main(void)
- {
- double a, b, c;
- double delta;
- double x1, x2;
- char ch;
- do
- {
- printf("请输入一元二次方程的三个系数:\n");
- printf("a = ");
- scanf("%lf", &a);
- printf("b = ");
- scanf("%lf", &b);
- printf("c = ");
- scanf("%lf", &c);
- delta = b*b - 4*a*c;
- if (delta > 0)
- {
- x1 = (-b + sqrt(delta)) / (2*a);
- x2 = (-b - sqrt(delta)) / (2*a);
- printf("有两个解,x1 = %lf, x2 = %lf\n", x1, x2);
- }
- else if (0 == delta)
- {
- x1 = x2 = (-b) / (2*a);
- printf("有唯一解,x1 = x2 = %lf\n", x1, x2);
- }
- else
- {
- printf("无实数解!\n");
- }
- printf("您想继续么(Y/N): ");
- scanf(" %c", &ch); //%c前面必须得加一个空格 原因略
- } while ('y'==ch || 'Y'==ch);
- return 0;
- }