A program to find out the number of odd and even Fibonacci numbers between given range
I'm going to assume you tested it for several inputs and got the right values. Your logic seems right so if a couple test cases work, it probably works. But I didn't look carefully for any off-by-one like errors.

There is one logic issue that I see:

You don't set n and  m if N = M. Double check your two if statements in the beginning of the loop; you only check for strict inequality on both sides. One of them should change to a &gt;= or &lt;=, or better yet just use if-else instead of two ifs.

As for optimization...

In terms of an average computer, your program is good enough for any N and M because you specify N,M &lt; 1018. Even if you don't, for-loops by themselves are pretty cheap.

However, there are still optimizations/cleanups. 

The biggest one is your last for loop:

for (j=0; j&lt;val; j++)
{
    even++;
    odd+=2;
}


This can just be: 

even+=val;
odd+=2*val


Your two if chains can turn into if, else if, else instead of three ifs (same thing for your first if pair, which should just be if else)

if (...) { }
else if (...) { }
else (...) { }


although the compiler will most likely optimize that for you.

I think that's the best you can do for your current algorithm.

However, it seems odd to me that you are checking for all mod cases for n and m; I think your idea is that you know the pattern for the large chunks in between N and M but you want to trim off the edge cases. Now, you won't need to trim down the case when n%3==1 or m%3==0, because you can just treat that as the even++, odd+=2 chunk.

n=m case

In programming you can always cop out :)

Here is how I would address the n=m case even though it's not too beautiful:

if (N == M)
{
  printf("Odd: 0, Even: 0"); //format this to match your output
  break;  // break ends the current for loop iteration in this context
}
else if (N &gt; M)
{
  n = M; m = N;
}
else
{
  n = N; m = M;
}