C-Programmierung
Vergleich Mit Java
← Einführung bzw. Unterschiede von C und C++ bzgl. Java | ● | Vergleich mit Java - Compiler →
Java Beispiel Quelltext (HelloJava.java):
// Java example
//
// to compile and run the example, type the following on the terminal:
// > javac HelloJava.java
// > java HelloJava
//
// output:
// > fibo(10) = 55
public class HelloJava
{
protected static int fibo(int n) {
if (n <= 0)
return(0);
else if (n <= 2)
return(1);
else
return( fibo(n-1) + fibo(n-2) );
}
public static void main(String[] args) {
int n = 10;
System.out.println("fibo(" + n + ") = " + fibo(n) );
}
}
//
// to compile and run the example, type the following on the terminal:
// > javac HelloJava.java
// > java HelloJava
//
// output:
// > fibo(10) = 55
public class HelloJava
{
protected static int fibo(int n) {
if (n <= 0)
return(0);
else if (n <= 2)
return(1);
else
return( fibo(n-1) + fibo(n-2) );
}
public static void main(String[] args) {
int n = 10;
System.out.println("fibo(" + n + ") = " + fibo(n) );
}
}
C++ Beispiel Quelltext (HelloCpp.cpp):
// C++ example
//
// to compile and run the example, type the following on the terminal:
// > g++ HelloCpp.cpp -o HelloCpp
// > ./HelloCpp
//
// output:
// > fibo(10) = 55
#include <iostream>
unsigned int fibo(unsigned int n)
{
if (n <= 0)
return(0);
else if (n <= 2)
return(1);
else
return( fibo(n-1) + fibo(n-2) );
}
int main(int argc, char *argv[])
{
int n = 10;
std::cout << "fibo(" << n << ") = " << fibo(n) << std::endl;
return(0);
}
//
// to compile and run the example, type the following on the terminal:
// > g++ HelloCpp.cpp -o HelloCpp
// > ./HelloCpp
//
// output:
// > fibo(10) = 55
#include <iostream>
unsigned int fibo(unsigned int n)
{
if (n <= 0)
return(0);
else if (n <= 2)
return(1);
else
return( fibo(n-1) + fibo(n-2) );
}
int main(int argc, char *argv[])
{
int n = 10;
std::cout << "fibo(" << n << ") = " << fibo(n) << std::endl;
return(0);
}
← Einführung bzw. Unterschiede von C und C++ bzgl. Java | ● | Vergleich mit Java - Compiler →