By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

is the output correct

P: 29
int n = 20;

while(n>0)
n/=2;
std::cout<<n * n<<std::endl;


is this correct if i divide 20/2 = 10
and multiply 10*10 as the output
Jun 12 '07 #1
Share this Question
Share on Google+
1 Reply


P: 94
The output of your code will be 0;
The while will continue until n = 0;

n = 20 while makes n = 10
n = 10 while makes n = 5
n = 5 while makes n = 2
n = 2 while makes n = 1
n = 1 while makes n = 0

perhaps what you mean to do is:

Expand|Select|Wrap|Line Numbers
  1. int n = 20;
  2.     while(n>0)
  3.     {
  4.         n/=2;    
  5.         std::cout <<n*n<< std::endl;
  6.     }
  7.  
Output will be
100
25
4
1
0
Jun 12 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.