Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,844 questions

51,765 answers

573 users

How to write a game that input 3 numbers that must be equal to random sum and random product of the numbers in C

1 Answer

0 votes
#include <time.h>
#include <stdio.h>
#include <stdlib.h> 
#include <stdbool.h>
 
bool play(int level, int max_level) {
    const int a = rand() % level + level;
    const int b = rand() % level + level;
    const int c = rand() % level + level;
     
    const int sumabc = a + b + c;
    const int productabc = a * b * c;
 
    printf("Level %d of %d\n", level, max_level);
    printf( "Enter 3 numbers:\n");
    printf("a + b + c must be = %d\n", sumabc);
    printf("a * b * c must be = %d\n", productabc);
 
    int usera, userb, userc;
    scanf("%d %d %d", &usera, &userb, &userc);
 
    int usersum = usera + userb + userc;
    int userproduct = usera * userb * userc;
 
    if (usersum == sumabc && userproduct == productabc) {
        printf("OK\n\n");
        return true;
    }
    else {
        printf("Wrong\n\n");
        return false;
    }
}
 
int main()
{
    srand(time(NULL));
     
    int level = 1;
    int const max_level = 3;
 
    while (level <= max_level) {
        bool LevelOK = play(level, max_level);
        fflush(stdin);
 
        if (LevelOK) {
            level++;
        }
    }
    printf("END\n");
     
    return 0;
}
 
 
 
/*
run 1:
 
Level 1 of 3
Enter 3 numbers:
a + b + c must be = 3
a * b * c must be = 1
1 1 1
OK

Level 2 of 3
Enter 3 numbers:
a + b + c must be = 7
a * b * c must be = 12
2 2 3
OK

Level 3 of 3
Enter 3 numbers:
a + b + c must be = 11
a * b * c must be = 45
3 5 3
OK

END
 
*/
 
 
/*
run 2:
 
Level 1 of 3
Enter 3 numbers:
a + b + c must be = 3
a * b * c must be = 1
1 1 1
OK

Level 2 of 3
Enter 3 numbers:
a + b + c must be = 9
a * b * c must be = 27
1 2 3
Wrong

Level 2 of 3
Enter 3 numbers:
a + b + c must be = 8
a * b * c must be = 18
3 3 2
OK

Level 3 of 3
Enter 3 numbers:
a + b + c must be = 11
a * b * c must be = 45
4 6 8
Wrong

Level 3 of 3
Enter 3 numbers:
a + b + c must be = 10
a * b * c must be = 36
9 0 1
Wrong

Level 3 of 3
Enter 3 numbers:
a + b + c must be = 12
a * b * c must be = 60
3 4 5
OK

END
 
*/
 

 



answered Feb 9, 2021 by avibootz
...