C++ Practical Programs Based on Fundamentals – 10 Basic C++ Programs with Output

C++ is a powerful general-purpose programming language used to develop various types of applications. Before learning advanced concepts of C++, it is important to understand its fundamental programming features through practical programs. This article contains 10 basic C++ practical programs based on fundamental concepts such as output statements, variables, data types, type casting, operators, mathematical calculations, and formatted output. All programs use standard C++ syntax and can be compiled using modern C++ compilers.

C++ एक Powerful General-Purpose Programming Language है, जिसका उपयोग विभिन्न प्रकार के Applications विकसित करने के लिए किया जाता है। C++ के Advanced Concepts सीखने से पहले इसके Fundamental Programming Features को Practical Programs के माध्यम से समझना महत्वपूर्ण है। इस Article में C++ के Fundamentals पर आधारित 10 Basic Practical Programs दिए गए हैं। इनमें Output Statements, Variables, Data Types, Type Casting, Operators, Mathematical Calculations और Formatted Output जैसे Concepts शामिल हैं। सभी Programs में Standard C++ Syntax का उपयोग किया गया है और इन्हें Modern C++ Compilers की सहायता से Compile किया जा सकता है।


1. C++ Program to Display Hello World | Hello World प्रदर्शित करने का C++ प्रोग्राम

Aim | उद्देश्य:

To write a C++ program to display the message "Hello World" on the screen.

Screen पर "Hello World" Message प्रदर्शित करने के लिए C++ Program लिखना।

Program:

#include <iostream>
using namespace std;
int main()
{
cout << "Hello World";
return 0;
}

Output:

Hello World

Explanation | व्याख्या:

In this program, #include <iostream> includes the input/output stream library. The main() function is the starting point of program execution. The cout statement displays the message "Hello World" on the screen.

इस Program में #include <iostream> Input/Output Stream Library को Include करता है। main() Function Program Execution का Starting Point होता है। cout Statement Screen पर "Hello World" Message Display करता है।


2. C++ Program to Find the ASCII Value of a Character | किसी Character का ASCII Value ज्ञात करने का C++ प्रोग्राम

Aim | उद्देश्य:

To write a C++ program to accept a character from the user and display its ASCII value.

User से एक Character प्राप्त करके उसका ASCII Value Display करने के लिए C++ Program लिखना।

Program:

#include <iostream>
using namespace std;
int main()
{
char ch;
cout << "Enter a character: ";
cin >> ch;
cout << "ASCII value = " << static_cast<int>(ch);
return 0;
}

Output:

Enter a character: A
ASCII value = 65

Explanation | व्याख्या:

In this program, the user enters a character that is stored in the variable ch. The static_cast<int>(ch) expression converts the character into its integer representation. For the character A, the ASCII value is 65.

इस Program में User एक Character Enter करता है, जिसे Variable ch में Store किया जाता है। static_cast<int>(ch) Expression Character को उसके Integer Representation में Convert करता है। Character A का ASCII Value 65 होता है।


3. C++ Program to Swap Two Variables Using a Third Variable | तीसरे Variable का उपयोग करके दो Variables की Values Swap करने का C++ प्रोग्राम

Aim | उद्देश्य:

To write a C++ program to interchange the values of two variables using a third temporary variable.

तीसरे Temporary Variable का उपयोग करके दो Variables की Values को आपस में बदलने के लिए C++ Program लिखना।

Program:

#include <iostream>
using namespace std;
int main()
{
int a, b, temp;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Before swapping: a = " << a
<< ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "After swapping: a = " << a
<< ", b = " << b;
return 0;
}

Output:

Enter two numbers: 10 20
Before swapping: a = 10, b = 20
After swapping: a = 20, b = 10

Explanation | व्याख्या:

In this program, three variables a, b, and temp are used. First, the value of a is stored in temp. Then the value of b is assigned to a, and finally the original value stored in temp is assigned to b. Thus, the values of both variables are exchanged.

इस Program में तीन Variables a, b और temp का उपयोग किया गया है। सबसे पहले a की Value को temp में Store किया जाता है। इसके बाद b की Value को a में Assign किया जाता है और अंत में temp में Store की गई Original Value को b में Assign किया जाता है। इस प्रकार दोनों Variables की Values आपस में बदल जाती हैं।


4. C++ Program to Swap Two Variables Without Using a Third Variable | तीसरे Variable के बिना दो Variables की Values Swap करने का C++ प्रोग्राम

