Create an Online Quiz System in C Language with Free Codes

Index

Online Quiz System

Here you get Free Codes of Online Quiz System multiple choice questions. This Project Prompt work like – it shows the question to the user, accepts their answers, and provides feedback on their performance.

Quiz Scoring

The scoring in the quiz system is straight forward:

  • Each Correct Answer earns the user one point.
  • The total score is calculated as the number of correct answers out of the total number of questions.
Online Quiz System

Project Codes

Here is the complete Project Codes

#include <stdio.h>
#include <string.h>

#define MAX_QUESTIONS 3

typedef struct {
    char question[200];
    char options[4][50];
    int correctOption;
} Question;

void initializeQuestions(Question questions[]) {
    strcpy(questions[0].question, "What is the capital of France?");
    strcpy(questions[0].options[0], "A. London");
    strcpy(questions[0].options[1], "B. Paris");
    strcpy(questions[0].options[2], "C. Berlin");
    strcpy(questions[0].options[3], "D. Rome");
    questions[0].correctOption = 1;

    strcpy(questions[1].question, "Which planet is known as the Red Planet?");
    strcpy(questions[1].options[0], "A. Mars");
    strcpy(questions[1].options[1], "B. Venus");
    strcpy(questions[1].options[2], "C. Jupiter");
    strcpy(questions[1].options[3], "D. Saturn");
    questions[1].correctOption = 0;

    strcpy(questions[2].question, "Who wrote 'Romeo and Juliet'?");
    strcpy(questions[2].options[0], "A. William Shakespeare");
    strcpy(questions[2].options[1], "B. Charles Dickens");
    strcpy(questions[2].options[2], "C. Jane Austen");
    strcpy(questions[2].options[3], "D. Mark Twain");
    questions[2].correctOption = 0;
}

void displayQuestion(Question question) {
    printf("\n%s\n", question.question);
    for (int i = 0; i < 4; i++) {
        printf("%s\n", question.options[i]);
    }
}

int validateChoice(char choice) {
    return choice >= 'A' && choice <= 'D';
}

int takeQuiz(Question questions[]) {
    int score = 0;
    char choice;

    for (int i = 0; i < MAX_QUESTIONS; i++) {
        displayQuestion(questions[i]);
        printf("Enter your choice (A/B/C/D): ");
        scanf(" %c", &choice);

        if (!validateChoice(choice)) {
            printf("Invalid choice. Please enter A, B, C, or D.\n");
            i--; // Decrement i to repeat the same question
            continue;
        }

        if (choice - 'A' == questions[i].correctOption) {
            printf("Correct!\n");
            score++;
        } else {
            printf("Incorrect. The correct answer is %c.\n", 'A' + questions[i].correctOption);
        }
    }

    return score;
}

int main() {
    Question questions[MAX_QUESTIONS];
    int totalQuestions = 0;
    int score;

    initializeQuestions(questions);

    printf("Welcome to the Online Quiz!\n");
    printf("There are %d questions in this quiz.\n", MAX_QUESTIONS);
    printf("You will be given 4 options for each question.\n");
    printf("Please enter the corresponding letter for your choice.\n");

    score = takeQuiz(questions);
    printf("\nQuiz completed! Your score: %d/%d\n", score, MAX_QUESTIONS);

    return 0;
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top