printf와 scanf 함수란?

 

예제1) 사용자로부터 두 개의 정수를 입력받아서, 그 합을 출력하는 프로그램을 작성하시오.

#include <stdio.h>


int main (void)
{


int x;
int y;


printf(“input two number:”);
scanf(“%d, %d, &x, &y”);


printf(“sum : %d\n” , x + y);


return 0;
}

 

예제2) 사용자에게 이름과 나이를 입력받아서 다음과 같이 출력하세요.

 

예시 출력: "을지문덕님의 나이는 15살입니다.“

 

#include <stdio.h>


int main (void)
{


char name[20]; // 이름 20자 까지 입력가능
int age;


printf(“이름을 입력하세요:”);
scanf(“%s”, name);


printf(“나이를 입력하세요:”);
scanf(“%d”, &age);


printf(“%s님의 나이는 %d살입니다.\d”. name, age);


return 0;
}

 

예제 3: 직사각형의 넓이 구하기 - 가로와 세로 길이를 입력받아 넓이를 출력하세요.

#include <stdio.h>


int main(void)
{
  int 가로;
  int 세로;


  printf(“가로와 세로의 길이를 입력하세요:”);
  scanf(“%d,%d”, &가로, &세로);


  printf(“넓이 : %d”, 가로 * 세로);


return 0;
}
 

 

 

예제 4) 실수(float)2개 입력받아서 평균을 계산한 후, 소수점 2자리까지 출력하세요.

#include <stdio.h>


int main(vioid)
{


float x, y;
printf(“실수를 입력하세요:”);
scanf(“%f, %f, &x, &y”); // f%는 실수 %d는 정수


float avg (x + y) / 2;


printf(“평균 : ”%.2f\n”, avg); // .2는 소수 2자리까지 출력하라는 뜻. %f는 실수값 출력
return 0;
}

+ Recent posts