B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP C++ Variables - TutorialsApp

C++ Variables

In C++, variables store values like numbers, characters, and decimals. Every variable must be declared with a data type.

C++ में variables मानों को store करते हैं जैसे numbers, characters, और decimals। हर variable को एक data type के साथ declare करना जरूरी है।

Syntax:

type variableName = value;

Example 1: Declare and print an integer

#include <iostream>
using namespace std;

int main() {
    int age = 20;
    cout << "Age is: " << age << endl;
    return 0;
}

This program declares an integer variable `age` and prints its value.

यह प्रोग्राम एक integer variable `age` को declare करता है और उसका value print करता है।

Output: Age is: 20


Example 2: Add two integers

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 5;
    int sum = a + b;
    cout << "Sum: " << sum << endl;
    return 0;
}

Two integers are declared and added, result is printed.

दो integers declare किए जाते हैं, उनका जोड़ निकाला जाता है और output में दिखाया जाता है।

Output: Sum: 15


Example 3: Floating-point variable

#include <iostream>
using namespace std;

int main() {
    float pi = 3.14;
    cout << "Value of pi: " << pi << endl;
    return 0;
}

A float variable stores decimal values.

यह program एक float variable declare करता है जो decimal value रखता है।

Output: Value of pi: 3.14


Example 4: Character variable

#include <iostream>
using namespace std;

int main() {
    char grade = 'A';
    cout << "Grade: " << grade << endl;
    return 0;
}

A character is stored using the `char` data type.

`char` data type का use करके एक character को store किया गया है।

Output: Grade: A


Example 5: Changing a variable value

#include <iostream>
using namespace std;

int main() {
    int x = 10;
    x = 15;
    cout << "x is now: " << x << endl;
    return 0;
}

You can change a variable’s value any time after declaration.

Variable की value को बाद में बदला जा सकता है।

Output: x is now: 15


Example 6: Declare multiple variables

#include <iostream>
using namespace std;

int main() {
    int a = 1, b = 2, c = 3;
    cout << a << " " << b << " " << c << endl;
    return 0;
}

You can declare and initialize multiple variables in one line.

एक ही line में multiple variables को declare और initialize किया गया है।

Output: 1 2 3


Example 7: Input using cin

#include <iostream>
using namespace std;

int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;
    cout << "You entered: " << num << endl;
    return 0;
}

User input is taken using `cin` and printed.

`cin` के माध्यम से input लिया गया है और फिर print किया गया है।

Output: You entered: 42 (if 42 was input)


Example 8: Variables in arithmetic

#include <iostream>
using namespace std;

int main() {
    float radius = 2.0;
    float area = 3.14f * radius * radius;
    cout << "Area: " << area << endl;
    return 0;
}

Variables can be used in mathematical formulas like area calculation.

Variables का उपयोग formulas में किया जा सकता है जैसे area निकालने के लिए।

Output: Area: 12.56


Example 9: Uninitialized variable

#include <iostream>
using namespace std;

int main() {
    int x;
    cout << "x: " << x << endl; // May print garbage
    return 0;
}

An uninitialized variable may give garbage output.

बिना initialize किया variable random (garbage) value दिखा सकता है।

Output: x: ??? (garbage value)


Example 10: Constant variable

#include <iostream>
using namespace std;

int main() {
    const float PI = 3.14159f;
    float r = 1.0f;
    float area = PI * r * r;
    cout << "Area: " << area << endl;
    return 0;
}

`const` is used to declare a variable whose value doesn't change.

`const` keyword ऐसे variable के लिए use किया जाता है जिसकी value नहीं बदलती।

Output: Area: 3.14