MarathiCode Official Examples
Fifteen MarathiCode v0.2 programs covering output, variables, conditions, loops, operators, logic, functions, scope, lists, reading input, the empty value रिक्त, how text and numbers behave, and error messages. Each one runs unchanged in the Playground and prints exactly the output shown; programs that read input open with their sample input filled in. The same programs are in the Playground's example menu.
1. Hello World (नमस्कार)
hello.mr
The smallest MarathiCode program: छापा prints a value on its own line.
छापा("नमस्कार, जग!")
छापा("मराठीकोड v0.2")- •छापा prints the value in parentheses, followed by a line break.
- •Strings are written in double quotes and can contain any Marathi or English text.
2. Variables & Arithmetic (चल व अंकगणित)
variables.mr
Declares variables with चल, mixes ASCII and Devanagari digits, and joins text with +.
चल लांबी_मी = १२
चल रुंदी_मी = 7.5
चल क्षेत्रफळ = लांबी_मी * रुंदी_मी
छापा("क्षेत्रफळ: " + क्षेत्रफळ)
छापा("परिमिती: " + 2 * (लांबी_मी + रुंदी_मी))
छापा(17 % 5)- •चल declares a variable. Names can be written in Devanagari.
- •१२ (Devanagari digits) and 7.5 (ASCII) are both numbers; printed numbers use ASCII digits.
- •When one side of + is a string, the other side is converted to text and joined.
- •% gives the remainder: 17 % 5 is 2.
3. Conditional Branching (जर - नाहीतर जर - नाहीतर)
grade.mr
Chooses one of several branches with जर (if), नाहीतर जर (else-if) and नाहीतर (else).
चल गुण = ७५
जर गुण >= ९० तर {
छापा("उत्कृष्ट")
} नाहीतर जर गुण >= ६० तर {
छापा("प्रथम श्रेणी")
} नाहीतर जर गुण >= ३५ तर {
छापा("उत्तीर्ण")
} नाहीतर {
छापा("अनुत्तीर्ण")
}- •गुण >= ९० is खोटे, so the first branch is skipped.
- •गुण >= ६० is खरे, so प्रथम श्रेणी is printed.
- •Only the first matching branch runs; the remaining conditions are not checked.
4. While Loop (पर्यंत)
digits_sum.mr
Repeats a block while a condition is true: here, adding up the digits of a number.
चल संख्या = 4729
चल बेरीज = 0
पर्यंत संख्या > 0 {
चल अंक = संख्या % 10
बेरीज = बेरीज + अंक
संख्या = (संख्या - अंक) / 10
}
छापा("अंकांची बेरीज: " + बेरीज)- •The condition संख्या > 0 is checked before every iteration.
- •Each iteration takes the last digit with % 10 and removes it.
- •बेरीज is updated without चल, so the outer variable changes; अंक is new in each iteration.
- •4 + 7 + 2 + 9 = 22.
5. For Loop with Break & Continue (साठी - थांब / पुढे)
for_loop.mr
A counted loop that skips some iterations with पुढे and stops early with थांब.
साठी (i = 1; i <= 10; i = i + 1) {
जर i % 3 == 0 तर {
पुढे
}
जर i > 7 तर {
थांब
}
छापा(i)
}- •साठी (start; condition; update) counts i from 1 upward.
- •पुढे skips multiples of 3 (3 and 6); the update i = i + 1 still runs.
- •At i = 8, थांब leaves the loop, so 9 and 10 are never reached.
6. Operators & Precedence (ऑपरेटर व क्रम)
operators.mr
Arithmetic and comparison operators, and the order in which they are applied.
// अंकगणित
छापा(7 + 3 * 2)
छापा((7 + 3) * 2)
छापा(10 / 4)
छापा(17 % 5)
छापा(-7 % 3)
// तुलना
छापा(5 > 3)
छापा(2 + 2 == 4)
छापा("अ" != "ब")- •* and / are applied before + and -, so 7 + 3 * 2 is 13; parentheses change the order.
- •/ always gives the exact result (10 / 4 is 2.5) and % gives the remainder.
- •The remainder takes the sign of the left side: -7 % 3 is -1.
- •Comparisons (>, ==, !=) give खरे or खोटे and work on numbers and text.
7. Logic (आणि / किंवा / नाही)
logic.mr
Combines conditions with आणि (and), किंवा (or) and नाही (not), and shows short-circuit evaluation.
चल वय = 20
चल परवाना = खरे
जर वय >= 18 आणि परवाना तर {
छापा("गाडी चालवू शकतो")
}
छापा(खोटे किंवा खरे)
छापा(नाही खरे)
// शॉर्ट-सर्किट: उजवी बाजू तपासलीच जात नाही
कार्य तपासा() {
छापा("तपासले!")
परत खरे
}
छापा(खोटे आणि तपासा())
छापा(खरे किंवा तपासा())- •आणि is खरे only when both sides are; किंवा when at least one side is; नाही flips a value.
- •Short-circuit: in खोटे आणि तपासा() the answer is already खोटे, so तपासा() is never called and तपासले! is never printed.
- •Likewise खरे किंवा तपासा() skips the call because the answer is already खरे.
8. Functions & Recursion (कार्य व परत)
factorial.mr
Defines functions with कार्य, returns values with परत, and calls a function recursively.
कार्य फॅक्टोरिअल(n) {
जर n <= 1 तर {
परत 1
}
परत n * फॅक्टोरिअल(n - 1)
}
कार्य सम_आहे(n) {
परत n % 2 == 0
}
छापा(फॅक्टोरिअल(5))
छापा(सम_आहे(10))
छापा(सम_आहे(7))- •फॅक्टोरिअल calls itself with n - 1 until it reaches the base case n <= 1.
- •5 × 4 × 3 × 2 × 1 = 120.
- •सम_आहे returns the result of a comparison, which prints as खरे or खोटे.
9. Scope (व्याप्ती)
scope.mr
Where a variable can be seen: inside a block, inside a function, or everywhere.
चल x = "जागतिक"
जर खरे तर {
चल x = "ब्लॉकमधील"
छापा(x)
}
छापा(x)
चल गणना = 0
कार्य वाढवा() {
चल स्थानिक = 10
गणना = गणना + स्थानिक
}
वाढवा()
वाढवा()
छापा("गणना: " + गणना)- •चल inside a { } block creates a new variable that exists only in that block, so the outer x is unchanged.
- •Assigning without चल (गणना = ...) updates the existing outer variable.
- •स्थानिक is local to वाढवा and disappears when the function returns; two calls add 10 twice.
10. Lists & Nested Lists (यादी)
lists.mr
Builds a list with जोडा, reads and changes items by index, and walks a nested list.
चल फळे = ["आंबा", "केळी"]
जोडा(फळे, "पेरू")
फळे[0] = "हापूस आंबा"
छापा(फळे)
छापा(लांबी(फळे))
चल गुण = [[80, 90], [70, 60]]
साठी (i = 0; i < लांबी(गुण); i = i + 1) {
छापा("विद्यार्थी " + i + ": " + (गुण[i][0] + गुण[i][1]))
}- •जोडा adds an item to the end of a list; लांबी gives the number of items.
- •Items are numbered from 0, and फळे[0] = ... replaces the first item.
- •गुण[i][0] reads from a nested list: the first number in row i.
- •Indexing past the end of a list is a runtime error.
11. Reading Input (वाचा व संख्या)
greet.mr
Reads lines typed by the user with वाचा and turns text into a number with संख्या.
चल नाव = वाचा()
जर नाव == रिक्त तर {
नाव = "मित्रा"
}
छापा("नमस्कार, " + नाव + "!")
चल ओळ = वाचा()
जर ओळ != रिक्त तर {
चल वय = संख्या(ओळ)
छापा("पुढच्या वर्षी तुमचे वय " + (वय + 1) + " असेल.")
}- •Each वाचा() call reads one line of input as text. In the Playground, type it into the Input box.
- •संख्या("२५") converts text to a number, so वय + 1 is 26 rather than the text "२५1".
- •When there is no more input, वाचा() returns रिक्त, so the program still works with no input at all.
12. Reading Until the End (पर्यंत + वाचा)
average.mr
Reads any number of marks, one per line, until the input runs out, then prints the average.
चल एकूण = 0
चल किती = 0
चल ओळ = वाचा()
पर्यंत ओळ != रिक्त {
एकूण = एकूण + संख्या(ओळ)
किती = किती + 1
ओळ = वाचा()
}
जर किती == 0 तर {
छापा("एकही गुण मिळाला नाही")
} नाहीतर {
छापा("विषय: " + किती)
छापा("एकूण: " + एकूण)
छापा("सरासरी: " + एकूण / किती)
}- •The loop keeps going while ओळ != रिक्त, that is, until every input line has been read.
- •The input uses Devanagari digits; संख्या accepts them just like ASCII digits.
- •With no input at all, किती stays 0 and the program prints एकही गुण मिळाला नाही instead of dividing by zero.
13. The Empty Value (रिक्त)
search.mr
A search function that returns the position of an item, or रिक्त when it is not found.
कार्य शोधा(यादी, हवे) {
साठी (i = 0; i < लांबी(यादी); i = i + 1) {
जर यादी[i] == हवे तर {
परत i
}
}
परत रिक्त
}
चल शहरे = ["पुणे", "नाशिक", "नागपूर"]
छापा(शोधा(शहरे, "नाशिक"))
चल जागा = शोधा(शहरे, "मुंबई")
जर जागा == रिक्त तर {
छापा("मुंबई यादीत नाही")
}
छापा(जागा)- •रिक्त means "no value". It is what a function returns when it ends without परत a value.
- •परत i leaves the function as soon as the item is found; नाशिक is at position 1.
- •Compare with == रिक्त to check for it. रिक्त is not equal to 0 or खोटे, but it counts as false in a condition.
14. Text & Large Numbers (मजकूर व संख्या)
text_and_numbers.mr
How Devanagari text is measured and indexed, and how very large and decimal numbers are printed.
चल शब्द = "मराठी"
छापा(लांबी(शब्द))
छापा(शब्द[0] + शब्द[3])
छापा("आंबा" < "केळी")
कार्य फॅक्टोरिअल(n) {
जर n <= 1 तर {
परत 1
}
परत n * फॅक्टोरिअल(n - 1)
}
छापा(फॅक्टोरिअल(20))
छापा(फॅक्टोरिअल(25))
छापा(0.1 + 0.2)- •Text is counted in Unicode characters: मराठी is म + र + ा + ठ + ी, so its length is 5 and शब्द[3] is ठ.
- •Text compares character by character in Unicode order, so आंबा comes before केळी.
- •Every number is a 64-bit decimal number: 20! still prints in full, 25! switches to e+ notation, and 0.1 + 0.2 shows the usual rounding.
15. Error Messages (त्रुटी संदेश)
error.mr
A program with a deliberate mistake, showing how MarathiCode reports errors.
// ही चूक मुद्दाम आहे: यादीत फक्त ३ घटक आहेत
चल गुण = [80, 90, 75]
छापा("पहिले गुण: " + गुण[0])
छापा(गुण[3])धावपळ त्रुटी (Runtime Error)
ओळ 4:
छापा(गुण[3])
यादीची निर्देशांक मर्यादा ओलांडली (Index 3, length 3).- •Lines before the mistake run normally, so पहिले गुण: 80 is printed first.
- •गुण has 3 items numbered 0 to 2, so गुण[3] is outside the list and the program stops.
- •Every error names its kind, the line number and the line itself, with the message in Marathi and English.