C语言练习记录(持续更新)

C语言练习记录(持续更新),第1张

C语言练习记录(持续更新) 记录一下本人在学习C语言时做的有价值的部分小练习,持续更新.英语较差,意会即可. (1)计算1/2+1/3+...+1/n=1
// this is a code calculating s=1/2+1/3+1/4+...+1/n=1

#include
int main()
{
    int n,m;
    double s,d;
    printf("enter the number of loop:");
    scanf("%d",&n);
    for (s=0,m=1,d=2;m<=n;m+=1,d*=2)
    {
        s+=1/d;
        printf("when loop =%d,d=%f,s=%fn ",m,d,s);
    }
    return 0;

}
(2)计算浮点类型数的正整数次方
// this is a code calculating the positive power of a number
#include
float power(float n,int p);
int main()
{
    float di;
    int zhi;

    printf("please enter the dishiu and the zhisu(char or negtive number to quit");
    while(scanf("%f%d",&di,&zhi)==2&&zhi>0)
    {
        printf("%f to the %dth power is %fn",di,zhi,power(di,zhi));
        printf("please enter another group of number(char or negtive number to quit)");
    }
    printf("quit successfullyn");
}
float power(float n,int p)
{
    int i;
    float pow=1;
    for(i=1;i<=p;i++)
        pow*=n;
    return pow;
}

(3)ABA型金字塔

#include
int main()
{
    char CH;int CHn;
    printf("please enter a character between A-Z");
    CH=getchar();CHn=(int)CH-64;
    // CHn确定程序有多少行,用于第一个for循环
    int hang,lie;
    for(hang=1,lie=1;hang<=CHn;hang++,lie=hang)
    {
        for( ;lie 

(4)输入字符串,倒序输出字符串.此时要注意,字符串的最后一个单元储存‘',所以定义时要注意单元总个数.

#include
#include
int main()
{
    char lstring [100];
    int n;
    printf("please enter a string:(within 100 characters):");
    scanf("%s",lstring);
    n=strlen(lstring);
    int a;char string[n];
    for(a=0;a 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5520689.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-13
下一篇2022-12-13

发表评论

登录后才能评论

评论列表(0条)

    保存