Quiz Application

Quiz Application image

Introduction

A quiz program is a simple console-based program that employs multiple-choice or other types of questions to test a user's knowledge of a range of subjects. Each question has several options, and the user selects an answer from them. The program maintains the user's score based on the correct answers. The overall score is displayed at the end of the quiz.


Source Code:

#include <stdio.h>

typedef struct {

    char question [255];
    char options [4][100];
    int correctAnswer;
} QuizQuestion;

void displayQuestion(QuizQuestion q, int questionNumber) {
    printf("\nQuestion %d: %s\n", questionNumber, q.question);
    for (int i = 0; i < 4; i++) {
       printf("%d. %s\n", i + 1, q.options[i]);
    }
}

int askQuestion(QuizQuestion q) {
    int answer;
    printf("Enter your answer (1-4): ");
    scanf("%d", &answer);

    if (answer == q.correctAnswer) {
      printf("Correct!\n");
      return 1;

    } else {
        printf("Wrong! The correct answer is %d.\n", q.correctAnswer);
        return 0;
    }
}

int main() {
    QuizQuestion quiz[] = {
      {"What is the capital of France?", {"Berlin", "Madrid", "Paris", "Rome"},3},
      {"Which programming language is known as the 'mother of all languages'?", 
      {"C", "C++", "Java", "Python"},1},
      {"What is 2 + 2?", {"3", "4", "5", "6"},2},
      {"Who wrote the play 'Macbeth'?", {"William Shakespeare", "John Milton", 
      "Paul Adam", "Paul Dehn"},1}
    };
    
    int totalQuestions = sizeof(quiz) / sizeof(quiz[0]);
    int score = 0;

    printf("Welcome to the Quiz Application!\n");

    for (int i = 0; i < totalQuestions; i++){
        displayQuestion(quiz[i], i + 1);
        score += askQuestion (quiz[i]);
    }

    printf("\n You scored %d out of %d.\n", score, totalQuestions);

    return 0;
}

Output

Output Image