Reference

Code Examples

A collection of complete, working BanglaCode programs demonstrating various language features and common programming patterns.

Hello World

hello.bangbanglacode
1// The classic first program
2dekho("Namaskar, BanglaCode!");
3dekho("Hello, World!");
4
5// With variables
6dhoro naam = "Rahim";
7dhoro greeting = "Shuvo Sokal";
8dekho(greeting + ",", naam + "!");

Calculator

calculator.bangbanglacode
1sreni Calculator {
2 shuru() {
3 ei.history = [];
4 }
5
6 kaj add(a, b) {
7 dhoro result = a + b;
8 ei.saveHistory(a, "+", b, result);
9 ferao result;
10 }
11
12 kaj subtract(a, b) {
13 dhoro result = a - b;
14 ei.saveHistory(a, "-", b, result);
15 ferao result;
16 }
17
18 kaj multiply(a, b) {
19 dhoro result = a * b;
20 ei.saveHistory(a, "*", b, result);
21 ferao result;
22 }
23
24 kaj divide(a, b) {
25 jodi (b == 0) {
26 felo "Cannot divide by zero!";
27 }
28 dhoro result = a / b;
29 ei.saveHistory(a, "/", b, result);
30 ferao result;
31 }
32
33 kaj saveHistory(a, op, b, result) {
34 dhokao(ei.history, lipi(a) + " " + op + " " + lipi(b) + " = " + lipi(result));
35 }
36
37 kaj showHistory() {
38 dekho("=== Calculation History ===");
39 ghuriye (dhoro i = 0; i < dorghyo(ei.history); i = i + 1) {
40 dekho(i + 1, ".", ei.history[i]);
41 }
42 }
43}
44
45dhoro calc = notun Calculator();
46dekho(calc.add(10, 5)); // 15
47dekho(calc.multiply(3, 4)); // 12
48dekho(calc.divide(20, 4)); // 5
49calc.showHistory();

Fibonacci Sequence

fibonacci.bangbanglacode
1// Recursive Fibonacci
2kaj fibRecursive(n) {
3 jodi (n <= 1) {
4 ferao n;
5 }
6 ferao fibRecursive(n - 1) + fibRecursive(n - 2);
7}
8
9// Iterative Fibonacci (more efficient)
10kaj fibIterative(n) {
11 jodi (n <= 1) {
12 ferao n;
13 }
14
15 dhoro a = 0;
16 dhoro b = 1;
17
18 ghuriye (dhoro i = 2; i <= n; i = i + 1) {
19 dhoro temp = a + b;
20 a = b;
21 b = temp;
22 }
23
24 ferao b;
25}
26
27// Generate Fibonacci sequence
28kaj fibSequence(count) {
29 dhoro sequence = [];
30 ghuriye (dhoro i = 0; i < count; i = i + 1) {
31 dhokao(sequence, fibIterative(i));
32 }
33 ferao sequence;
34}
35
36dekho("First 15 Fibonacci numbers:");
37dekho(fibSequence(15));

Prime Number Checker

primes.bangbanglacode
1kaj isPrime(n) {
2 jodi (n < 2) {
3 ferao mittha;
4 }
5 jodi (n == 2) {
6 ferao sotti;
7 }
8 jodi (n % 2 == 0) {
9 ferao mittha;
10 }
11
12 ghuriye (dhoro i = 3; i * i <= n; i = i + 2) {
13 jodi (n % i == 0) {
14 ferao mittha;
15 }
16 }
17
18 ferao sotti;
19}
20
21kaj findPrimes(limit) {
22 dhoro primes = [];
23 ghuriye (dhoro n = 2; n <= limit; n = n + 1) {
24 jodi (isPrime(n)) {
25 dhokao(primes, n);
26 }
27 }
28 ferao primes;
29}
30
31kaj primeFactors(n) {
32 dhoro factors = [];
33 dhoro d = 2;
34
35 jotokkhon (d * d <= n) {
36 jotokkhon (n % d == 0) {
37 dhokao(factors, d);
38 n = n / d;
39 }
40 d = d + 1;
41 }
42
43 jodi (n > 1) {
44 dhokao(factors, n);
45 }
46
47 ferao factors;
48}
49
50dekho("Primes up to 50:", findPrimes(50));
51dekho("Is 97 prime?", isPrime(97));
52dekho("Prime factors of 84:", primeFactors(84));

