Is there a more efficient way to have this C program print different things based on results of addition?

Viewed 90

I'm trying to write a C program which reads three integers, where if any two of those integers equal the remaining integer, it will print all integers within 1 and 10. If not, it will ask for three more integers and print the number of integers that are divisible by 6. This is my program, but it looks long-winded. Is there a cleaner way to do it? Thanks.

#include<stdio.h>

int main(){
    int x,y,z;
    
    printf("Enter three integers.\n");
    scanf("%d %d %d", &x, &y, &z);
    
    if(x + y == z){ 
        printf("1, 2, 3, 4, 5, 6, 7, 8, 9");
    }
    else if(x+z == y){
        printf("1, 2, 3, 4, 5, 6, 7, 8, 9");
    }
    else if(y+z == x){
        printf("1, 2, 3, 4, 5, 6, 7, 8, 9");
    }
    else {
        int a, b, c, count;
        count = 0;
        printf("Type in three more integers.\n");
        scanf("%d %d %d", &a, &b, &c); 
        if(a%6 == 0){
            count++;
        }
        if(b%6 == 0){
            count++;
        }
        if(c%6 == 0){
            count++;
        }
        printf("%d integers are divisible by 6.", count);
    }
}
3 Answers
  1. Can put three if conditions on one using || operator. This tutorial can help. link
  2. If only wish to check divisible for 3 integers (c % 6 == 0) + (b % 6 == 0) + (a % 6 == 0) can help to get the count.
  3. And reusing the same variables will help in optimizing memory.

Solution code :

#include <stdio.h>

int main()
{
    int x, y, z;
    printf("Enter three integers.\n");
    scanf("%d %d %d", &x, &y, &z);
    if(x + y == z || x+z == y || y+z == x)
    {
        printf("1, 2, 3, 4, 5, 6, 7, 8, 9\n");
    }
    else
    {
        printf("Type in three more integers.\n");
        scanf("%d %d %d", &x, &y, &z);
        printf("%d integers are divisible by 6\n", (x % 6 == 0) + (y % 6 == 0) + (z % 6 == 0));
    }
}

"Is there a cleaner way to do it?"
Yes! Write less code and reuse code and variables...

EDIT: removed bad code example and effected light "touch-ups"

void get3ints( char *prmpt, int *x, int *y, int *z ) {
    printf( "Enter three %sintegers.", prmpt, &x, &y, &z );
    scanf( "%d %d %d", x, y, z ); // != 3 /* omitting check */
}

int main() {
    int x, y, z;

    get3ints( "", &x, &y, &z );
    if( (+x+y-z) * (+x-y+z) * (-x+y+z) ) {
        get3ints( "more ", &x, &y, &z );
        printf( "%d integers are divisible by 6.\n", !(x%6) + !(y%6) + !(z%6) );
    } else
        printf( "1, 2, 3, 4, 5, 6, 7, 8, 9\n" );

    return 0;
}

Here is version that uses:

  1. Function input() to reduce duplicate code of the prompt and reading data from user.
  2. scanf() error handling
  3. Early return for less branched code
  4. Macro SUM to make variable part stand out, and derive the index of the right hand side to reduce chance of error.
  5. Macro MOD6 to make the variable part stand out.
  6. Reuse array variable a just to make it a line shorter. This is usually bad practice.
#include <stdio.h>

int input(const char *prompt, int *a) {
    printf("%s", prompt);
    if(scanf("%d %d %d", a, a + 1, a + 2) != 3) {
        printf("failed\n");
        return 1;
    }
    return 0;
}

int main() {
    int a[3];

    if(input("Enter three integers.\n", a))
        return 1;
    #define SUM(i, j) a[(i)] + a[(j)] == a[3 - (i) - (j)]
    if(SUM(0, 1) || SUM(0, 2) || SUM(1, 2)) {
        printf("1, 2, 3, 4, 5, 6, 7, 8, 9");
        return 0;
    }
    #undef SUM

    if(input("Type in three more integers.\n", a))
        return 1;
    #define MOD6(i) !(a[(i)] % 6)
    printf("%d integers are divisible by 6.\n", 
        MOD6(0) + MOD6(1) + MOD6(2)
    );
    #undef MOD6
}
Related