Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,914 questions

51,847 answers

573 users

How to get a grade and transform it into a letter grade in C

2 Answers

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

const char* toLetterGrade(double score) {
    // Define scores and grades
    double scores[] = {95.0, 90.0, 85.0, 80.0, 75.0, 70.0, 65.0, 60.0};
    const char* grades[] = {"A+", "A", "B+", "B", "C+", "C", "D+", "D"};

    // Find the length of the scores array
    int scores_size = sizeof(scores) / sizeof(scores[0]);

    // Iterate through scores and find the grade
    for (int i = 0; i < scores_size; i++) {
        if (score >= scores[i]) {
            return grades[i];
        }
    }

    return "F"; // Default grade if none of the scores match
}

int main() {
    // Test the program with individual scores
    printf("%s\n", toLetterGrade(95)); // A+
    printf("%s\n", toLetterGrade(90)); // A
    printf("%s\n", toLetterGrade(80)); // B
    printf("%s\n", toLetterGrade(60)); // D
    printf("%s\n", toLetterGrade(50)); // F

    return 0;
}



/*
run:

A+
A
B
D
F

*/

 



answered Mar 23, 2025 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

const double scores[] = {95.0, 90.0, 85.0, 80.0, 75.0, 70.0, 65.0, 60.0};
const char* grades[] = {"A+", "A", "B+", "B", "C+", "C", "D+", "D"};
const int scores_size = sizeof(scores) / sizeof(scores[0]);

const char* toLetterGrade(double score) {
    for (int i = 0; i < scores_size; i++) {
        if (score >= scores[i]) {
            return grades[i];
        }
    }
    
    return "F"; // Default grade if none of the scores match
}

int main() {
    printf("%s\n", toLetterGrade(95)); // A+
    printf("%s\n", toLetterGrade(90)); // A
    printf("%s\n", toLetterGrade(80)); // B
    printf("%s\n", toLetterGrade(60)); // D
    printf("%s\n", toLetterGrade(50)); // F
    
    return 0;
}




/*
run:

A+
A
B
D
F

*/

 



answered Mar 23, 2025 by avibootz

Related questions

...