Manipulators of C++ endl , setw, setfill, setprecision

In C++, Manipulators are special type of operators used for formatting output as per programmer's choice.Some important manipulators are as follows:-
C++ में, मैनीपुलेटर विशेष प्रकार के ऑपरेटर होते है जो आउटपुट को प्रोग्रामर की इच्छानुसार फॉरमेट करते है। कुछ महत्वपूर्ण मैनीपुलेटर निम्न है:- 

1.) endl :-
This manipulator is end of line operator in C++. It shifts the cursor to the next line. It is similar to '\n' operator of C language.
यह मैनीपुलेटर C++  का एन्ड ऑफ़ लाइन ऑपरेटर है। यह कर्सर को अगली लाइन पर पहुचाने का कार्य करता है।यह C लैंग्वेज के '\n' ऑपरेटर के समान ही होता है।    

Example:- Simple interest program
#include<iostream.h>
void main(){
float p,r,t;
cout<"enter principle,rate and time "<<endl;
cin>>p>>r>>t;
float si=(p*r*t)/100;
cout<<"Simple Interest = "<<si<<,endl;
}

2.) setw(int w) :-
This manipulator sets the minimum field width (minimum number of characters) on output. this setw manipulator exist in iomanip.h header file.
यह मैनीपुलेटर आउटपुट हेतु न्यूनतम फील्ड विड्थ (करैक्टर की न्यूनतम संख्या) को सुनिश्चित करता है। यह setw मैनीपुलेटर iomanip.h हैडर फाइल में उपलब्ध होता है।   

Example:- Counting from 1 to 100 program
#include<iostrem.h>
#include<iomanip.h>
void main(){
int i;
cout<"Counting from 1 to 100"<<endl;
for(i=1;i<=100;i++){
cout<<setw(3)<<i<<" ";
if(i%10==0) cout<endl;
}
}

3.) setfill(char ch) :-
This is used after setw manipulator. If a value does not entirely fill a field, then setfill is used for filling the empty space of the field with a special character. this setfill manipulator exist in iomanip.h header file.
यह मैनीपुलेटर setw के पश्चात् प्रयुक्त किया जाता है। यदि वैल्यू द्वारा सम्पूर्ण फील्ड को नहीं भरा जाता है तब रिक्त स्थान को किसी विशेष अक्षर से भरने के लिए setfill का प्रयोग किया जाता है। यह setfill मैनीपुलेटर भी iomanip.h हैडर फाइल में उपलब्ध होता है।   

Example:-
#include<iostrem.h>
#include<iomanip.h>
void main(){
int i;
cout<"Counting from 1 to 100"<<endl;
for(i=1;i<=100;i++){
cout<<setfill('*')<<setw(3)<<i<<" ";
if(i%10==0) cout<endl;
}
}

4.) setprecision(int nod): This manipulator is used to specify the number of digits in fractional part of decimal to be printed in the output. this setprecision manipulator exist in iomanip.h header file.
यह फंक्शन आउटपुट में दर्शायी जाने वाली वास्तविक संख्या के आंशिक भाग में अंको की संख्या को परिभाषित करता है। यह setprecision मैनीपुलेटर भी iomanip.h हैडर फाइल में उपलब्ध होता है।   

Example:-
#include <iostream.h>
#include <iomanip.h>
void main(){  
double pi =3.14159;  
cout << setprecision(5) << pi <<endl; 
cout << setprecision(2) << pi <<endl;  
}

No comments:

Post a Comment