Welcome to
| नमस्कार दोस्तों मैं अमित कुमार आज की इस पोस्ट में आपको C++ प्रोग्रामिंग लैंग्वेज के बारे में बताऊंगा। प्रोग्रामिंग लैंग्वेज के बारे में बेहतरीन जानकारी जानने के लिए पोस्ट को पूरा पढ़ते रहे। |
|---|
C++ एक सांख्यिकी प्रकार की स्वतंत्र और प्रपत्र सामान्य प्रयोग होने वाली भाषा है, यह एक मध्यस्तरीय भाषा के रूप में जानी जाती है, क्योंकि यह दोनों उच्च स्तर और निम्न स्तर की भाषा सुविधाओं का एक संयोजन है। C++ को ब्यार्ना स्त्रौस्त्रप (डेनिश-Bjarne Stroustrup) बेल लेबोरेटरीज में 1979 में शुरू किया था। C++ एक आब्जेक्ट उन्मुखी (ऑब्जेक्ट ओरिएंटेड) भाषा है। जिसका उपयोग Computer तथा Mobile प्रोग्राम बनाने के लिए किया जाता है। सी++ कई तरह के प्रोग्रामिंग शैलियों का समर्थन करने के हिसाब से रची गयी है। इसमें प्रोसीजरल प्रोग्रामिंग, वस्तु-केन्द्रित प्रोग्रामिंग (ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग), मॉड्युलर प्रोग्रामिंग, अथवा जेनेरिक प्रोग्रामिंग शैली में से किसी भी शैली को अपनाकर प्रोग्राम किया जा सकता है
C++ के कोड नीचे दिए गए हैं आप इन्हे समझे और प्रोग्रामिंग सीखे।
Welcome Program
#include < iostream >
int main() {
std::cout << "Welcome to Vivek Computer Education!";
return 0;
}
Example: Print Number Entered by User
#include < iostream >
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
cout << "You entered " << number;
return 0;
}
यह प्रोग्राम यूज़र से एक नंबर डालने के लिए कहता है। जब यूज़र कोई पूर्णांक (Integer) डालता है, तो वह Number नाम के वेरिएबल में स्टोर हो जाता है। फिर इसे Cout का इस्तेमाल करके स्क्रीन पर दिखाया जाता है।
Example: Program to Add Two Integers
include < iostream >
using namespace std;
int main() {
int first_number, second_number, sum;
cout << "Enter two integers: ";
cin >> first_number >> second_number;
// sum of two numbers in stored in variable sumOfTwoNumbers
sum = first_number + second_number;
// prints sum
cout << first_number << " + " << second_number << " = " << sum;
return 0;
}
इस प्रोग्राम में, यूज़र से दो इंटीजर (पूर्णांक) डालने के लिए कहा जाता है। ये दोनों इंटीजर क्रमशः first_number और second_number वेरिएबल में स्टोर किए जाते हैं। इसके बाद, + ऑपरेटर का इस्तेमाल करके इन वेरिएबल्स को जोड़ा जाता है और sum वेरिएबल में स्टोर किया जाता है। आखिर में, sum को स्क्रीन पर दिखाया जाता है।
Program to Find Quotient and Remainder
#include < iostream >
using namespace std;
int main()
{
int divisor, dividend, quotient, remainder;
cout << "Enter dividend: ";
cin >> dividend;
cout << "Enter divisor: ";
cin >> divisor;
quotient = dividend / divisor;
remainder = dividend % divisor;
cout << "Quotient = " << quotient << endl;
cout << "Remainder = " << remainder;
return 0;
}
Program to Find Size of int, float, double and char in Your System
#include < iostream >
using namespace std;
int main()
{
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
return 0;
}
Program to Swap Two Numbers
#include < iostream >
using namespace std;
int main()
{
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
Swap Numbers Without Using Temporary Variables
#include < iostream >
using namespace std;
int main()
{
int a = 5, b = 10;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
Whether Number is Even or Odd using if else
#include < iostream >
using namespace std;
int main() {
int n;
cout << "Enter an integer: ";
cin >> n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0;
}
Whether Number is Even or Odd using ternary operators
#include < iostream >
using namespace std;
int main() {
int n;
cout << "Enter an integer: ";
cin >> n;
(n % 2 == 0) ? cout << n << " is even." : cout << n << " is odd.";
return 0;
}
Check Vowel or a Consonant Manually
#include < iostream >
using namespace std;
int main() {
char c;
bool isLowercaseVowel, isUppercaseVowel;
cout << "Enter an alphabet: ";
cin >> c;
// evaluates to 1 (true) if c is a lowercase vowel
isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 (true) if c is an uppercase vowel
isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// show error message if c is not an alphabet
if (!isalpha(c))
printf("Error! Non-alphabetic character.");
else if (isLowercaseVowel || isUppercaseVowel)
cout << c << " is a vowel.";
else
cout << c << " is a consonant.";
return 0;
}
Find Largest Number Using if...else Statement
#include < iostream >
using namespace std;
int main() {
double n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
// check if n1 is the largest number
if(n1 >= n2 && n1 >= n3)
cout << "Largest number: " << n1;
// check if n2 is the largest number
else if(n2 >= n1 && n2 >= n3)
cout << "Largest number: " << n2;
// if neither n1 nor n2 are the largest, n3 is the largest
else
cout << "Largest number: " << n3;
return 0;
}
Find All Roots of a Quadratic Equation
#include < iostream >
#include < cmath >
using namespace std;
int main() {
float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
discriminant = b*b - 4*a*c;
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a); cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = -b/(2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
return 0;
}
Sum of Natural Numbers using loop
#include < iostream >
using namespace std;
int main() {
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i;
}
cout << "Sum = " << sum;
return 0;
}
Check Leap Year Using if...else Ladder
#include < iostream >
using namespace std;
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
// leap year if perfectly divisible by 400
if (year % 400 == 0) {
cout << year << " is a leap year.";
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
cout << year << " is not a leap year.";
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
cout << year << " is a leap year.";
}
// all other years are not leap years
else {
cout << year << " is not a leap year.";
}
return 0;
}
Check Leap Year Using Nested if
#include
using namespace std;
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
cout << year << " is a leap year.";
}
else {
cout << year << " is not a leap year.";
}
}
else {
cout << year << " is a leap year.";
}
}
else {
cout << year << " is not a leap year.";
}
return 0;
}
No comments:
Post a Comment