Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Program not running after scanf

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 ).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading