C语言实例(一)

输出Hello,world!

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("Hello,world!\n");
    return 0}

输入一个整数

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a;
    printf("please enter an intenger:");
    scanf("%d",&a);
    printf("The integer you entered is:%d",a);
    return 0;
}

输入单个字符

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char c;
    c='A';
    printf("the value of c is:%c",c);
    return 0;
}

输出浮点数

#include <stdio.h>
#include <stdlib.h>
int main()
{
    float f;
    f=12.56730;
    printf("the value of f is:%f",f);
    return 0;
}

输出双精度数

#include <stdio.h>
#include <stdlib.h>
int main()
{
    double d;
    d=12.56730;
    printf("the value of d is:%lf\n",d);//浮点数的方法
    printf("the value of d is:%le",d);//科学计数法的方法
    return 0;
}

两个整数相加

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num1,num2,sum;
    printf("please enter two integer\n");
    scanf("%d%d",&num1,&num2);
    sum=num1+num2;
    printf("%d",sum);
    return 0;
}

两个浮点数相乘

在这里插#include <stdio.h>
#include <stdlib.h>
int main()
{
    double num1,num2,product;
    printf("please enter two floating point number\n");
    scanf("%lf%lf",&num1,&num2);
    product=num1*num2;
    printf("%.2lf",product);//保留小数点后的两位
    return 0;
}
入代码片

字符转ASCII码

在这里插入代#include <stdio.h>
#include <stdlib.h>
int main()
{
    char c;
    printf("please enter a character:\n");
    scanf("%c",&c);
    printf("%c的ASCII值为%d",c,c);
    return 0;
}
码片

两数相除

#include <stdio.h>
#include <stdlib.h>

int main()
{

    int dividend, divisor, quotient, remainder;

    printf("输入被除数: ");
    scanf("%d", &dividend);

    printf("输入除数: ");
    scanf("%d", &divisor);

    // 计算商
    quotient = dividend / divisor;

    // 计算余数
    remainder = dividend % divisor;

    printf("商 = %d\n", quotient);
    printf("余数 = %d", remainder);

    return 0;
}

比较两个数大小

在这里#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b;
    printf("please enter two integer\n");
    scanf("%d%d",&a,&b);
    if(a>b)
    {
        printf("a比b大\n");
    }
    else
    {
        printf("a比b小\n");
    }
    return 0;
}
插入代码片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值