Algorithm Analysis
The task is to implement a conditional statement.
Algorithm Implementation
Read the student's score. Output the answer, implementing a conditional statement.
scanf("%d",&n); if (n <= 3) printf("Initial\n"); else if (n <= 6) printf("Average\n"); else if (n <= 9) printf("Sufficient\n"); else printf("High\n");
Algorithm Implementation – switch
#include <stdio.h> int n; int main(void) { scanf("%d", &n); switch (n) { case 1: case 2: case 3: printf("Initial\n"); break; case 4: case 5: case 6: printf("Average\n"); break; case 7: case 8: case 9: printf("Sufficient\n"); break; default: printf("High\n"); } return 0; }
Algorithm Implementation – switch optimized
#include <stdio.h> int n; int main(void) { scanf("%d", &n); n = (n - 1) / 3; switch (n) { case 0: printf("Initial\n"); break; case 1: printf("Average\n"); break; case 2: printf("Sufficient\n"); break; default: printf("High\n"); } return 0; }
Java Implementation
import java.util.*; public class Main { public static void main(String[] args) { Scanner con = new Scanner(System.in); int n = con.nextInt(); if (n <= 3) System.out.println("Initial"); else if (n <= 6) System.out.println("Average"); else if (n <= 9) System.out.println("Sufficient"); else System.out.println("High"); con.close(); } }
Java Implementation – switch
import java.util.*; public class Main { public static void main(String[] args) { Scanner con = new Scanner(System.in); int n = con.nextInt(); switch (n) { case 1: case 2: case 3: System.out.println("Initial"); break; case 4: case 5: case 6: System.out.println("Average"); break; case 7: case 8: case 9: System.out.println("Sufficient"); break; default: System.out.println("High"); } con.close(); } }