Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Beginner Code Examples in C, Python, C++ and Java

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.

What you will compare

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 example

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
./average

Continue with the free C language course or test a program in the browser-based C compiler.

Python example

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.py

Continue with the free Python course or experiment in the online Python compiler.

C++ example

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
./average

Continue with the free C++ course or use the online C++ compiler.

Java example

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 AverageScore

Continue with the free Java course or run code in the browser-based Java compiler.

Expected output

Average: 82.67
Pass

Minor formatting differences are normal.

Practice challenges

  1. Ask the user to enter the scores instead of hardcoding them.
  2. Print the highest and lowest scores.
  3. Support any number of scores.
  4. Assign grades: A, B, C, D or F.
  5. Reject values outside the range 0–100.

Which language should a beginner choose?

  • 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.

Contributing

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.

License

MIT

About

Beginner-friendly C, Python, C++ and Java examples with explanations and learning resources.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors