Search This Blog

Saturday 23 January 2016

Write a program to demonstrate the use of Manipulators (setw () and endl).

Practical 6


Endl

#include<iostream.h>


int main()
{

 char name[10];
 cout<<"Enter your name: ";                      //same line it will get your name
 cin>>num;

 cout<<"Hello, "<<name<<endl;                //next line will be printed in new line
 cout<<"Welcome to www.coscet.blogspot.in!!";
 getch();
 return 0;

}



output:

hello
welcome to www.coscet.blogspot.in


setw

Which basically set the width of field:
It is included in iomanip file

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>

int main()
{
 int num1,num2,num3;
 cout<<"Enter three numbers:\n";
 cin>>num1>>num2>>num3;

 cout<<"\nDisplaying the three numbers\n"
     <<"Num1:"<<setw(8)<<num1<<endl
     <<"Num2:"<<setw(8)<<num2<<endl      //field set is 8
     <<"Num3:"<<setw(8)<<num3<<endl;     //the output will be:    1234 !!

 getch();
 return 0;

}



output:
        1234



Another Example:

#include<iostream.h>
#include<iomanip.h>

int main()
{
     cout<<setw(10);
     cout<<"karan"<<endl;

     return 0;
}

No comments:

Post a Comment