본문 바로가기
개발자

free code camp / C programming 유튜브 강의 정리 1

by ⁖⁝‥ 2022. 4. 21.

1. 함수는 void로 시작 (void는 어떤 것도 return 하지 않음)

단순히 불러오려면 void 뒤에 sayHi(); 를 하면됨

근데 이름을 여러개 하려면 

char name[] , int age를 괄호 안에 넣고

char name은 %s, int age는 %d 로 받으면 됨.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("TOP\n");
    sayHi("Mike",40);
     sayHi("Tom",50);
      sayHi("Oscar",13);
printf("BOTTOM");

    return 0;
}

void sayHi(char name[], int age){
    printf("Hello %s\nYou are %d\n", name, age);

}

2. cubes number

#include <stdio.h>
#include <stdlib.h>

double cube(double num){
 return num * num * num;
}

int main()
{
    printf("Answer : %f", cube(7.0));

   return 0;
   
}

3. 숫자 두개중에 큰것 고르기

#include <stdio.h>
#include <stdlib.h>



int max(int num1, int num2){
    int result;
    if(num1 > num2){

        result = num1;

    } else {
    result = num2;
    }
    return result;
}


int main()
{

printf("%d", max(40, 40));
   return 0;

}

4. 숫자 세개중에 제일 큰것 고르기

 

#include <stdio.h>
#include <stdlib.h>



int max(int num1, int num2, int num3){
    int result;

    if(num1 >= num2 && num1 >= num3) {

        result = num1;
    }
    else if(num2 >= num1 && num2 >= num3){
        result=num2;
    }

    else{
        result = num3;
    }
    return result;
}


int main()
{

printf("%d", max(40, 42, 10));
   return 0;

}

5. 계산기 만들기

#include <stdio.h>
#include <stdlib.h>





int main()
{

    double num1;
    double num2;
    char op;

    printf("Enter a number: ");
    scanf("%lf",&num1);
    printf("Enter operator: ");
    scanf(" %c", &op);
    printf("Enter a number: ");
    scanf("%lf",&num2);


    if (op == '+'){

        printf("%f", num1 + num2);
    } else if (op == '-'){
    printf("%f", num1- num2);
    } else if (op == '/'){
    printf("%f", num1/ num2);
    }else if (op == '*'){
        printf("%f", num1* num2);
    }
    else {
        printf("invalid operator");

    }

   return 0;

}

6. 성적 입력하기

#include <stdio.h>
#include <stdlib.h>





int main()
{

    char grade = 'C';


    switch(grade){
 case 'A' :
    printf("You did great!");
    break;

 case 'B' :
    printf("You did alright!");
    break;

 case 'C' :
    printf("You did poorly");
    break;
 case 'D' :
    printf("You did very bad");
    break;
 case 'F' :
    printf("You failed !");
    break;


 default :
    printf("Invalid Grade");
    }

   return 0;

}

7. 여러가지 변수 입력하기

 

#include <stdio.h>
#include <stdlib.h>


struct Student{
   char name [50];
   char major [50];
   int age;
   double gpa;

};


int main()
{

   struct Student student1;

   student1.age = 22;
   student1. gpa = 3.2;
   strcpy( student1.name, "Jim");
   strcpy( student1.major, "Business");

   struct Student student2;
   student2.age = 30;
   student2. gpa = 2.52;
   strcpy( student2.name, "Tom");
   strcpy( student2.major, "Art");


   printf("%s", student2.name);

   return 0;

}

8.

 

while loop

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int index = 1;
   while(index <= 5){
        printf("%d\n", index);
        index++;
   }
   return 0;

do while loop

#include <stdio.h>
#include <stdlib.h>



int main()
{


   int index = 6;

    do {
        printf("%d\n", index);
        index++;
     } while(index <= 8);




   return 0;

}

9. 숫자 알아맞히기 게임

#include <stdio.h>
#include <stdlib.h>



int main()
{


  int secretNumber = 5;
  int guess;

  while (guess!= secretNumber){
      printf("Enter a number : ");
      scanf("%d", &guess);

  }
   printf("You win!");


   return 0;

}

10. 3번 기회 주고 숫자 맞히기

#include <stdio.h>
#include <stdlib.h>



int main()
{


  int secretNumber = 5;
  int guess;
  int guessCount = 0;
  int guessLimit = 3;
  int outOfGuesses = 0;

  while (guess!= secretNumber && outOfGuesses == 0){

        if ( guessCount < guessLimit){

      printf("Enter a number : ");
      scanf("%d", &guess);
      guessCount++;
  } else { outOfGuesses = 1;}

  }
  if (outOfGuesses == 1) {
    printf("Out of guesses");
  }else {
   printf("You win!");
  }

   return 0;

}

11. for loop

 

#include <stdio.h>
#include <stdlib.h>



int main()
{        int i = 1;
        while (i <=5) {
            printf ("%d\n", i);
            i++;        }

   return 0;}

 

#include <stdio.h>
#include <stdlib.h>



int main()
{                 int i;
    for(i = 1; i <=5; i++){

        printf("%d\n", i);    }
   return 0;}
#include <stdio.h>
#include <stdlib.h>



int main()
{
     int luckyNumbers[] = {4, 8, 15, 16, 23, 42};
     luckyNumbers[2];
     int i;
    for(i = 0; i < 6; i++){

        printf("%d\n", luckyNumbers[i]);
    }
   return 0;}

12. 2D for 반복문

#include <stdio.h>
#include <stdlib.h>



int main()
{

    int nums[3][2] = {{1, 2},{3, 4}, {5, 6}};


   int i, j ;
   for(i =0; i <3; i++){

    for(j =0; j <2; j++){     printf("%d,", nums[i][j]);    }
    printf("\n");
   }

   return 0;

}

 

반응형

댓글