컴퓨터/프로그래밍

05-07

review777777 2011. 6. 16. 01:53
반응형


사용자로부터 0000과 1111사이의 2진수를 입력받아 10진수로 출력하는 프로그램을 작성



#include <stdio.h>

int main(void)
{
  int first, second, third, fourth, decimal;
  int dec1, dec2;                             // for temporary decisions
  printf("--------Between 0000 and 1111, Convert to Demical Number-------\n");
  printf("The First : "), scanf("%d",&first);
  printf("The Second : "), scanf("%d",&second);
  printf("The Third : "), scanf("%d",&third);
  printf("The Fourth : "), scanf("%d",&fourth);

  decimal = (first*1)+(second*2)+(third*4)+(fourth*8);

  dec1 = (first > 1 || second > 1 || third > 1 || fourth > 1);   // not greater than 1
  dec2 = (first < 0 || second < 0 || third < 0 || fourth < 0);   // not less than 0

  dec1 || dec2 ?
    printf("\n Wrong Input \n") :      // for exceptions
    printf("\nThe input is %d%d%d%d , The decimal number is %d.\n", fourth, third, second, first, decimal);

}

반응형

'컴퓨터 > 프로그래밍' 카테고리의 다른 글

웹에서 텍스트 가져오기  (0) 2011.08.05
1부터 100까지 숫자중 임의의 숫자 추측하기  (0) 2011.08.05
python 처음 시작하기  (0) 2011.08.05
05-04  (0) 2011.06.16
05-02  (0) 2011.05.11
04-03  (0) 2011.05.11
02-04  (0) 2011.05.11