0% found this document useful (0 votes)
7 views

C Program for Conducting Multiple Choice Questions Test

Uploaded by

hibiscushop4u
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

C Program for Conducting Multiple Choice Questions Test

Uploaded by

hibiscushop4u
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C program for conducting multiple choice

questions test.

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

#define MAX_QUESTIONS 10
#define MAX_LENGTH 100

void inputQuestions(char questions[MAX_QUESTIONS][MAX_LENGTH],


char options[MAX_QUESTIONS][4][MAX_LENGTH],
char correctAnswers[MAX_QUESTIONS],
int *numQuestions) {
printf("How many questions do you want to add (max %d)? ", MAX_QUESTIONS);
scanf("%d", numQuestions);

for (int i = 0; i < *numQuestions; i++) {


printf("\nEnter question %d: ", i + 1);
getchar();
fgets(questions[i], MAX_LENGTH, stdin);
questions[i][strcspn(questions[i], "\n")] = 0;

printf("Enter option A: ");


fgets(options[i][0], MAX_LENGTH, stdin);
options[i][0][strcspn(options[i][0], "\n")] = 0;

printf("Enter option B: ");


fgets(options[i][1], MAX_LENGTH, stdin);
options[i][1][strcspn(options[i][1], "\n")] = 0;
printf("Enter option C: ");
fgets(options[i][2], MAX_LENGTH, stdin);
options[i][2][strcspn(options[i][2], "\n")] = 0;

printf("Enter option D: ");


fgets(options[i][3], MAX_LENGTH, stdin);
options[i][3][strcspn(options[i][3], "\n")] = 0;

printf("Enter the correct answer (A, B, C, or D): ");


scanf(" %c", &correctAnswers[i]);
}
}

void conductQuiz(char questions[MAX_QUESTIONS][MAX_LENGTH],


char options[MAX_QUESTIONS][4][MAX_LENGTH],
char correctAnswers[MAX_QUESTIONS],
int numQuestions) {
char userAnswer;
int score = 0;

printf("\nStarting the Quiz!\n\n");


for (int i = 0; i < numQuestions; i++) {
printf("Question %d: %s\n", i + 1, questions[i]);
printf("A. %s\n", options[i][0]);
printf("B. %s\n", options[i][1]);
printf("C. %s\n", options[i][2]);
printf("D. %s\n", options[i][3]);
printf("Your answer: ");
scanf(" %c", &userAnswer);

if (userAnswer == correctAnswers[i] || userAnswer == correctAnswers[i] + 32) {


printf("Correct!\n");
score++;
} else {
printf("Wrong! The correct answer was %c.\n", correctAnswers[i]);
}
printf("\n");
}

printf("Quiz Over! You scored %d out of %d.\n", score, numQuestions);


}

int main() {
char questions[MAX_QUESTIONS][MAX_LENGTH];
char options[MAX_QUESTIONS][4][MAX_LENGTH];
char correctAnswers[MAX_QUESTIONS];
int numQuestions = 0;

inputQuestions(questions, options, correctAnswers, &numQuestions);

conductQuiz(questions, options, correctAnswers, numQuestions);

return 0;
}

You might also like