소개
본 랩은 학생들이 비트 연산자를 사용하여 숫자가 짝수인지 홀수인지 판별하는 프로그램을 이해하도록 돕는 것을 목표로 합니다. 또한, 나머지 연산자 (modulus operator) 를 사용하지 않고 숫자가 짝수인지 홀수인지 확인하는 방법도 제공합니다.
본 랩은 학생들이 비트 연산자를 사용하여 숫자가 짝수인지 홀수인지 판별하는 프로그램을 이해하도록 돕는 것을 목표로 합니다. 또한, 나머지 연산자 (modulus operator) 를 사용하지 않고 숫자가 짝수인지 홀수인지 확인하는 방법도 제공합니다.
~/project/ 디렉토리의 main.c 파일에 복사하도록 요청합니다.#include<stdio.h>
int main()
{
printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
int x;
for(x = 0; x <= 10; x++)
{
if(x&1) // if number is odd
printf("\t\t\t%d is odd\n",x);
else if(!(x&1)) // ! is used inside if to reverse the boolean value
printf("\t\t\t%d is even\n",x);
}
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
~/project/ 디렉토리의 main.c 파일에 복사하도록 요청합니다.#include<stdio.h>
int main()
{
printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
int n;
printf("Enter a number: ");
scanf("%d",&n);
if((n/2)*2 == n)
printf("\n\n\t\t %d is Even\n", n);
else
printf("\n\n\t\t %d is Odd\n", n);
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
학생들은 이제 숫자가 짝수인지 홀수인지 확인하는 두 가지 방법에 익숙해졌습니다. 원하는 기능을 달성하기 위해 이러한 기술을 모든 C 프로그램에서 구현할 수 있습니다.