-2

I am new to programming . I am trying to do an assignment but the code doesn't do anything after the first input to scanf . ( I am coding in VS code and i have to run the program again to get the result . until then nothing happens. ) I wrote an another simple code to check if anything is wrong with the code or if it is a IDE problem . Here it is :

#include <stdio.h>

int main () {
    int a, b;

    printf("Enter an positive Integer :  ");
    scanf("%i ", &a);
    b = a%2;

    if (b == 0) {
        printf("The given number  is an Even Integer. \a\n ");
    } else { 
        printf("The given Integer is an Odd integer. \n");
    }

    return 0;
} 

After the input if I enter a character or a number and then press 'Enter' then it prints out the result. Or else I have to run the program to print the result .

( I am using code runner ).

I referred to similar questions on stack overflow and applied some solutions like ' removing the \n in scanf ' and 'leaving a space before the %i in scanf ' but nothing works .

Your help is MUCH appreciated.

jarmod
  • 71,565
  • 16
  • 115
  • 122
  • 1
    it is not a VS code problem because i tried running the code in an online c compiler and sill it does the same thing mentioned above . – Tanmay Bhosale May 07 '22 at 16:51

2 Answers2

4

Remove the space after %i. Specifically, scanf("%i ", &a) should be scanf("%i", &a). This works for me.

MissSirius
  • 152
  • 11
1

Just replace the scanf ( "%i " , &a ) ; with scanf("%d",&a) and you are good to go ! for more explaination refer Refer this Question