Switch Case 를 활용한 C 메뉴 기반 프로그램

CBeginner
지금 연습하기

소개

이 랩에서는 C 언어의 switch case 문을 사용하여 메뉴 기반 프로그램을 만들 것입니다. 메뉴 기반 프로그램은 사용자에게 메뉴를 제시하고, 사용자가 메뉴에서 옵션을 선택하여 특정 작업을 수행하는 프로그램입니다. 이 프로그램에서는 숫자의 팩토리얼 계산, 소수 또는 합성수 확인, 짝수 또는 홀수 확인과 같은 다양한 작업을 수행할 수 있는 메뉴를 만들 것입니다.

새 C 파일 생성

첫 번째 단계는 ~/project/ 디렉토리에 새로운 C 파일을 생성하는 것입니다. 이 파일의 이름을 main.c로 지정하십시오.

필요한 라이브러리 포함

이 단계에서는 프로그램에 표준 입출력 라이브러리를 포함할 것입니다. 이 라이브러리는 C 언어에서 입출력 연산을 위한 함수를 제공합니다.

#include <stdio.h>

main() 함수 정의

main() 함수는 C 프로그램의 진입점 함수입니다. 이 단계에서는 main() 함수를 정의할 것입니다.

int main()
{
    // Code goes here
    return 0;
}

메뉴 표시

이 단계에서는 사용자에게 메뉴를 표시합니다. 메뉴를 통해 사용자는 다양한 옵션 중에서 선택할 수 있습니다. 이 프로그램에서는 다음과 같은 옵션을 사용합니다.

  • 옵션 1: 숫자의 팩토리얼 계산
  • 옵션 2: 숫자가 소수인지 확인
  • 옵션 3: 숫자가 짝수인지 홀수인지 확인
  • 옵션 4: 프로그램 종료
int main()
{
    printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Factorial \n");
        printf("2. Prime\n");
        printf("3. Odd\\Even\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);
    }
    return 0;
}

Switch Case 구현

이 단계에서는 switch case 문을 구현합니다. 사용자의 선택에 따라 다른 case 를 실행합니다.

int main()
{
    printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Factorial \n");
        printf("2. Prime\n");
        printf("3. Odd\\Even\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                /*
                * Code to calculate factorial of a number
                */
                break;

            case 2:
                /*
                * Code to check if a number is prime or composite
                */
                break;

            case 3:
                /*
                * Code to check if a number is even or odd
                */
                break;

            case 4:
                /*
                * Code to exit the program
                */
                break;
        }
    }
    return 0;
}

숫자 팩토리얼 계산

이 단계에서는 사용자가 옵션 1 을 선택했을 때 사용자가 요청한 숫자의 팩토리얼을 계산합니다.

int main()
{
    printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Factorial \n");
        printf("2. Prime\n");
        printf("3. Odd\\Even\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                printf("Enter number:\n");
                scanf("%d", &num);
                fact = 1;
                for(i = 1; i <= num; i++)
                {
                    fact = fact*i;
                }
                printf("\n\nFactorial value of %d is = %lu\n\n\n",num,fact);
                break;

            case 2:
                /*
                * Code to check if a number is prime or composite
                */
                break;

            case 3:
                /*
                * Code to check if a number is even or odd
                */
                break;

            case 4:
                printf("\n\n\t\t\tCoding is Fun !\n\n\n");
                exit(0);
        }
    }
    return 0;
}

소수 판별

이 단계에서는 숫자가 소수 (prime) 인지 합성수 (composite) 인지 확인합니다. 프로그램은 사용자가 입력한 숫자가 소수인지 아닌지를 나타내는 메시지를 출력합니다.

int main()
{
    printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Factorial \n");
        printf("2. Prime\n");
        printf("3. Odd\\Even\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                /*
                * Code to calculate factorial of a number
                */
                break;

            case 2:
                printf("Enter number:\n");
                scanf("%d", &num);
                if(num == 1)
                    printf("\n1 is neither prime nor composite\n\n");
                for(i = 2; i < num; i++)
                {
                    if(num%i == 0)
                    {
                        printf("\n%d is not a prime number\n\n", num);
                        break;
                    }

                }
                /*
                    Not divisible by any number other
                    than 1 and itself
                */
                if(i == num)
                {
                    printf("\n\n%d is a Prime number\n\n", num);
                    break;
                }
                break;

            case 3:
                /*
                * Code to check if a number is even or odd
                */
                break;

            case 4:
                printf("\n\n\t\t\tCoding is Fun !\n\n\n");
                exit(0);
        }
    }
    return 0;
}

숫자 짝수/홀수 판별

이 단계에서는 숫자가 짝수 (even) 인지 홀수 (odd) 인지 확인합니다. 프로그램은 사용자가 입력한 숫자가 짝수인지 홀수인지 나타내는 메시지를 출력합니다.

int main()
{
    printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Factorial \n");
        printf("2. Prime\n");
        printf("3. Odd\\Even\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                /*
                * Code to calculate factorial of a number
                */
                break;

            case 2:
                /*
                * Code to check if a number is prime or composite
                */
                break;

            case 3:
                printf("Enter number:\n");
                scanf("%d", &num);

                if(num%2 == 0) // 0 is considered to be an even number
                    printf("\n\n%d is an Even number\n\n",num);
                else
                    printf("\n\n%d is an Odd number\n\n",num);
                break;

            case 4:
                printf("\n\n\t\t\tCoding is Fun !\n\n\n");
                exit(0);
        }
    }
    return 0;
}

프로그램 종료

이 단계에서는 사용자가 옵션 4 를 선택했을 때 프로그램을 종료하는 케이스를 추가합니다.

int main()
{
    printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Factorial \n");
        printf("2. Prime\n");
        printf("3. Odd\\Even\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                /*
                * Code to calculate factorial of a number
                */
                break;

            case 2:
                /*
                * Code to check if a number is prime or composite
                */
                break;

            case 3:
                /*
                * Code to check if a number is even or odd
                */
                break;

            case 4:
                printf("\n\n\t\t\tCoding is Fun !\n\n\n");
                exit(0);
        }
    }
    return 0;
}

요약

이 랩에서는 switch case 를 사용하여 C 로 메뉴 기반 프로그램을 만들었습니다. 이 프로그램은 사용자에게 메뉴를 표시하며, 사용자는 숫자의 팩토리얼 계산, 숫자가 소수 (prime) 인지 합성수 (composite) 인지 확인, 숫자가 짝수 (even) 인지 홀수 (odd) 인지 확인하는 등 다양한 작업을 수행하기 위해 여러 옵션 중에서 선택할 수 있습니다. 이 랩을 통해 C 에서 switch case 를 사용하여 메뉴 기반 프로그램을 만드는 방법에 대한 이해를 높이셨기를 바랍니다.