例题三、简单的分支与循环结构

1.编写求圆面积的程序,要求当输入的半径r<=0时,提示输入错误,要求r为浮点型,r的数值是动态的由键盘输入;

#include<stdio.h>
int main(void)
{
    double R,S;

    printf("Enter R:");
    scanf("%lf",&R);
    if(R<=0){
        printf("输入错误
");
    }
    else {
        S = 3.1415926*R*R;
        printf("S=%.2f
",S);
    }

    return 0;
}

2.利用循环计算n个圆柱体体积。
要求:
1. 当输入的半径r或h<=0时,提示输入错误,重新输入;
2. r和h为整形;
3. n、r和h的数值是由键盘输入;
4. 连续计算n个圆柱体体积;  

#include<stdio.h>
int main(void)
{
    int i,n;
    double R,V,H;

    printf("Enter n:");
    scanf("%d",&n);

 for(i=1;i<=n;i++){

    printf("Enter H:");
    scanf("%Lf",&H);
    printf("Enter R:");
    scanf("%Lf",&R);

    if(R,H<=0){
        printf("输入错误。 ");
    }
    else{
            V=3.1415926*R*R*H;
            printf("V=%.2f ",V);
        }
    }

    return 0;
}

3.阅读下面程序、分析说明运行结果,并上机验证。

若从键盘分行输入以下数据,则输出结果是什么?
12↙
3456↙

#include<stdio.h>
int main()
{
    int c1,c2,c3,c4;
    scanf("%d%d",&c1,&c2);
    printf("%d
",c1+c2);

    getchar();
    c3 = getchar();
    c4 = getchar();

    printf("%d
",c3+c4);
    return 0;
}

 

4.阅读下面程序、分析说明运行结果,并上机验证。

若从键盘分行输入以下数据,则输出结果是什么?

12↙
3456↙

#include<stdio.h>
int main()
{
    char c1,c2,c3,c4;
    scanf("%c%c",&c1,&c2);
    printf("%c %c
",c1,c2);
    getchar();
    c3 = getchar();
    c4 = getchar();
    printf("%c %c
",c3,c4);
    printf("%c %c %c %c
",c1,c2,c3,c4);
    return 0;
}

原文地址:https://www.cnblogs.com/danson-daisy/p/3367786.html