A program that tells the user whether one number is evenly divisible by another

int main()
{
    int num;
    int num2;
    printf("Enter two numbers\n");
    scanf("%i\n",&num);
    scanf("%i\n",&num2);

    if (num % num2 == 0){
        printf("%i is evenly divided by %i",num,num2);
    }
    else {
        printf("%i is not evenly divided by %i", num, num2);

    }
        return 0;
}

When I run the above code in the terminal, this is what happens

Enter two numbers
3
4


dsfa
3 is not evenly divided by 4

I entered two numbers, but then nothing happens until I enter some form of text (this is what is random "dsfa") and then the program will return with the correct operator printf. It should be text, I can’t just press the enter button (where do the spaces come from). Why does this program not return what I intend immediately after the user enters two numbers?

+4
source share
6 answers

An '\n' (, ) .

, scanf , .

+3

, '\n' ( ) scanf . , .

, %i , scanf . 0, (base 8). 0x 0x, ( 16). . , %i %d, .

, %i . , '\n' scanf .

#include <stdio.h>

int main(void)
{
    int num;
    int num2;
    printf("Enter two numbers\n");
    scanf("%i", &num);
    scanf("%i", &num2);

    if (num % num2 == 0){
        printf("%i is evenly divided by %i\n", num, num2);
    }
    else 
    {
        printf("%i is not evenly divided by %i\n", num, num2);
    }
    return 0;
}
+2

'\n' scanf(). , printf("\n") scanf() ( , printf()),

  printf("Enter two numbers\n");

  scanf("%i", &num);

  // printf("\n"); 

  scanf("%i", &num2);

  // printf("\n"); 

'\n' scanf() :

 scanf("%i", &num);
 scanf("%i", &num2);

scanf() :

scanf("%i%i",&num,&num2);

!

+1

\n scanf().

scanf("%i",&num);
scanf("%i",&num2);

.

0

'\n' scanf(); scanf();. scanf(); .

scanf("%i %i", &num, &num2);
0

\n scanf().

\n - escape-, , newline, tabspace ..

, Escape Sequence

So, just delete \nin the function scanf(). Another thing is that everything is correct ..

0
source

Source: https://habr.com/ru/post/1536966/


All Articles