Sorting Algorithms

sorting.bangbanglacode
1// Bubble Sort
2kaj bubbleSort(arr) {
3 dhoro n = dorghyo(arr);
4 dhoro result = kato(arr, 0); // Copy array
5
6 ghuriye (dhoro i = 0; i < n - 1; i = i + 1) {
7 ghuriye (dhoro j = 0; j < n - i - 1; j = j + 1) {
8 jodi (result[j] > result[j + 1]) {
9 // Swap
10 dhoro temp = result[j];
11 result[j] = result[j + 1];
12 result[j + 1] = temp;
13 }
14 }
15 }
16
17 ferao result;
18}
19
20// Selection Sort
21kaj selectionSort(arr) {
22 dhoro n = dorghyo(arr);
23 dhoro result = kato(arr, 0);
24
25 ghuriye (dhoro i = 0; i < n - 1; i = i + 1) {
26 dhoro minIdx = i;
27
28 ghuriye (dhoro j = i + 1; j < n; j = j + 1) {
29 jodi (result[j] < result[minIdx]) {
30 minIdx = j;
31 }
32 }
33
34 jodi (minIdx != i) {
35 dhoro temp = result[i];
36 result[i] = result[minIdx];
37 result[minIdx] = temp;
38 }
39 }
40
41 ferao result;
42}
43
44// Test
45dhoro numbers = [64, 34, 25, 12, 22, 11, 90];
46dekho("Original:", numbers);
47dekho("Bubble Sort:", bubbleSort(numbers));
48dekho("Selection Sort:", selectionSort(numbers));

Todo List Application

todo.bangbanglacode
1sreni TodoApp {
2 shuru() {
3 ei.todos = [];
4 ei.nextId = 1;
5 }
6
7 kaj add(text) {
8 dhoro todo = {
9 id: ei.nextId,
10 text: text,
11 done: mittha,
12 createdAt: somoy()
13 };
14 dhokao(ei.todos, todo);
15 ei.nextId = ei.nextId + 1;
16 dekho("Added:", text);
17 ferao todo.id;
18 }
19
20 kaj complete(id) {
21 ghuriye (dhoro i = 0; i < dorghyo(ei.todos); i = i + 1) {
22 jodi (ei.todos[i].id == id) {
23 ei.todos[i].done = sotti;
24 dekho("Completed:", ei.todos[i].text);
25 ferao sotti;
26 }
27 }
28 dekho("Todo not found:", id);
29 ferao mittha;
30 }
31
32 kaj remove(id) {
33 dhoro newTodos = [];
34 ghuriye (dhoro i = 0; i < dorghyo(ei.todos); i = i + 1) {
35 jodi (ei.todos[i].id != id) {
36 dhokao(newTodos, ei.todos[i]);
37 }
38 }
39 ei.todos = newTodos;
40 }
41
42 kaj list() {
43 dekho("\n=== Todo List ===");
44 jodi (dorghyo(ei.todos) == 0) {
45 dekho("No todos!");
46 ferao;
47 }
48
49 ghuriye (dhoro i = 0; i < dorghyo(ei.todos); i = i + 1) {
50 dhoro t = ei.todos[i];
51 dhoro status = t.done ? "[X]" : "[ ]";
52 dekho(status, t.id + ".", t.text);
53 }
54 }
55
56 kaj pending() {
57 dhoro count = 0;
58 ghuriye (dhoro i = 0; i < dorghyo(ei.todos); i = i + 1) {
59 jodi (na ei.todos[i].done) {
60 count = count + 1;
61 }
62 }
63 ferao count;
64 }
65}
66
67// Usage
68dhoro app = notun TodoApp();
69app.add("Learn BanglaCode");
70app.add("Build a project");
71app.add("Share with friends");
72app.complete(1);
73app.list();
74dekho("\nPending tasks:", app.pending());

