Number guess game using c programming language

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h>


void loading(void);
int main() {
// srand statement generate new random every time.
    srand(time(NULL));
    printf("Get ready for the Number game.\n");
    int RandomNumber = rand() % 10;
    int guess;
    // calling the loading effect function.
    loading();
    // game begins.
    printf("\nlet's start.\n");
    while(1) {

        printf("Guess the number:");
        scanf("%d",&guess);
// checks is user guessed correctly. 
        if(RandomNumber == guess) {
            printf("You guessed right.\n");
            break;
        }
        else {
            continue;
        }


    }
}

void loading(void){

    const int total_length = 100;
    const int barWidth = 20;
        
        
        
    for(int count = 1; count<=total_length; count++){
    
    printf("[");
        float progress = (float)count / total_length;
        float progress_Bar = progress * barWidth;
        
        for(int i = 1; i<=barWidth; i++){
            if(i<progress_Bar){
                printf("#");
            }
            else{
                printf(" ");
            }
        }
        usleep(10000);
        printf("]%.2f%%\r",progress*100);
        fflush(stdout);
    }
    

}

Comments

Popular posts from this blog

AI and Human Evolution

Snake Game using JAVA

To Do List App (GUI) using C#