# need help here

Page 1 of 1

## 6 Replies - 485 Views - Last Post: 03 October 2008 - 09:10 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=65970&amp;s=347acb5fb33501d3a2e46df18dfeb9aa&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 khestoi

Reputation: 0
• Posts: 13
• Joined: 30-September 08

# need help here

Posted 03 October 2008 - 08:12 AM

I just dont get this code.
Mind if you can help me guys explain this thing?

```#include <iostream.h>

int n(int x) {
return ( x ? (x > 0 ? n(x-1) * x: 0) : 1 );
}

int main() {
int number;

cout << "Enter an integer: ";
cin >> number;

cout << "n(" << number << ") = " << n(number) << endl;

return 0;
}

```

I know that the output is the factorial of the number but i cant understand the code.

This post has been edited by khestoi: 03 October 2008 - 08:15 AM

Is This A Good Question/Topic? 0

## Replies To: need help here

### #2 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

## Re: need help here

Posted 03 October 2008 - 08:15 AM

It is trying to check whether the entered string is palindrome. [I haven't tested it, so I don't know whether the logic works . But I think it may work.]

### #3 khestoi

Reputation: 0
• Posts: 13
• Joined: 30-September 08

## Re: need help here

Posted 03 October 2008 - 08:17 AM

AmitTheInfinity, on 3 Oct, 2008 - 08:15 AM, said:

It is trying to check whether the entered string is palindrome. [I haven't tested it, so I don't know whether the logic works . But I think it may work.]

sorry got the wrong code

### #4 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

## Re: need help here

Posted 03 October 2008 - 08:28 AM

khestoi, on 3 Oct, 2008 - 08:47 PM, said:

AmitTheInfinity, on 3 Oct, 2008 - 08:15 AM, said:

It is trying to check whether the entered string is palindrome. [I haven't tested it, so I don't know whether the logic works . But I think it may work.]

sorry got the wrong code

I can't beleive you did that. never mind.

The new code you posted is a recursive function to calculate factorial of a number. It also uses a ternary conditional operator in it.
x ? (x > 0 ? n(x-1) * x: 0) : 1 ); means

if x is not zero then
----if x is greater than zero then
--------result = call same function with n-1
--------return (result * x)
----else
--------return 0
else
----return 1

### #5 khestoi

Reputation: 0
• Posts: 13
• Joined: 30-September 08

## Re: need help here

Posted 03 October 2008 - 08:34 AM

Thanks!
Ur d man!
The loop is quite tricky.
Im not used to those kind of codes.

### #6 baavgai

• Dreaming Coder

Reputation: 6129
• Posts: 13,237
• Joined: 16-October 07

## Re: need help here

Posted 03 October 2008 - 08:48 AM

khestoi, on 3 Oct, 2008 - 11:12 AM, said:

```int n(int x) {
return ( x ? (x > 0 ? n(x-1) * x: 0) : 1 );
}

```

This is one of those cases where someone got way too happy with short syntax and murdered clarity.

To understand it, let's break it off into "if then" constructs:
```int n(int x) {
if (x) {
if (x > 0) {
return n(x-1) * x;
} else {
return 0;
}
} else {
return 1;
}
}

```

The "if (x)" is relying on an implicit conversion where 0 is false and everything else is true. Being explicit gives a little more clarity:
```	if (x!=0) {
if (x > 0) {
return n(x-1) * x;
} else {
return 0;
}
} else {
return 1;
}

```

The logic is a little wonky, let's see if you we can break it down a little:
```	if (x==0) {
return 1;
} else if (x > 0) {
return n(x-1) * x;
} else {
return 0;
}

```

There's actually no time that's you'd ever want to return a 0, so it's bugged. It will get there if the number is <0. Let's make it safe:
```int n(int x) {
if (x<2) {
return 1;
} else {
return n(x-1) * x;
}
}

```

Now it's safe and a little easier to follow. I like this form a little better:
```int n(int x) {
if (x<2) { return 1; }
return n(x-1) * x;
}

```

If you want to take it back to the inline:

```int n(int x) { return (x<2) ? 1 : (n(x-1) * x); }

```

As to what it's doing? It's doing recursion. We had fairly exhaustive explorations of that on this site, if you care to search.

Hope this helps.

Edit: Darn, AmitTheInfinity beat me to it. I'm leaving it anyway, took me a while to type.

This post has been edited by baavgai: 03 October 2008 - 08:50 AM

### #7 khestoi

Reputation: 0
• Posts: 13
• Joined: 30-September 08

## Re: need help here

Posted 03 October 2008 - 09:10 AM

Thanks for a much clearer explanation!