C# · Chapter 3.8 · Intermediate level

Methods that Return Values + Math + User Input

Value लौटाने वाले Methods + Math + यूज़र Input

A method can give back an answer, like a juicer gives juice. The return type is written before the name: static int Damage(int power, int level) { return power * level; }. void means it returns nothing. C# also has a built-in Math toolbox (Math.Max, Math.Sqrt, Math.Round) and Console.ReadLine() to read what the user types — convert it with int.Parse(...). Remember: 7 / 2 is 3 for ints (decimal part is dropped); use 7.0 / 2 for 3.5.

Method जवाब वापस भी दे सकता है, जैसे जूसर जूस देता है। नाम से पहले return type लिखते हैं: static int Damage(int power, int level) { return power * level; }। void का मतलब कुछ नहीं लौटाता। C# में Math टूलबॉक्स भी है (Math.Max, Math.Sqrt, Math.Round) और Console.ReadLine() यूज़र का लिखा पढ़ता है — उसे int.Parse(...) से बदलो। याद रखो: ints में 7 / 2 = 3 (दशमलव हट जाता है); 3.5 के लिए 7.0 / 2 लिखो।

In the classroom you can run and edit this code in your browser, get hints and mark the chapter as done. क्लास में यह code ब्राउज़र में चलाकर बदल सकते हो।

Words and symbols in this chapter

static int Damage(int power, int level)
Returns an int; needs two int inputs (parameters).
int लौटाता है; दो int inputs (parameters) चाहिए।
return power * level;
Sends the result back to where the method was called.
नतीजा वहीं वापस भेजता है जहाँ method बुलाया गया।
Math.Max(a, b)
The bigger of two numbers. Also Math.Min, Math.Abs, Math.Sqrt.
दो नंबरों में बड़ा। साथ में Math.Min, Math.Abs, Math.Sqrt।
int.Parse(Console.ReadLine())
Reads a line typed by the user and turns it into an int.
यूज़र की लिखी लाइन पढ़कर उसे int में बदलता है।

Ready example

using System;

class Program {
    static int Damage(int power, int level) {
        return power * level;
    }

    static void Main() {
        Console.Write("Hero level? ");
        int level = int.Parse(Console.ReadLine());
        int hit = Damage(25, level);
        Console.WriteLine("Damage: " + hit);
        Console.WriteLine("Strongest: " + Math.Max(hit, 100));
        Console.WriteLine("7 / 2 = " + (7 / 2) + " but 7.0 / 2 = " + (7.0 / 2));
    }
}

Sample input typed into the Input box, one value per line:

7

What you will see: Change the level in the 📥 Input box and Run — the damage changes.

📥 Input box में level बदलो और Run करो — damage बदल जाएगा।

Line by line

static int Damage(int power, int level) { return power * level; }
A reusable calculator method.
दोबारा इस्तेमाल होने वाला calculator method।
int level = int.Parse(Console.ReadLine());
Takes the 📥 Input value (e.g. 7) as a number.
📥 Input की वैल्यू (जैसे 7) को नंबर बनाकर लेता है।
int hit = Damage(25, level);
Calls the method and stores the returned value.
Method बुलाता है और लौटी वैल्यू रख लेता है।
(7 / 2) vs (7.0 / 2)
Integer division drops decimals: 3 vs 3.5.
Integer division दशमलव हटा देता है: 3 बनाम 3.5।

Common mistake

✗ Wrong

static int Add(int a, int b) {
    Console.WriteLine(a + b);
}

✓ Right

static int Add(int a, int b) {
    return a + b;
}

A method with return type int must return a value (CS0161).

int return type वाले method को return से वैल्यू लौटानी होगी (CS0161)।

Your task

Create static int Square(int n) that returns n * n, call it with 9 and print the answer (81).

static int Square(int n) बनाओ जो n * n लौटाए, उसे 9 के साथ बुलाओ और जवाब (81) print करो।

  1. Above Main, write static int Square(int n) { return n * n; }.
    Main के ऊपर static int Square(int n) { return n * n; } लिखो।
  2. Inside Main, write Console.WriteLine(Square(9));.
    Main के अंदर Console.WriteLine(Square(9)); लिखो।
  3. Run — you should see 81.
    Run करो — 81 दिखना चाहिए।
Starter code for the task
using System;

class Program {
    // TODO 1: static int Square(int n) method banao jo n * n return kare

    static void Main() {
        // TODO 2: Square(9) ko call karke result print karo
    }
}

Do this task in the classroom: your code is checked automatically and you get hints if something is missing.

Recap

Quick quiz

In C#, what is 7 / 2 when both are int?

C# में दोनों int हों तो 7 / 2 क्या होगा?

  1. 3.5
  2. 3
  3. 4
  4. Error
Show the answer

Answer: B (3)

Integer division drops the decimal part. Use 7.0 / 2 for 3.5.

Integer division दशमलव भाग हटा देता है। 3.5 के लिए 7.0 / 2 लिखो।

Practice problems for this chapter

Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.