← Back to C++ Course | Chapter 2: Input & Output | Lesson 2 of 8

C++ में Numbers Print करना

cout << numeric literals और expressions के results सीधे print कर सकता है, और iomanip से setprecision/setw decimal places और field width पर extra control देते हैं।
Syntax
cpp
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

cpp
// 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;
}

Arithmetic Results Print करना

जब cout को एक mathematical expression दिया जाए, C++ पहले उस expression को पूरी तरह evaluate करता है और सिर्फ तब single resulting number print करता है, original expression text नहीं।

उदाहरण: Printing Arithmetic Results

cpp
#include <iostream>

int main() {
	std::cout << 5 + 3 << std::endl; // prints 8, not "5 + 3"
	return 0;
}

अलग Number Types Print करना

cout << C++ में हर numeric type के साथ काम करता है -- short, int, long, float, और double -- और हर एक को उस type के अपने conventions के हिसाब से formatted print करता है।

उदाहरण: Printing Different Number Types

cpp
// 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;
}

Text के साथ Numbers जोड़ना

<< operator को एक single cout statement में कई बार chain किया जा सकता है, string literals और numeric values मिलाते हुए, और इस तरह combine की गई कोई भी number automatically इसके printable text form में convert हो जाती है।

उदाहरण: Concatenating Numbers with Text

cpp
// 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;
}

Output में Numbers Format करना

एक number कैसे दिखता है इस पर ज़्यादा control के लिए, iomanip library setprecision और fixed जैसे manipulators देती है decimal places की एक fixed संख्या के लिए, और setw एक minimum field width के लिए।

उदाहरण: Formatting Numbers in Output

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि cout << 5 / 2 2.5 print करेगा, जब दोनों ints हैं इसलिए यह 2 print करता है।
  2. cout << "Total: " + 5; लिखना, जो text और एक number जोड़ने की बजाय string पर pointer arithmetic करता है।
  3. यह भूल जाना कि 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.