473,471 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do we write odd function in an if-else statement

24 New Member
I'm facing some problems.. Its very basic but i cant seems to remember how to go abt doing it...

Im supposed to input in a value
if it is 1, then im supposed to stop the program,
if n is odd, then n=n*3+1,
else n=n/2

What ive written is

int n;

cin>>n;

if (n==1)
{
cout<<n<<endl;
}

else if (n==??) ---> I'm supposed to write this as 'if n is an odd number, answer will be as follows..
{
n = 3*n + 1;
}

else
{
n = n/2;
}

cout<<n<<endl;

I;m not sure how do i do the ODD part.. Can anyone kindly help me in this.. Thank u..
Jan 17 '07 #1
6 1906
Ganon11
3,652 Recognized Expert Specialist
If a number is odd, than it is not evenly divisible by 2 - in other words, it will have a remainder of 1. What operator can you use to check the remainder of a number divided by 2?
Jan 17 '07 #2
Nhd
24 New Member
If a number is odd, than it is not evenly divisible by 2 - in other words, it will have a remainder of 1. What operator can you use to check the remainder of a number divided by 2?

is the the % operator? meaning i shud juz write it as

if (n%2)
{
n=3*n+1;
}

ok, thk u so much, i get this already... i can go to the second part now... Thanks alot
Jan 17 '07 #3
shardul316
20 New Member
for odd number you have to use
if(n%2!=0)
{


n=3*n+1;

}

where n%2!=0 is the condition to check odd number.
for even number it condition will be n%2==0.
Jan 17 '07 #4
horace1
1,510 Recognized Expert Top Contributor
for odd number you have to use
if(n%2!=0)
{

n=3*n+1;
}
where n%2!=0 is the condition to check odd number.
for even number it condition will be n%2==0.
either
Expand|Select|Wrap|Line Numbers
  1. if(n%2!=0)
  2.  
which is true if the number is odd
or
Expand|Select|Wrap|Line Numbers
  1. if(n%2)
  2.  
which is non zero if the number is odd will work because C/C++ treats a non zero value as true
Jan 17 '07 #5
Nhd
24 New Member
Thanks shardul316 and horace1..

Ive goten that part right already...

However, may i noe how do we continuously continue this statement. i tried using a for loop, but it doesnt work..

after i input in 22, 11 will be the output that i'll get.. but im expected to get this output instead -> 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1..

which means that the output is continuously fed into the loop till i get 1, then it will terminate. is there anyone who might noe how to go abt starting this...

for eg, 22 is even, hence 22/2 is 11.
11 is odd, then 3*11+1 is 34.
34 is even, 34/2 is 17 and it goes on...

and how do we start counting cycle length?
Jan 18 '07 #6
horace1
1,510 Recognized Expert Top Contributor
try
Expand|Select|Wrap|Line Numbers
  1.             int n;
  2.             scanf("%d", &n);
  3.             while(n!=1)
  4.             if(n%2) 
  5.                {
  6.                 printf("odd %d ", n); 
  7.                 n = 3*n + 1; 
  8.                }
  9.             else  
  10.                {
  11.                printf("even %d ", n); 
  12.                n = n/2; 
  13.                }
  14.  
Jan 18 '07 #7

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

Similar topics

1
by: Bob Murdoch | last post by:
I have a situation where I call a COM object from ASP that is supposed to create a file. On occasion, especially during development, the output of the COM function is an error string rather than...
9
by: James Marshall | last post by:
I'm writing a library where I want to override document.write(), but for all document objects; thus, I want to put it in the prototype. I tried Document.prototype.write= my_doc_write ; but it...
2
by: Geoff Wilkins | last post by:
I am using <SCRIPT src=...> to import a Javascript routine from a remote source. I am then using some of the variables given values in the routine, in my own Javascript.. Unfortunately the...
14
by: Eli | last post by:
I've got a script that I'm trying to debug which uses document.write() to place HTML within a page. In both IE6 and Firefox when I view source, I see only the script itself and not any HTML as...
2
by: Eric Mitchell | last post by:
Hello all, I am using the document.write() method to create new content on the same page, however... I need to create a new button using this method (button in HTML). Complicating the matter...
2
by: bissatch | last post by:
Hi, I am trying to use JavaScript to write a table column on a web page. The code is as follows: <html> <head> <script> function displaycount() {
18
by: jacob navia | last post by:
In C, we have read-only memory (const), read/write memory (normal data), and write only memory. Let's look at the third one in more detail. Write only memory is a piece of RAM that can only...
1
by: Tony B | last post by:
Hi, I'm trying to understand a small cpp program which uses a function called write. An example of a line using this function is write (hsocket,strclose,strlen(strclose)); where strclose is a...
1
by: anupamaavadutha | last post by:
hi all, iam new to javascript. i have problem calling javascript functions.iam designing a calender page.here is my code. <%@ page...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.