Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Exercise 15

C Exercise 15

100 Classic C Language Examples 100 Classic C Language Examples

**Problem:** Use nested conditional operators to solve this problem: students scoring >=90 are marked A, those between 60 and 89 are marked B, and those below 60 are marked C.

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

Example

// Created by www.runoob.com on 15/11/9.

// Copyright (c) 2015 Runoob Tutorial. 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 Language Examples 100 Classic C Language Examples