Hello, I need to compare the 2 numbers entered from the keyboard, say, M1 and M2. Compare them with each other and display the text: “Number M1> M2” or “Number M1 <M2”. Here is what I did:

#include "stdafx.h" #include <stdio.h> #include <conio.h> #include <iostream> #include <locale.h> int main() { int a; int b; int max, min; // настаиваем консоль на вывод кириллицы setlocale(LC_ALL, "Russian"); // название программы printf("программа для сравнения чисел .n"); // получаем данные от пользователя printf("nВведите первое число: "); printf("nВведите второе число: "); if (a > b) printf("max=%dn", a) else (b > a) printf("max=%dn", b); // выводим результат на экран printf("nМаксимальное число %dnn", max); return 0; } 

But does not work. what cant?

  • one
    The basic cant is that you need to compile the compiler before asking such questions. Then they can be formulated more accurately. - avp

4 answers 4

You forgot to count numbers from the console:

 // получаем данные от пользователя printf("\nВведите первое число: "); scanf("%d", &a); printf("\nВведите второе число: "); scanf("%d", &b); 

    Errors 3:

    1. The user does not read the numbers a and b entered by the user.
    2. Nowhere is max calculated.
    3. At the end of the condition, if not worth it ; .

      you are missing the reading of a & b:

       scanf("%d", &a); scanf("%d", &b); 

      then in if-else, if you want to check in else, that b> a - add if after else:

       else if(b > a) 

      and the max variable, you are left uninitialized, in the if execution branches, assign the value to the desired max variable:

       if(a > b) { max = a; ...} 
         if (a>b ) printf("max=%d\n",a) else (b>a) printf("max=%d\n",b); 

        Syntactically not true. After else there can be no conditions.

         if (a > b) .... else .... 

        If you need a condition in else, it is done like this:

         if (a > b) ... else if (a == b)... else ...