Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> C++

सी ++ में जटिल संख्याएं

इस खंड में हम देखेंगे कि C++ में सम्मिश्र संख्याएँ कैसे बनाई जाती हैं और उनका उपयोग कैसे किया जाता है। हम सी ++ में जटिल संख्या वर्ग बना सकते हैं, जो कि जटिल संख्या के वास्तविक और काल्पनिक भाग को सदस्य तत्वों के रूप में रख सकता है। कुछ सदस्य कार्य होंगे जिनका उपयोग इस वर्ग को संभालने के लिए किया जाता है।

इस उदाहरण में हम एक जटिल प्रकार का वर्ग बना रहे हैं, एक जटिल संख्या को सही प्रारूप में प्रदर्शित करने के लिए एक फ़ंक्शन। दो सम्मिश्र संख्याओं आदि को जोड़ने और घटाने की दो अतिरिक्त विधियाँ।

उदाहरण

#include<iostream>
using namespace std;
class complex {
   int real, img;
   public:
      complex() {
         //default constructor to initialize complex number to 0+0i
         real = 0; img = 0;
      }
      complex(int r, int i) {
         //parameterized constructor to initialize complex number.
         real = r; img = i;
      }
      void set();
      void get();
      void display();
      friend complex add(complex, complex);
      friend complex sub(complex, complex);
};
void complex::set() {
   cout << "Enter Real part: ";
   cin >> real;
   cout << "Enter Imaginary Part: ";
   cin >> img;
}
void complex::get() {
   cout << "The complex number is: "<< real << "+" << img << "i" << endl;
}
void complex::display() {
   if(img < 0)
      if(img == -1)
         cout << "The complex number is: "<< real << "-i" << endl;
      else
         cout << "The complex number is: "<< real << img << "i" << endl;
      else
         if(img == 1)
            cout << "The complex number is: "<< real << " + i"<< endl;
         else
            cout << "The complex number is: "<< real << " + " << img << "i" <<
   endl;
}
complex add(complex c1, complex c2) {
   complex res;
   res.real = c1.real + c2.real;//addition for real part
   res.img = c1.img + c2.img;//addition for imaginary part
   return res;//the result after addition
}
complex sub(complex c1, complex c2) {
   complex res;
   res.real = c1.real - c2.real;//subtraction for real part
   res.img = c1.img - c2.img;//subtraction for imaginary part
   return res;//the result after subtraction
}
main() {
   complex n1(3, 2), n2(4, -3);
   complex result;
   result = add(n1,n2);
   result.display();
   result = sub(n1,n2);
   result.display();
}

आउटपुट

The complex number is: 7-i
The complex number is: -1 + 5i

  1. C++ . में इक्विडिजिटल नंबर

    इक्विडिजिटल संख्याएँ गणितीय रूप से विशेष संख्याएँ होती हैं जिनमें संख्या में अंकों की संख्या उसके अभाज्य गुणनखंड में संख्या के बराबर होती है। इस समस्या में, हमें एक पूर्णांक मान n दिया गया है। हमारा काम n तक के सभी सम-डिजिटल नंबरों के लिए एक प्रोग्राम बनाना है। समस्या को समझने के लिए एक उदाहरण लेते

  1. C++ में Emirp नंबर

    एमिर्प संख्या एक विशेष प्रकार की संख्या है जो एक अभाज्य संख्या है जिसके अंकों को उलटने पर एक अन्य अभाज्य संख्या बनती है (यह अभाज्य संख्या मूल संख्या से भिन्न होती है)। एमिर्प प्राइम का उल्टा है। कुछ अभाज्य संख्याएँ जो emirp नहीं हैं वे हैं पैलिंड्रोमिक अभाज्य और एकल अंक अभाज्य संख्याएँ। कुछ ए

  1. सी++ में डुडेनी नंबर्स

    संख्या सिद्धांत में परिभाषित एक गणितीय संख्या (विकिपीडिया)। नंबर हेनरी डुडेनी . द्वारा खोजा गया था . इसका गणितीय सूत्र है - यहाँ, हमें एक पूर्णांक n दिया गया है। हमारा काम जांच करना है कि दिया गया नंबर n एक डुडनी नंबर है या नहीं। समस्या को समझने के लिए एक उदाहरण लेते हैं, इनपुट: एन =17592 आ