Bank Account System

bank.bangbanglacode
1sreni BankAccount {
2 shuru(accountNumber, owner, initialBalance) {
3 ei.accountNumber = accountNumber;
4 ei.owner = owner;
5 ei.balance = initialBalance;
6 ei.transactions = [];
7 }
8
9 kaj deposit(amount) {
10 jodi (amount <= 0) {
11 felo "Deposit amount must be positive";
12 }
13 ei.balance = ei.balance + amount;
14 ei.recordTransaction("DEPOSIT", amount);
15 dekho("Deposited Tk.", amount);
16 }
17
18 kaj withdraw(amount) {
19 jodi (amount <= 0) {
20 felo "Withdrawal amount must be positive";
21 }
22 jodi (amount > ei.balance) {
23 felo "Insufficient funds";
24 }
25 ei.balance = ei.balance - amount;
26 ei.recordTransaction("WITHDRAW", amount);
27 dekho("Withdrew Tk.", amount);
28 }
29
30 kaj transfer(toAccount, amount) {
31 ei.withdraw(amount);
32 toAccount.deposit(amount);
33 dekho("Transferred Tk.", amount, "to", toAccount.owner);
34 }
35
36 kaj recordTransaction(type, amount) {
37 dhokao(ei.transactions, {
38 type: type,
39 amount: amount,
40 balance: ei.balance,
41 timestamp: somoy()
42 });
43 }
44
45 kaj getBalance() {
46 ferao ei.balance;
47 }
48
49 kaj printStatement() {
50 dekho("\n=== Account Statement ===");
51 dekho("Account:", ei.accountNumber);
52 dekho("Owner:", ei.owner);
53 dekho("Current Balance: Tk.", ei.balance);
54 dekho("\nTransactions:");
55
56 ghuriye (dhoro i = 0; i < dorghyo(ei.transactions); i = i + 1) {
57 dhoro t = ei.transactions[i];
58 dekho(" ", t.type, "Tk.", t.amount, "- Balance: Tk.", t.balance);
59 }
60 }
61}
62
63// Usage
64dhoro account1 = notun BankAccount("001", "Rahim", 10000);
65dhoro account2 = notun BankAccount("002", "Karim", 5000);
66
67account1.deposit(5000);
68account1.withdraw(2000);
69account1.transfer(account2, 3000);
70
71account1.printStatement();
72account2.printStatement();

Simple HTTP API

api.bangbanglacode
1// Simple REST API for managing users
2dhoro users = [];
3dhoro nextId = 1;
4
5kaj findUserById(id) {
6 ghuriye (dhoro i = 0; i < dorghyo(users); i = i + 1) {
7 jodi (users[i].id == id) {
8 ferao users[i];
9 }
10 }
11 ferao khali;
12}
13
14kaj deleteUserById(id) {
15 dhoro newUsers = [];
16 ghuriye (dhoro i = 0; i < dorghyo(users); i = i + 1) {
17 jodi (users[i].id != id) {
18 dhokao(newUsers, users[i]);
19 }
20 }
21 users = newUsers;
22}
23
24server_chalu(8080, kaj(req, res) {
25 // Enable CORS
26 res.headers["Access-Control-Allow-Origin"] = "*";
27 res.headers["Content-Type"] = "application/json";
28
29 // GET /users - List all users
30 jodi (req.path == "/users" ebong req.method == "GET") {
31 json_uttor(res, {
32 success: sotti,
33 data: users,
34 count: dorghyo(users)
35 });
36 }
37 // POST /users - Create user
38 nahole jodi (req.path == "/users" ebong req.method == "POST") {
39 chesta {
40 dhoro body = json_poro(req.body);
41 dhoro user = {
42 id: nextId,
43 naam: body.naam,
44 email: body.email,
45 createdAt: somoy()
46 };
47 dhokao(users, user);
48 nextId = nextId + 1;
49 json_uttor(res, {success: sotti, data: user}, 201);
50 } dhoro_bhul (e) {
51 json_uttor(res, {success: mittha, error: lipi(e)}, 400);
52 }
53 }
54 // GET /users/:id - Get single user
55 nahole jodi (angsho(req.path, 0, 7) == "/users/" ebong req.method == "GET") {
56 dhoro id = sonkha(angsho(req.path, 7));
57 dhoro user = findUserById(id);
58 jodi (user != khali) {
59 json_uttor(res, {success: sotti, data: user});
60 } nahole {
61 json_uttor(res, {success: mittha, error: "User not found"}, 404);
62 }
63 }
64 // DELETE /users/:id - Delete user
65 nahole jodi (angsho(req.path, 0, 7) == "/users/" ebong req.method == "DELETE") {
66 dhoro id = sonkha(angsho(req.path, 7));
67 deleteUserById(id);
68 json_uttor(res, {success: sotti, message: "User deleted"});
69 }
70 // 404 for other routes
71 nahole {
72 json_uttor(res, {success: mittha, error: "Not found"}, 404);
73 }
74});
75
76dekho("API running on http://localhost:8080");