Aim | उद्देश्य:

To write a C++ program to interchange the values of two variables without using a third variable.

तीसरे Variable का उपयोग किए बिना दो Variables की Values को आपस में बदलने के लिए C++ Program लिखना।

Program:

#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Before swapping: a = " << a
<< ", b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "After swapping: a = " << a
<< ", b = " << b;
return 0;
}

Output:

Enter two numbers: 10 20
Before swapping: a = 10, b = 20
After swapping: a = 20, b = 10

Explanation | व्याख्या:

In this program, the values of a and b are exchanged using arithmetic operations without declaring a third variable.

  • First, a = a + b stores the sum of both values in a.

  • Then, b = a - b assigns the original value of a to b.

  • Finally, a = a - b assigns the original value of b to a.

इस Program में तीसरा Variable Declare किए बिना Arithmetic Operations की सहायता से a और b की Values को Exchange किया जाता है।

  • सबसे पहले a = a + b दोनों Values का Sum a में Store करता है।

  • इसके बाद b = a - b में a की Original Value b में Assign हो जाती है।

  • अंत में a = a - b में b की Original Value a में Assign हो जाती है।

Note: This arithmetic method can cause integer overflow for sufficiently large values. In practical C++ programming, std::swap(a, b) is generally preferred.

नोट: बहुत बड़ी Integer Values के लिए इस Arithmetic Method से Integer Overflow हो सकता है। Practical C++ Programming में सामान्यतः std::swap(a, b) का उपयोग अधिक उपयुक्त होता है।


5. C++ Program to Calculate the Area and Circumference of a Circle | वृत्त का क्षेत्रफल और परिधि ज्ञात करने का C++ प्रोग्राम

Aim | उद्देश्य:

To write a C++ program to calculate the area and circumference of a circle using its radius.

वृत्त की Radius का उपयोग करके उसका Area और Circumference ज्ञात करने के लिए C++ Program लिखना।

Formula | सूत्र:

Area of Circle:

[
A = \pi r^2
]

Circumference of Circle:

[
C = 2\pi r
]

Where r is the radius of the circle and π (pi) is approximately 3.14159.

जहाँ r वृत्त की Radius है और π (Pi) का मान लगभग 3.14159 होता है।

Program:

#include <iostream>
using namespace std;
int main()
{
const double PI = 3.14159;
double radius, area, circumference;
cout << "Enter radius of circle: ";
cin >> radius;
area = PI * radius * radius;
circumference = 2 * PI * radius;
cout << "Area of circle = " << area << endl;
cout << "Circumference of circle = "
<< circumference;
return 0;
}

Output:

Enter radius of circle: 5
Area of circle = 78.5397
Circumference of circle = 31.4159

Explanation | व्याख्या:

In this program, the user enters the radius of the circle. The area is calculated by multiplying π by the square of the radius. The circumference is calculated by multiplying 2, π, and the radius. Finally, both results are displayed using cout.

इस Program में User वृत्त की Radius Enter करता है। Area की गणना π और Radius के Square का गुणा करके की जाती है। Circumference की गणना 2, π और Radius का गुणा करके की जाती है। अंत में दोनों Results को cout की सहायता से Display किया जाता है।


6. C++ Program to Calculate the Area of a Rectangle and Triangle | आयत और त्रिभुज का क्षेत्रफल ज्ञात करने का C++ प्रोग्राम

Aim | उद्देश्य:

To write a C++ program to calculate the area of a rectangle and a triangle using their respective dimensions.

आयत और त्रिभुज की संबंधित Dimensions का उपयोग करके उनका Area ज्ञात करने के लिए C++ Program लिखना।

Formula | सूत्र:

Area of Rectangle:

[
A = l \times w
]

Area of Triangle:

[
A = \frac{1}{2} \times b \times h
]

Where l is length, w is width, b is base, and h is height.

जहाँ l Length, w Width, b Base और h Height को दर्शाता है।

Program:

#include <iostream>
using namespace std;
int main()
{
double length, width;
double base, height;
double rectangleArea, triangleArea;
cout << "Enter length and width of rectangle: ";
cin >> length >> width;
cout << "Enter base and height of triangle: ";
cin >> base >> height;
rectangleArea = length * width;
triangleArea = 0.5 * base * height;
cout << "Area of rectangle = "
<< rectangleArea << endl;
cout << "Area of triangle = "
<< triangleArea;
return 0;
}

Output:

