• C语言实例_5


    1.加法运算

    写一个加法程序,输入整数a,b,输出他们的和。

    #include<stdio.h>
    	int main(void)
    	{  
    	   int a,b,c; 
    	   scanf("%d,%d", &a,&b);
    	   c = a+b; 
    	   printf("%d+%d=%d\n",a,b,c);
           return 0;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.不使用第3个变量,实现两个数的对调

    不用第三个变量,实现将两个数进行对调的操作。

    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {  
    	  int a,b;
          scanf("%d%d",&a,&b); 
          printf("a=%d b=%d\n",a,b);
    	  a=a^b;
          b=a^b;
          a=a^b;
    	  printf("a=%d  b=%d\n",a,b);  
          return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3.用宏定义常量

    已知某物品单价为30,数量为x。求商品的总价钱。用宏定义物品的单价。

    #include<stdio.h>
    int main(void)
    {  
    	  int x,f;
          int p=30;
          scanf("%d",&x);
          f = x*p;
          printf("%d",f);
          return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    4.计算总成绩和平均成绩

    编程求从键盘上输入一个学生的五门成绩,计算出该学生的总成绩和平均成绩。

    #include<stdio.h>
    int main(void)
    {  
    	  int a,b,c,d,e;
          int m;
          double n;
          scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
          m=a+b+c+d+e;
          n=m/5.0;
          printf("%d %.2lf",m,n);
          return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5.求三角形的面积

    编程求以a、b、c为边长的三角形的面积area。

    #include<stdio.h>
    #include<math.h>
    int main(void)
    {  
    	  double a,b,c;
          double s,area;
          scanf("%lf %lf %lf",&a,&b,&c);
          s=(a+b+c)/2;
          area = sqrt(s*(s-a)*(s-b)*(s-c));
    	  printf("%.3lf",area);
    	  /*********End**********/ 
          return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    6.计算两个正整数的最大公约数

    编程计算两个正整数的最大公约数。其中求最大公约数的函数原型已经给出,请在主函数中编程调用函数,输出最大公约数。

    程序的运行示例:

    12,3
    • 1
    3
    
    • 1

    ####函数原型说明
    求最大公约数的函数原型如下:

    int  MaxCommonFactor( int a, int  b);
    
    • 1

    返回值:返回的是最大公约数;若输入的数据有任意一个不满足条件,返回值是-1。
    参数:a,b是两个整型数

    #include<stdio.h>
    int MaxCommonFactor( int a, int b)
    { 
       int c; 
       if(a<=0||b<=0) 
          return -1; 
       while(b!=0)
       { 
         c=a%b; 
         a=b;
         b=c;
       } 
      return a; 
    }   
    int main(void)
    	{  
    	  int a,b;
          scanf("%d,%d",&a,&b);
          int x=MaxCommonFactor(a,b);
          printf("%d",x);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    浅谈基于云计算的环境智能监控系统
    二、JavaScript基本语法:var、let、const、数据类型、运算符、类型转换
    ADB各种操作指令详解大汇总
    Ceph入门到精通-sysctl.conf 配置
    安卓项目结构分析
    java数组
    二叉树的层序遍历
    Vue中this指向问题
    如何在 JavaScript 中使用闭包——初学者指南
    notepad++ 批量替换删除指定字符之后 或者 之前的字符,Notepad+批量替换使用大全
  • 原文地址:https://blog.csdn.net/qq_51771374/article/details/125628129