# Help with dividing number by 2 homework

• (3 Pages)
• 1
• 2
• 3

## 33 Replies - 3112 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=484b2792b2b024ecc03de023ae21114c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Annush

• New D.I.C Head

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

# Help with dividing number by 2 homework

Posted 31 January 2013 - 04:08 PM

Make a function called half() that takes an integer argument. The function must print the number it received to the screen, then the program should divide that number by two to make a new number. If the new number is greater than zero the function then calls the function half() passing it the new number as its argument. If the number is zero or less than the function exits

Call the function half() with an argument of 100, the screen output should be
100
50
25
...
...
1

please suggest meh the correct coding for this
I have tried it with for loop, do-while loop, while loop, if statement still em not getting the answer.

Is This A Good Question/Topic? 0

## Replies To: Help with dividing number by 2 homework

### #2 andrewsw

• Well, I'll need some beer.

Reputation: 4978
• Posts: 18,880
• Joined: 12-December 12

## Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 04:12 PM

You should show your code. If not, it essentially means that you are just asking us to do this for you.

And you should revise your post-title; everything posted in this forum should be a question.. and the title is meant to help others find useful information.

This post has been edited by andrewsw: 31 January 2013 - 04:17 PM

### #3 BetaWar

• #include "soul.h"

Reputation: 1306
• Posts: 7,678
• Joined: 07-September 06

## Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 04:14 PM

It is most likely supposed to be a recursive function. Which I am assuming you have gone over in class one or two days this week.

You can find examples of recursive functions all over the place, but don't expect people to do your work for you. That isn't what this community is about. Normally the best way to get help is to post the code that you have tried (so people know you have put forth an effort) in the tags.

### #4 Annush

• New D.I.C Head

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

## Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 04:19 PM

here is the coding..

coding..
```#include<iostream>
using namespace std;
int half(int n);
int main()
{
int num1,x;
num1=100;
x=(num1/2);

cout<<num1<<endl;
cout<<x<<endl;

system ("pause");
return 0;
}
int half(int n)
{
int x;
if(x>0)
{
cout<<x<<endl;
}
else if(x<=0)
{
cout<<"exit"<<endl;
}
return x;
}

```

Mod edit - Please

### #5 BetaWar

• #include "soul.h"

Reputation: 1306
• Posts: 7,678
• Joined: 07-September 06

## Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 04:33 PM

Updated title.

Okay, so you have a function that does something, now you know you need to divide the input number by 2, so lets start by you dividing the number by two and outputting it instead. Once you have that code completed (and posted in ) we can move on to making the function do something more meaningful.

### #6 Annush

• New D.I.C Head

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

## Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 04:36 PM

at the above I had divided it by 2 but, still em not getting the answer why??
my out is just coming till 100 nd 50 em not getting rest of the numbers..

### #7 mojo666

Reputation: 383
• Posts: 832
• Joined: 27-June 09

## Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 04:45 PM

The assignment spells out step by step what you need to code. All you really have to do is translate each step into code.

```int main()
{
/*Call the function half() with an argument of 100*/

system("pause");
return 0;
}

void half(int n)  //made it void because you don't need to return anything.
{
/*The function must print the number it received to the screen*/

/*then the program should divide that number by two to make a new number*/

if(/*the new number is greater than zero*/)
{
/*the function then calls the function half() passing it the new number as its argument*/
}
else //the number is zero or less
{
/*the function exits*/
}
}
```

### #8 Annush

• New D.I.C Head

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

## Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 04:53 PM

I did it this way but, now I'm not geting anything in my output

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

system ("pause");
return 0;
}

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

if(a>0)
{
cout<<a<<endl;
}
else if(a<=0)
{
cout<<"exits"<<endl;
}
}

```

This post has been edited by Skydiver: 31 January 2013 - 08:46 PM
Reason for edit:: Added code tags.

### #9 Skydiver

• Code herder

Reputation: 4371
• Posts: 14,100
• Joined: 05-May 12

## Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 08:46 PM

It usually helps if you call your function at least once.

### #10 Annush

• New D.I.C Head

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

## Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 09:01 PM

so, how should I call the function..?
I'm trying but, I'm not able to get it ..

### #11 jimblumberg

Reputation: 4734
• Posts: 14,759
• Joined: 25-December 09

## Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 09:28 PM

I suggest you start by reading the function tutorials contained in my signature. They should help you better understand functions. This link may also be of some use:Functions I.

Jim

### #12 raghav.naganathan

• Perfectly Squared ;)

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

## Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 11:00 PM

Well, you need to call your function in the main

calling function is something like this.
```void some_function( int something) //function definition
{
cout<<"Hi...I am present in function definition and being called from main()";
}

int main()
{
some_function(3); //Calling function
}

```

regards,
Raghav

This post has been edited by raghav.naganathan: 31 January 2013 - 11:01 PM

### #13 Annush

• New D.I.C Head

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

## Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 11:29 PM

I'm seriously not getting the answer :S

### #14 raghav.naganathan

• Perfectly Squared ;)

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

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 12:07 AM

Well, if I would do the same in your program, it would be something like this.

```#include<iostream>
02	using namespace std;
03	void half(int n);
04	int main()
05	{
06	half(1000); //although this will do nothing with the number 1000, it will help the function to get executed by calling it.
07	system ("pause");
08	return 0;
09	}
10
11	void half(int n)
12	{
13	     int a,b;
14	     a=100;
15	     b=a/2;
16
17	     if(a>0)
18	     {
19	            cout<<a<<endl;
20	            }
21	     else if(a<=0)
22	     {
23	          cout<<"exits"<<endl;
24	          }
25	}
```

regards,
Raghav

### #15 Annush

• New D.I.C Head

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

## Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 12:16 AM

by this I'm only getting my output 100

but, I want my output to be like this

100
50
25
...
...
1

and for that I guess I have to use loop..