how can i exit the program without it returning a value?

output is 0 when entered 1001 or something greater than it.

it should give 0 when the number isn't abundant and should exit if entered number is greater than the limit ,have tried using goto exit.

#include <stdio.h>
void main()
{
    int n;
    printf("Enter the number\n");
    scanf("%d", &n);
    int i, sum = 0;
    if (1 <= n <= 1000)
    {
        for (i = 2; i < n; i++)
        {
            if (n % i == 0)
                sum = sum + i;
        }
        (sum > n) ? printf("1") : printf("0");
    }
    else
        return;
}

Upvotes: 1

Views: 55

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311166

The condition in this if statement

if (1 <= n <= 1000)

is equivalent to

if ( ( 1 <= n ) <= 1000)

the result of the sub-expression 1 <= n is either 0 or 1. So this value is in any case less than 1000.

From the C Standard (6.5.8 Relational operators)

6 Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int.

You need to write

if (1 <= n && n <= 1000)

using the logical AND operator.

Pay attention to that according to the C Standard the function main without parameters shall be declared like

int main( void )

Upvotes: 1

Related Questions