C++ Programming Code Examples
C++ > Beginners Lab Assignments Code Examples
Write a program in C++ to formatting the output.
/* Write a program in C++ to formatting the output. */
#include <iostream>
#include <iomanip> // Needed to do formatted I/O
using namespace std;
int main()
{
cout << "\n\n Formatting the output :\n";
cout << "----------------------------\n";
double pi = 3.14159265; // this is floating point number
cout << fixed << setprecision(4); // number is set to display with 4 decimal places
cout <<" The value of pi : " << pi << endl;
cout << " The value of pi 4 decimal place of total width 8 : |" << setw(8) << pi << "|" << endl; // setw() sets the total width
cout << " The value of pi 4 decimal place of total width 10 : |" << setw(10) << pi << "|"<< endl;
cout << setfill('-'); // setfill() sets to fill the blanks with specified character
cout << " The value of pi 4 decimal place of total width 8 : |" << setw(8) << pi << "|" << endl;
cout << " The value of pi 4 decimal place of total width 10 : |" << setw(10) << pi << "|"<< endl;
cout << scientific; // set value in scientific format with exponent
cout <<" The value of pi in scientific format is : " << pi << endl;
bool done = false; // this is boolean variable
cout <<" Status in number : " << done << endl;
cout << boolalpha; // set output in alphabet true or false
cout <<" Status in alphabet : " << done << endl;
cout << endl;
return 0;
}
The floating-point precision determines the maximum number of digits to be written on insertion operations to express floating-point values. How this is interpreted depends on whether the floatfield format flag is set to a specific notation (either fixed or scientific) or it is unset (using the default notation, which is not necessarily equivalent to either fixed nor scientific). Get/Set floating-point decimal precision. The first form (1) returns the value of the current floating-point precision field for the stream. The second form (2) also sets it to a new value. Function returns the precision selected in the stream before the call.
Set fill character. Sets c as the stream's fill character. Behaves as if member fill were called with c as argument on the stream on which it is inserted as a manipulator (it can be inserted on output streams). The setfill() method of iomaip library in C++ is used to set the ios library fill character based on the character specified as the parameter to this method. This manipulator is declared in header <iomanip>.
A return statement ends the processing of the current function and returns control to the caller of the function. A value-returning function should include a return statement, containing an expression. If an expression is not given on a return statement in a function declared with a non-void return type, the compiler issues an error message. If the data type of the expression is different from the function return type, conversion of the return value takes place as if the value of the expression were assigned to an object with the same function return type.
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
Set field width. Sets the field width to be used on output operations. The C++ function std::setw behaves as if member width were called with n as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams). It is used to sets the field width to be used on output operations. This manipulator is declared in header <iomanip>. This method accepts n as a parameter which is the integer argument corresponding to which the field width is to be set. This function returns an object of unspecified type. The setw function should only be used as a stream manipulator.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
Set decimal precision. Sets the decimal precision to be used to format floating-point values on output operations. Behaves as if member precision were called with n as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams). This manipulator is declared in header <iomanip>. This method accepts n as a parameter which is the integer argument corresponding to which the floating-point precision is to be set.
C++ Sample code Print first 100 "Ramanujan Numbers" by TWO WAYS 1st way is printing ramunajan numbers from RN taking RN from 0 to INFINITY & print first 100 numbers. 2nd