Enter length and width of rectangle: 10 5
Enter base and height of triangle: 8 6
Area of rectangle = 50
Area of triangle = 24

Explanation | व्याख्या:

In this program, the user enters the length and width of the rectangle and the base and height of the triangle. The area of the rectangle is calculated by multiplying length and width. The area of the triangle is calculated by multiplying its base and height by 0.5. Both results are displayed on the screen.

इस Program में User Rectangle की Length और Width तथा Triangle की Base और Height Enter करता है। Rectangle का Area Length और Width का गुणा करके Calculate किया जाता है। Triangle का Area उसकी Base और Height का 0.5 से गुणा करके Calculate किया जाता है। दोनों Results Screen पर Display किए जाते हैं।


7. C++ Program to Demonstrate Different Data Types | विभिन्न Data Types प्रदर्शित करने का C++ प्रोग्राम

Aim | उद्देश्य:

To write a C++ program to demonstrate different basic data types such as int, float, double, char, and bool.

int, float, double, char और bool जैसे विभिन्न Basic Data Types को प्रदर्शित करने के लिए C++ Program लिखना।

Program:

#include <iostream>
#include <string>
using namespace std;
int main()
{
int age = 25;
float percentage = 85.5f;
double salary = 45000.75;
char grade = 'A';
bool isPassed = true;
string name = "Rahul";
cout << "Name = " << name << endl;
cout << "Age = " << age << endl;
cout << "Percentage = " << percentage << endl;
cout << "Salary = " << salary << endl;
cout << "Grade = " << grade << endl;
cout << "Passed = " << boolalpha << isPassed << endl;
cout << "\nSize of int = " << sizeof(int) << " bytes";
cout << "\nSize of float = " << sizeof(float) << " bytes";
cout << "\nSize of double = " << sizeof(double) << " bytes";
cout << "\nSize of char = " << sizeof(char) << " byte";
cout << "\nSize of bool = " << sizeof(bool) << " byte";
return 0;
}

Output (Typical):

Name = Rahul
Age = 25
Percentage = 85.5
Salary = 45000.8
Grade = A
Passed = true
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
Size of bool = 1 byte

Explanation | व्याख्या:

In this program, different data types are used to store different kinds of values. The int data type stores integer values, float and double store floating-point values, char stores a single character, bool stores true or false, and string stores text. The sizeof() operator displays the memory size of each data type in bytes. The displayed sizes are typical and may vary for some data types depending on the compiler and platform.

इस Program में विभिन्न प्रकार की Values Store करने के लिए अलग-अलग Data Types का उपयोग किया गया है। int Integer Values, float और double Floating-Point Values, char एक Character, bool True या False तथा string Text Store करता है। sizeof() Operator प्रत्येक Data Type का Memory Size Bytes में Display करता है। दिए गए Sizes सामान्यतः प्रचलित हैं, लेकिन कुछ Data Types का Size Compiler और Platform के अनुसार अलग हो सकता है।


8. C++ Program to Demonstrate Type Casting | Type Casting प्रदर्शित करने का C++ प्रोग्राम

Aim | उद्देश्य:

To write a C++ program to demonstrate type casting by converting a value from one data type into another.

एक Data Type की Value को दूसरे Data Type में Convert करके Type Casting प्रदर्शित करने के लिए C++ Program लिखना।

Program:

#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 3;
double result;
result = static_cast<double>(a) / b;
cout << "Integer value a = " << a << endl;
cout << "Integer value b = " << b << endl;
cout << "Result after type casting = " << result;
return 0;
}

Output:

Integer value a = 10
Integer value b = 3
Result after type casting = 3.33333

Explanation | व्याख्या:

In this program, both a and b are integer variables. Normally, dividing two integers produces an integer result. The expression static_cast<double>(a) converts a into a double value before division. Therefore, the division produces a floating-point result instead of an integer result.

इस Program में a और b दोनों Integer Variables हैं। सामान्यतः दो Integers को Divide करने पर Integer Result प्राप्त होता है। Expression static_cast<double>(a) Division से पहले a को Double Value में Convert करता है। इसलिए Division का Result Integer के स्थान पर Floating-Point Value प्राप्त होता है।


9. C++ Program to Demonstrate the Use of Different Operators | विभिन्न Operators का उपयोग प्रदर्शित करने का C++ प्रोग्राम

Aim | उद्देश्य:

To write a C++ program to demonstrate different types of operators, such as arithmetic, relational, logical, assignment, increment, and conditional operators.

