chloramphenicol chloramphenicol - 1 month ago 4
C Question

C programming: Find the subtraction of two greatest numbers out of three

I'm starting to learn the c programming language and I have to write a program that subtracts two greatest numbers out of the three entered numbers. Can anyone help me with this?

Edit: Sorry, I still don't know how this site functions...

I know how to find the greatest number, but I'm not sure how to find the other one.

#include<stdio.h>
int main()
{
int a, b, c, d, max;

printf("Enter three numbers: ");
scanf("%d%d%d%d", &a, &b, &c);
max=a;
if (max<b) {max=b;}
if (max<c) {max=c;}
printf("the greatest number is %d\n", max);


return 0;
}

Answer

Don't expect to get answers to questions like yours. Try this. The assumption is that you are dealing with positive integers only. If you need to consider negative integers as well, you can do it yourself. You should note that this is the not the best solution, there can be much more elegant ones.

#include <stdio.h>

int main() {
    int numbers[3];
    printf("Enter number 1: \n");
    scanf("%d", &numbers[0]);

    printf("Enter number 2; \n");
    scanf("%d", &numbers[1]);

    printf("Enter number 3: \n");
    scanf("%d", &numbers[2]);

    printf("%d %d %d\n", numbers[0], numbers[1], numbers[2]);

    int maximum_0 = 0;
    int maximum_1 = 0;

    int i;


    for (i = 0; i < 3; i++) {
        if (numbers[i] > maximum_0) {
            maximum_0 = numbers[i];
        }
    }

    for (i = 0; i < 3; i++) {
        if (numbers[i] > maximum_1 && numbers[i] < maximum_0) {
            maximum_1 = numbers[i];
        }
    }

    printf("Result: %d\n", (maximum_0 - maximum_1)); 

    return 0;
}