# Help with dividing number by 2 homework

• (3 Pages)
• 1
• 2
• 3

## 33 Replies - 3276 Views - Last Post: 01 February 2013 - 02:06 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=310596&amp;s=369e4dd59351f802a1f0da15c6f35493&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 Annush

Reputation: 0
• Posts: 17
• Joined: 31-January 13

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 12:51 AM

thankyou so much everyone .. I got the answer .. thankyou thankyou!!

### #17 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 12:53 AM

Annush, on 01 February 2013 - 12:46 PM, said:

for that I guess I have to use loop..

Exactly...also you will notice that the value n is not being used here.

So, something like this would be fine...

```void half(int a)
{
int b;//not required here

while(a>0)
{
//print a
//do the calculation of a=a/2;
}
}
```

regards,
Raghav

This post has been edited by raghav.naganathan: 01 February 2013 - 01:11 AM

### #18 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 12:58 AM

Well, one more thing you notice here, the program will not work effectively for odd numbers...you might want to look into that as well.

regards,
Raghav

### #19 Annush

Reputation: 0
• Posts: 17
• Joined: 31-January 13

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 01:01 AM

aryt.. I got the answer .. thankyuh awl...!!

### #20 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 01:13 AM

You would notice that you will get the output something like this.

```100
50
25
12
6
3
1

```

but, 25/2=12.5 and not 12, so the effectiveness of the program suffers here.

regards,
Raghav

### #21 Annush

Reputation: 0
• Posts: 17
• Joined: 31-January 13

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 01:16 AM

I want the output like this

100
50
25
12
6
3
1

that's y em taking an int value..

### #22 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 01:25 AM

regards,
Raghav

### #23 Annush

Reputation: 0
• Posts: 17
• Joined: 31-January 13

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 01:27 AM

sure...

this is the code which I used..

#include<iostream>
using namespace std;
void half(int n);
int main()
{

half(1000); //although this will do nothing with the number 1000, it will help the function to get executed by calling it.
system ("pause");
return 0;

}

void half(int n)
{
int a,;
a=100;

cout<<a<<endl;

do
{
a=a/2;
cout<<a<<endl;

}
while(a>1);

}

### #24 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 01:34 AM

Well, here are the corrections that I would like to suggest...also I have removed some lines which were redundant.

```#include<iostream>
using namespace std;
void half(int n);//replace as half(int a);
int main()
{
half(1000);
system ("pause");
return 0;
}

void half(int n) //here too...replace as half(int a)
{
//int a,;
//a=100;
//Remove the above lines as they are unnecessary
cout<<a<<endl;
do
{
a=a/2;
cout<<a<<endl;
}
while(a>1);
}

```

regards,
Raghav

### #25 Annush

Reputation: 0
• Posts: 17
• Joined: 31-January 13

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 01:39 AM

I cant replace half(n) by half (a)
its giving me a error..

### #26 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 01:41 AM

Did you replace both the half(int n) by half(int a) ?

regards,
Raghav

### #27 Annush

Reputation: 0
• Posts: 17
• Joined: 31-January 13

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 01:44 AM

yes, I did..

### #28 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 01:50 AM

So,does your updated code look like this?

```01	#include<iostream>
02	using namespace std;
03	void half(int a);
04	int main()
05	{
06	half(1000);
07	system ("pause");
08	return 0;
09	}
10
11	void half(int a)
12	{
13	//int a,;
14	//a=100;
15	//Remove the above lines as they are unnecessary
16	cout<<a<<endl;
17	do
18	  {
19	  a=a/2;
20	  cout<<a<<endl;
21	  }
22	  while(a>1);
23	}
```

regards,
Raghav

### #29 Annush

Reputation: 0
• Posts: 17
• Joined: 31-January 13

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 01:52 AM

yes...

### #30 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 01:53 AM

Then there should be no problem...it should work perfectly as you wanted...what are the errors you are getting?

regards,
Raghav

• (3 Pages)
• 1
• 2
• 3

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}