Async Data Fetcher

Demonstrates asynchronous programming with proyash (async) and opekha (await). Shows concurrent execution with sob_proyash (Promise.all).

1// Simulate fetching data from different sources
2proyash kaj fetchUser(id) {
3 dekho("Fetching user", id, "...");
4 opekha ghumaao(1000); // Simulate network delay
5 ferao {naam: "User " + lipi(id), id: id};
6}
7
8proyash kaj fetchPosts(userId) {
9 dekho("Fetching posts for user", userId, "...");
10 opekha ghumaao(1500); // Simulate network delay
11 ferao ["Post 1", "Post 2", "Post 3"];
12}
13
14proyash kaj fetchComments(userId) {
15 dekho("Fetching comments for user", userId, "...");
16 opekha ghumaao(800); // Simulate network delay
17 ferao ["Comment 1", "Comment 2"];
18}
19
20// Load dashboard - sequential approach (slow)
21proyash kaj loadDashboardSlow() {
22 dekho("=== Sequential Loading (slow) ===");
23 dhoro start = somoy();
24
25 dhoro user = opekha fetchUser(1);
26 dhoro posts = opekha fetchPosts(user["id"]);
27 dhoro comments = opekha fetchComments(user["id"]);
28
29 dhoro elapsed = somoy() - start;
30 dekho("User:", user["naam"]);
31 dekho("Posts:", dorghyo(posts));
32 dekho("Comments:", dorghyo(comments));
33 dekho("Sequential time:", elapsed, "ms"); // ~3300ms
34}
35
36// Load dashboard - concurrent approach (fast)
37proyash kaj loadDashboardFast() {
38 dekho("");
39 dekho("=== Concurrent Loading (fast) ===");
40 dhoro start = somoy();
41
42 // First get user
43 dhoro user = opekha fetchUser(1);
44
45 // Then fetch posts and comments concurrently
46 dhoro results = opekha sob_proyash([
47 fetchPosts(user["id"]),
48 fetchComments(user["id"])
49 ]);
50
51 dhoro posts = results[0];
52 dhoro comments = results[1];
53
54 dhoro elapsed = somoy() - start;
55 dekho("User:", user["naam"]);
56 dekho("Posts:", dorghyo(posts));
57 dekho("Comments:", dorghyo(comments));
58 dekho("Concurrent time:", elapsed, "ms"); // ~2500ms (33% faster!)
59}
60
61// Run both approaches
62proyash kaj main() {
63 opekha loadDashboardSlow();
64 opekha loadDashboardFast();
65 dekho("");
66 dekho("✓ Async demo complete!");
67}
68
69main();

This example shows how async/await enables concurrent execution, reducing total wait time from 3300ms (sequential) to 2500ms (concurrent) - a 33% performance improvement!