PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 17, 2022

[FIXED] How to print positive numbers with a prefix + in C++

 October 17, 2022     c++, int     No comments   

Issue

Is there any way to print the integer along with its sign in c++...i.e. by default if the number is negative we would get a - sign printed. In the same way can we get + before the positive numbers.

int x=-1;
cout<<"x="<<x;

gives output x=-1

but,..

int x=+1;
cout<<"x="<<x;

gives output as x=1 but how do i get it printed as x=+1

I know we can take cases by using if-else for x>0 and x<0;..but without using the if-else is there any direct way of printing in c++


Solution

Use std::showpos:

int x = 1;
std::cout << "x=" << std::showpos << x;


Answered By - NPE
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing