For loop counting between numbers

What is the problem with the code? It always return me the B value. I'm trying to count the numbers between two values. Thanks in advance!

`````` #include <stdio.h>
#include <stdlib.h>
int main(void) {

int a, b, i, cont;

printf("Digite um valor de Inicio: ");
scanf("%d", &a);

printf("Digite um valor de Fim: ");
scanf("%d", &b);

for(i=a;i<b;i++);

printf("Existem %d numeros entre os valores fornecidos", i);

system("pause")
return 0;
}
``````

3 answers

• answered 2019-09-10 02:34

Need to update cont when for loop execute i.e calculate numbers between two values. And then print the cont. Easy way: use b - a equation instead of loop.

• answered 2019-09-10 03:49

In C the following is the syntax of `for` loop.

``````for ( init expression; condition expression; update expression) {
statement(s);
}
``````

Note that, you can have multiple statements separated by `,` in each expression of the for loop.

To answer your question, you need to do,

``````for ( <initialize i with zero, initialize count to zero>;
<check to break the loop, it is already correct>;
<increment i, increment count>);
``````

• answered 2019-09-10 04:31

Set the counter:

``````if(!a < b){
printf("Numbers should be not equal");
return;
}
int count=0;
for(int i=a;i<b;i++)
{
count++;
}
count=count-1;
printf(i);
``````