Arithmetic, Relational, Logical, Assignment, Increment तथा Conditional Operators जैसे विभिन्न प्रकार के Operators को प्रदर्शित करने के लिए C++ Program लिखना।

Program:

#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 5;
// Arithmetic Operators
cout << "Arithmetic Operators:" << endl;
cout << "Addition = " << a + b << endl;
cout << "Subtraction = " << a - b << endl;
cout << "Multiplication = " << a * b << endl;
cout << "Division = " << a / b << endl;
cout << "Modulus = " << a % b << endl;
// Relational Operators
cout << "\nRelational Operators:" << endl;
cout << "a > b = " << boolalpha << (a > b) << endl;
cout << "a == b = " << (a == b) << endl;
// Logical Operators
cout << "\nLogical Operators:" << endl;
cout << "(a > b && b > 0) = "
<< (a > b && b > 0) << endl;
// Assignment Operator
cout << "\nAssignment Operator:" << endl;
a += 5;
cout << "a after a += 5 = " << a << endl;
// Increment Operator
cout << "\nIncrement Operator:" << endl;
++b;
cout << "b after increment = " << b << endl;
// Conditional Operator
cout << "\nConditional Operator:" << endl;
int maximum = (a > b) ? a : b;
cout << "Maximum value = " << maximum << endl;
return 0;
}

Output:

Arithmetic Operators:
Addition = 15
Subtraction = 5
Multiplication = 50
Division = 2
Modulus = 0
Relational Operators:
a > b = true
a == b = false
Logical Operators:
(a > b && b > 0) = true
Assignment Operator:
a after a += 5 = 15
Increment Operator:
b after increment = 6
Conditional Operator:
Maximum value = 15

Explanation | व्याख्या:

This program demonstrates different categories of C++ operators. Arithmetic operators perform mathematical calculations. Relational operators compare two values. Logical operators combine conditions. Assignment operators assign or update values, increment operators increase a variable's value, and the conditional operator selects a value based on a condition.

यह Program C++ Operators की विभिन्न Categories को प्रदर्शित करता है। Arithmetic Operators Mathematical Calculations करते हैं। Relational Operators दो Values की तुलना करते हैं। Logical Operators Conditions को Combine करते हैं। Assignment Operators Values को Assign या Update करते हैं, Increment Operators किसी Variable की Value बढ़ाते हैं तथा Conditional Operator Condition के आधार पर एक Value Select करता है।


10. C++ Program to Demonstrate Formatted Output Using Manipulators | Manipulators का उपयोग करके Formatted Output प्रदर्शित करने का C++ प्रोग्राम

Aim | उद्देश्य:

To write a C++ program to demonstrate formatted output using manipulators such as setw(), setfill(), setprecision(), and fixed.

setw(), setfill(), setprecision() और fixed जैसे Manipulators का उपयोग करके Formatted Output प्रदर्शित करने के लिए C++ Program लिखना।

Program:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int rollNo = 101;
string name = "Rahul";
double marks = 85.5678;
cout << "Student Details" << endl;
cout << "-----------------------------" << endl;
cout << left << setw(15) << "Roll Number"
<< setw(15) << "Name" << "Marks" << endl;
cout << "-----------------------------" << endl;
cout << left << setw(15) << rollNo
<< setw(15) << name
<< fixed << setprecision(2) << marks << endl;
cout << "\nFormatted Number: "
<< setfill('*') << setw(10) << 123;
return 0;
}

Output:

Student Details
-----------------------------
Roll Number Name Marks
-----------------------------
101 Rahul 85.57
Formatted Number: *******123

Explanation | व्याख्या:

This program demonstrates the use of different output manipulators. The setw() manipulator sets the minimum field width, while left aligns the output to the left. The fixed and setprecision(2) manipulators display the floating-point value with two digits after the decimal point. The setfill('*') manipulator fills unused field positions with asterisks.

यह Program विभिन्न Output Manipulators के उपयोग को प्रदर्शित करता है। setw() Manipulator Minimum Field Width निर्धारित करता है, जबकि left Output को Left Side में Align करता है। fixed और setprecision(2) Manipulators Floating-Point Value को Decimal Point के बाद दो Digits के साथ Display करते हैं। setfill('*') Manipulator खाली Field Positions को Asterisk से भरता है।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

Complete C++ Programming Syllabus - From Basics to Advanced C++ , including OOP and Data Structures and Algorithms

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)