C Exercise Example 15

100 Classic C Examples

Title:Use nested conditional operators to solve this problem: students with a score of >=90 are represented by A, those between 60-89 are represented by B, and those below 60 are represented by C.

Program analysis:(a>b)?a:b is a basic example of the conditional operator.

Example

// Created by www.example.com on 15/11/9. // Copyright © 2015 Example. All rights reserved. // #include<stdio.h> int main() { int score; char grade; printf("Please enter a score:"); scanf("%d",&score); grade=(score>=90)?'A':((score>=60)?'B':'C'); printf("%c\n",grade); return 0; }

The output of the above example is:

请输入分数: 87
B

100 Classic C Examples

other extensions