{ }codelper
else-if chainfor-each loopP0011

Letter grades

The first line gives the number of students n, and the second line gives n scores.
For each score print its grade on its own line: A for 90 or more, B for 80 or more, C for 70 or more, D for 60 or more, otherwise F.

Input
1 ≤ n ≤ 100, 0 ≤ score ≤ 100
Output
n lines
Example 1 · stdin
4
95 80 61 59
stdout
A
B
D
F
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

Mixing up >= and > at the boundaries (90, 80, 70, 60) is the most common mistake.
Checking from the top down means earlier conditions already filter, so you never need 80 <= x < 90.
Iterating a list: Python for x in, Java for (int x : a), C# foreach, Craykon foreach (x in a).

Pythonmain.py · 12 lines
n = int(input())for x in map(int, input().split()):    if x >= 90:        print("A")    elif x >= 80:        print("B")    elif x >= 70:        print("C")    elif x >= 60:        print("D")    else:        print("F")
JavaScriptmain.js · 3 lines
const [n, ...scores] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const grade = (x) => (x >= 90 ? "A" : x >= 80 ? "B" : x >= 70 ? "C" : x >= 60 ? "D" : "F");console.log(scores.map(grade).join("\n"));
TypeScriptmain.ts · 4 lines
import { readFileSync } from "node:fs";const [n, ...scores]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const grade = (x: number): string => (x >= 90 ? "A" : x >= 80 ? "B" : x >= 70 ? "C" : x >= 60 ? "D" : "F");console.log(scores.map(grade).join("\n"));
JavaMain.java · 18 lines
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();        StringBuilder out = new StringBuilder();        for (int i = 0; i < n; i++) {            int x = sc.nextInt();            if (x >= 90) out.append("A\n");            else if (x >= 80) out.append("B\n");            else if (x >= 70) out.append("C\n");            else if (x >= 60) out.append("D\n");            else out.append("F\n");        }        System.out.print(out);    }}
C#Main.cs · 14 lines
using System; class Program{    static void Main()    {        Console.ReadLine();        foreach (string s in Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries))        {            int x = int.Parse(s);            Console.WriteLine(x >= 90 ? "A" : x >= 80 ? "B" : x >= 70 ? "C" : x >= 60 ? "D" : "F");        }    }}
C++main.cpp · 14 lines
#include <iostream> int main() {    int n, x;    std::cin >> n;    for (int i = 0; i < n; i++) {        std::cin >> x;        if (x >= 90) std::cout << "A\n";        else if (x >= 80) std::cout << "B\n";        else if (x >= 70) std::cout << "C\n";        else if (x >= 60) std::cout << "D\n";        else std::cout << "F\n";    }}
Cmain.c · 15 lines
#include <stdio.h> int main(void) {    int n, x;    scanf("%d", &n);    for (int i = 0; i < n; i++) {        scanf("%d", &x);        if (x >= 90) puts("A");        else if (x >= 80) puts("B");        else if (x >= 70) puts("C");        else if (x >= 60) puts("D");        else puts("F");    }    return 0;}
Craykonmain.cray · 25 lines
n = Console.ReadLine().ToNumber();scores = Console.ReadNumbers();foreach (x in scores){    if (x >= 90)    {        Console.WriteLine("A");    }    else if (x >= 80)    {        Console.WriteLine("B");    }    else if (x >= 70)    {        Console.WriteLine("C");    }    else if (x >= 60)    {        Console.WriteLine("D");    }    else    {        Console.WriteLine("F");    }}