A practical comparison of four widely used programming languages. Each example solves the same small problem: read three scores, calculate the average, and print whether the learner passed.
This repository is designed for beginners who want to compare syntax instead of memorizing isolated definitions.
| Concept | C | Python | C++ | Java |
|---|---|---|---|---|
| Program entry | main() |
Top-level code | main() |
main() method |
| Collection | Array | List | std::array |
Array |
| Output | printf |
print |
std::cout |
System.out.println |
| Typical execution | Compiled | Interpreted | Compiled | JVM bytecode |
C makes memory, types and control flow explicit. It is an excellent language for learning how programs work close to the system.
#include <stdio.h>
int main(void) {
int scores[] = {72, 85, 91};
int total = 0;
int count = sizeof(scores) / sizeof(scores[0]);
for (int i = 0; i < count; i++) {
total += scores[i];
}
double average = (double) total / count;
printf("Average: %.2f\n", average);
printf("%s\n", average >= 60 ? "Pass" : "Try again");
return 0;
}Compile and run:
gcc average.c -o average
./averageContinue with the free C language course or test a program in the browser-based C compiler.
Python uses concise, readable syntax. It is popular for automation, data work, web development and beginner programming.
scores = [72, 85, 91]
average = sum(scores) / len(scores)
print(f"Average: {average:.2f}")
print("Pass" if average >= 60 else "Try again")Run:
python average.pyContinue with the free Python course or experiment in the online Python compiler.
C++ retains low-level control while adding classes, templates and a powerful standard library.
#include <array>
#include <iostream>
#include <numeric>
int main() {
std::array<int, 3> scores{72, 85, 91};
int total = std::accumulate(scores.begin(), scores.end(), 0);
double average = static_cast<double>(total) / scores.size();
std::cout << "Average: " << average << '\n';
std::cout << (average >= 60 ? "Pass" : "Try again") << '\n';
return 0;
}Compile and run:
g++ average.cpp -std=c++17 -o average
./averageContinue with the free C++ course or use the online C++ compiler.
Java uses classes and runs compiled bytecode on the Java Virtual Machine, making it portable across operating systems.
public class AverageScore {
public static void main(String[] args) {
int[] scores = {72, 85, 91};
int total = 0;
for (int score : scores) {
total += score;
}
double average = (double) total / scores.length;
System.out.printf("Average: %.2f%n", average);
System.out.println(average >= 60 ? "Pass" : "Try again");
}
}Compile and run:
javac AverageScore.java
java AverageScoreContinue with the free Java course or run code in the browser-based Java compiler.
Average: 82.67
Pass
Minor formatting differences are normal.
- Ask the user to enter the scores instead of hardcoding them.
- Print the highest and lowest scores.
- Support any number of scores.
- Assign grades: A, B, C, D or F.
- Reject values outside the range 0–100.
- Choose C to understand memory, types and core programming mechanics.
- Choose Python for readable syntax and fast experimentation.
- Choose C++ for performance, systems, games and competitive programming.
- Choose Java for object-oriented foundations, backend applications and Android-related ecosystems.
There is no universal best language. Choose one, build small programs consistently, and use comparisons like this only when they help you understand a concept.
Contributions are welcome. Keep examples:
- runnable with standard tools;
- small enough for beginners to understand;
- documented with expected output;
- free of unnecessary dependencies.
Please open an issue before adding a new language or substantially changing the learning objective.
MIT