What's new

Help C language program

Thonskie17

Honorary Poster
Established
Joined
Nov 13, 2014
Posts
582
Reaction
118
Points
188
Pa help guys, pahingi po ng simple program sa C language gamit ang nasa baba:

Create a code that can emulate your choice of object or situation. Your code
MUST include the following concepts:
o Variables
o Comments
o Arithmetic Operators
o Conditional statements (choose between if-else and switch-case)
o printf()
o scanf()
o loops (choose between while, do-while, and for loop)
o arrays
o strings
o functions and parameters

PS: gusto daw kasi ni sir yung medyo iba sa nga basic program etc. calculator.

Salamat po
 
Sure! Here's a simple program in C that emulates a guessing game. The user needs to guess a randomly generated number within a specified range. The program uses various concepts like variables, comments, arithmetic operators, conditional statements (if-else), printf(), scanf(), loops (while), arrays, strings, functions, and parameters:

C:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function to generate a random number between min and max (inclusive)
int generateRandomNumber(int min, int max) {
    return min + rand() % (max - min + 1);
}

int main() {
    int guess;
    int randomNumber;
    int attempts = 0;
    int minRange = 1;
    int maxRange = 100;

    // Seed the random number generator
    srand(time(0));

    // Generate a random number
    randomNumber = generateRandomNumber(minRange, maxRange);

    printf("Welcome to the Guessing Game!\n");
    printf("I have chosen a number between %d and %d.\n", minRange, maxRange);

    while (1) {
        printf("\nEnter your guess: ");
        scanf("%d", &guess);

        attempts++;

        if (guess == randomNumber) {
            printf("Congratulations! You guessed the correct number in %d attempts.\n", attempts);
            break;
        } else if (guess < randomNumber) {
            printf("Too low! Try again.\n");
        } else {
            printf("Too high! Try again.\n");
        }
    }

    return 0;
}

In this program, the user needs to guess a number within a specified range (in this case, between 1 and 100). The program generates a random number within that range using the generateRandomNumber() function. The user's guess is compared with the randomly generated number, and appropriate messages are displayed to guide the user towards the correct answer. The number of attempts taken by the user is also counted and displayed when they guess the correct number.
 
Back
Top