while includestdio h int mainvoid int i sum

  • Slides: 14
Download presentation

while 문이 사용된 프로그램 예 #include<stdio. h> int main(void) { int i, sum; i

while 문이 사용된 프로그램 예 #include<stdio. h> int main(void) { int i, sum; i = 100; sum = 0; while(i >= 0){ sum += i; i = i – 1; } printf(“Sum of 1 – 100 = %dn”, sum); return 0; } 3

while 문 n 형식 n n while(expression) statement expression의 값이 참인 동안 statement를 수행한다.

while 문 n 형식 n n while(expression) statement expression의 값이 참인 동안 statement를 수행한다. 4

for 문이 사용된 프로그램 예 #include<stdio. h> int main(void) { int i, sum; sum

for 문이 사용된 프로그램 예 #include<stdio. h> int main(void) { int i, sum; sum = 0; for(i = 0; i <= 100; i++) sum += i; printf(“Sum of 1 – 100 = %dn”, sum); return 0; } 5

printf() 함수 n n stdout으로 무엇인가를 출력하는 함수 함수의 인자(parameter) n n () 안에는

printf() 함수 n n stdout으로 무엇인가를 출력하는 함수 함수의 인자(parameter) n n () 안에는 함수의 인자(parameter)가 들어 간다. printf()의 인자 n control_string, other_arguments 7

printf()의 예 #include<stdio. h> int main(void) { int score 1, score 2, score 3,

printf()의 예 #include<stdio. h> int main(void) { int score 1, score 2, score 3, avg_score; int num_score; score 1 = 87; score 2 = 93; score 3 = 100; num_score = 3; - %d: conversion specification - avg_score: other_arguments avg_score = (score 1 + score 2 + score 3) / num_score; printf(“Average score: %dn”, avg_score); return 0; } 8

Conversion Specifications and other_arguments n conversion_specification n %로 시작한다. control_string에 나타난 conversion_specification은 other_arguments의 수식들에

Conversion Specifications and other_arguments n conversion_specification n %로 시작한다. control_string에 나타난 conversion_specification은 other_arguments의 수식들에 차례로 대응 된다. conversion_specification은 해당 수식을 어떻게 출력할 것인가를 정하는 역할을 한 다. 9

scanf() 함수 n Conversion specifications n n %d: 정수 %c: 문자 %s: 문자열 %f:

scanf() 함수 n Conversion specifications n n %d: 정수 %c: 문자 %s: 문자열 %f: 실수 12

성적 처리 프로그램(1/2) #include<stdio. h> int main(void) { float prog_score, eng_score, math_score, avg_score; int

성적 처리 프로그램(1/2) #include<stdio. h> int main(void) { float prog_score, eng_score, math_score, avg_score; int i; printf(“n. Input score of the programming: ”); scanf(“%f”, &prog_score); printf(“n. Input score of the english: ”); scanf(“%f”, &eng_score); printf(“n. Input score of the mathematics: ”); scanf(“%f”, &math_score); avg_score = (prog_score + eng_score + math_score) / 3; printf(“nn%15 s%15 sn”, “Programming”, “English”, “Mathematics”, “Average”); 13

성적 처리 프로그램(2/2) for(i = 0; i < 60; i++) printf(“=“); printf(“n%15. 1 f%15.

성적 처리 프로그램(2/2) for(i = 0; i < 60; i++) printf(“=“); printf(“n%15. 1 f%15. 1 fn”, prog_score, eng_score, math_score, avg_score); return 0; } Input score of the programming: 76. 0. . . Programming English Mathematics Average ====================== 76. 0 85. 3 68. 9 76. 7 14