栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言简单题目收集

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言简单题目收集

一、10进制转化为任意数

#include
void exchange(int src,int n){
    if(src==0)
        return;
    else{
        exchange(src/n,n);
        printf("%d", src % n);
    }
}
int main(){
    int src, n;
    scanf("%d,%d",&src,&n);
    exchange(src,n);
    return 0;
}

二、打印n以内所有素数

#include
int main(){
    int n,i,j;
    scanf("%d",&n);
    if(n>=1)
        printf("1 ");
    for (i = 1; i <= n;i++)
    {
        for (j = 2; j < i;j++)
        {   
            if(i%j==0)
                break;
        }
        if(i==j)
            printf("%d ",i);
    }
    return 0;
}

三、将结构体数据写入到文件中

#include
#include
struct student
{
    int id;
    char name[10];
    int age;
}stu;

int main(){
    scanf("%d %s %d",&stu.id,stu.name,&stu.age);
    FILE *fp;
    if((fp = fopen("student.txt","w"))==NULL)
        exit(-1);
    fprintf(fp,"%d %s %d",stu.id,stu.name,stu.age);
    fclose(fp);
    return 0;
}

四、从文件中读取数据到结构体中

#include
#include
struct student
{
    int id;
    char name[10];
    int age;
}stu;

int main(){
    FILE *fp;
    if((fp = fopen("student.txt","r"))==NULL)
        exit(-1);
    fscanf(fp,"%d %s %d",&stu.id,stu.name,&stu.age);
    fclose(fp);
    printf("%d %s %dn",stu.id,stu.name,stu.age);
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/864094.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号