15,942,710 members
See more:
Hi
I am trying to make a program that prints an interval. When the interval have numbers between 1 to 9 the number should be printed in text. If the number is greater than 9 it should just be written even when it is even (for example 10) and odd when it is odd (for example 11).
I hope the code is pretty explanatory also (you can see what the maximum interval is there also). Here is the code:

```// For Loop in C - Hackerrank

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{
int a = 1;
int b = 15;

//scanf("%d\n%d", &a, &b);
// Complete the code.

for(int i = 0; i < 10; i++) {

if ( (a > pow(10, 6)) || (a < 1) || (b > pow(10, 6)) || (b < 1) )  {
break;
}

for(int i = 0; ((i < 9) && (a < b)); i++) {
switch(a) {
case 1 :
printf("one\n");
break;
case 2 :
printf("two\n");
break;
case 3 :
printf("three\n");
break;
case 4 :
printf("four\n");
break;
case 5 :
printf("five\n");
break;
case 6 :
printf("six\n");
break;
case 7 :
printf("seven\n");
break;
case 8 :
printf("eight\n");
break;
case 9 :
printf("nine\n");
break;
default :
break;
};
a++;
}
printf("int a is: %d\n", a);

if (a%2 == 0) {
printf("even\n");
}
else {
printf("odd\n");
}
if (a >= b) {
break;
}
a++;

}
return 0;
}```

Now my problem is that in the last part in the code, the program goes from int a being 10 to int a being 15. No 11, 12, 13, 14 etc.. Why is this?

What I have tried:

I dont remember right now. I got to this point and it seems I can read the code but I cant figure out where the 15 comes without incrementation to 11, 12, 13 etc.
This code:
`printf("int a is: %d\n", a);`
is just there to see clearly the int a. It shouldnt be there in the final program.
Posted
Updated 20-May-21 9:13am
CHill60 20-May-21 4:55am
Step through the code using the debugger and you should be able to see exactly what happens
Mieczyslaw1683 20-May-21 5:03am
It (the debugger) is completly empty.
CHill60 20-May-21 5:21am
Not showing your code? Which IDE are you using?
Mieczyslaw1683 20-May-21 5:56am
CodeBlocks.
CHill60 20-May-21 6:50am
And your code is not loading into the debugger? Sounds like you haven't set it up correctly - see How to Use the Code::Blocks Debugger with C Programming - dummies[^]

## Solution 1

it is clearly a standard case to use the debugger and find it out yourself. It would clearly give you the hint where the buggy memory access is and you may fix it yourself.

Read some Debugger tutorial to get a grip on it and enhance your knowledge. Your code like very good, but I dont like such big if cases because the are too messy and so cause of bugs. But that is my 20+ experience. ;-)

## Solution 2

Here's is an excerpt from your code :
C++
```for(int i = 0; i < 10; i++)
{
if ( (a > pow(10, 6)) || (a < 1) || (b > pow(10, 6)) || (b < 1) )
{
break;
}

for(int i = 0; ((i < 9) && (a < b)); i++)
{
}
}
```
I have revised the braces and indentation in order to see it more clearly. Do you see the problem?

I am somewhat surprised this even compiles. I use Visual Studio and I have the warning level set to four always and warnings are errors. It would have told me the redefinition of the variable `i` hides the previous definition.

Try changing the inner loop indexer to be `j` and see what happens. In other words, make it look like this :
C++
```for( int j = 0; ((j < 9) && (a < b)); j++ )
{
}
```
Another thing - the `pow` function is very inefficient and should be avoided for the most part. Especially since it returns a floating point value and you are using it to compare with integers. You would be better off to use a macro definition and revise your loop. Something like this :
C++
```#define MAX_VALUE 1000000

if ( (a > MAX_VALUE) || (a < 1) || (b > MAX_VALUE) || (b < 1) )```

## Solution 3

Getting rid of the switch block shows what is going wrong:
C++
```int main()
{
int a = 1;
int b = 15;

//scanf("%d\n%d", &a, &b);
// Complete the code.

for(int i = 0; i < 10; i++) {
printf("outer i = %d\n", i);
for(int i = 0; ((i < 9) && (a < b)); i++) {
printf("i = %d, a = %d\n", i, a);
a++;
}
printf("int a is: %d\n", a);

if (a%2 == 0) {
printf("even\n");
}
else {
printf("odd\n");
}
if (a >= b) {
break;
}
a++;

}

return 0;
}```

## Solution 4

Okey. I got a discussion going so I can be kind and paste my solution also (I made it later):
```<pre>// For Loop in C - Hackerrank

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{
int a = 0;
int b = 0;

scanf("%d\n%d", &a, &b);
// Complete the code.

for(int i = 0; ; i++) {

if ( (a > pow(10, 6)) || (a < 1) || (b > pow(10, 6)) || (b < 1) )  {
break;
}

for(int i = 0; ((i < 9) && (a <= b) && (a < 10)); i++) {
switch(a) {
case 1 :
printf("one\n");
break;
case 2 :
printf("two\n");
break;
case 3 :
printf("three\n");
break;
case 4 :
printf("four\n");
break;
case 5 :
printf("five\n");
break;
case 6 :
printf("six\n");
break;
case 7 :
printf("seven\n");
break;
case 8 :
printf("eight\n");
break;
case 9 :
printf("nine\n");
break;
default :
break;
};
a++;
}
if (a > 9) {
if (a%2 == 0) {
printf("even\n");
}
else {
printf("odd\n");
}

}
if (a < b) {
a++;
} else {
break;
}

}
return 0;
}```