C++ में Numbers Print करना
In this page:
std::cout << number << std::endl;
std::cout << number1 operator number2 << std::endl;
Numeric Literals Print करना
cout << numeric literals सीधे print कर सकता है, बिना किसी conversion के, और number को exactly वैसे display करेगा जैसे लिखा गया, चाहे यह एक whole integer हो या एक decimal value।
उदाहरण: Printing Numeric Literals
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << 42 << std::endl;
// Print to the console with cout
std::cout << 3.14 << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Arithmetic Results Print करना
जब cout को एक mathematical expression दिया जाए, C++ पहले उस expression को पूरी तरह evaluate करता है और सिर्फ तब single resulting number print करता है, original expression text नहीं।
उदाहरण: Printing Arithmetic Results
#include <iostream>
int main() {
std::cout << 5 + 3 << std::endl; // prints 8, not "5 + 3"
return 0;
}
Login to try C/C++/Java/PHP code in the editor
अलग Number Types Print करना
cout << C++ में हर numeric type के साथ काम करता है -- short, int, long, float, और double -- और हर एक को उस type के अपने conventions के हिसाब से formatted print करता है।
उदाहरण: Printing Different Number Types
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare s and set it to 10
short s = 10;
// Declare i and set it to 1000
int i = 1000;
long l = 100000L;
float f = 1.5f;
double d = 3.14159;
// Print to the console with cout
std::cout << s << " " << i << " " << l << " " << f << " " << d << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Text के साथ Numbers जोड़ना
<< operator को एक single cout statement में कई बार chain किया जा सकता है, string literals और numeric values मिलाते हुए, और इस तरह combine की गई कोई भी number automatically इसके printable text form में convert हो जाती है।
उदाहरण: Concatenating Numbers with Text
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 95
int score = 95;
// Print to the console with cout
std::cout << "Score: " << score << " out of 100" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Output में Numbers Format करना
एक number कैसे दिखता है इस पर ज़्यादा control के लिए, iomanip library setprecision और fixed जैसे manipulators देती है decimal places की एक fixed संख्या के लिए, और setw एक minimum field width के लिए।
उदाहरण: Formatting Numbers in Output
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>
// Program execution starts in main()
int main() {
// Declare price and set it to 19.5
double price = 19.5;
// Print to the console with cout
std::cout << std::fixed << std::setprecision(2) << price << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि
cout << 5 / 22.5 print करेगा, जब दोनों ints हैं इसलिए यह 2 print करता है। cout << "Total: " + 5;लिखना, जो text और एक number जोड़ने की बजाय string पर pointer arithmetic करता है।- यह भूल जाना कि
cout << 10 + 5 << 3सब कुछ साथ add करने की बजाय separate values की तरह 153 print करता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: