Compare C and Java

There is a lot of differences between C and java, so write those codes and compare with them.There is a question about a triangle, need u to write codes to obtain its length and area.Use C and java to write it and compare what the differences there are.

C codes:
#include <stdio.h>
#include <math.h> 

int perimeter(int m,int n,int q)
{
    return (m+n+q);
}

double trianglearea(int m,int n,int q)
{
    double p = 1.0*(m+n+q)/2;
    return sqrt(p*(p-m)*(p-n)*(p-q));//This is a formula can quickly get the area of a triangle.
}

int main()
{
    int m,n,q;
    scanf("%d %d %d",&m,&n,&q);
    
    int len = perimeter(m,n,q);
    
    double area = trianglearea(m,n,q);
    
    printf("%d\n",len);
    printf("%lf\n",area);
    
    return 0;
} 
Java codes:
package javabase;
class Trian{
    int a;
    int b;
    int c;
    int len()
    {
        return a+b+c;
    }
    double area()
    {
        double p = 1.0*(a+b+c)/2;
        return Math.sqrt(p*(p-a)*(p-b)*(p-c));
    }
}
public class Triangle {

    public static void main(String[] args) {

        Trian t = new Trian();
        t.a = 3;
        t.b = 4;
        t.c = 5;
        System.out.printf("%d    %f\n",t.len(),t.area());
        //use java,no matter the type is double or float ,print out with the format %f
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容