기본 콘텐츠로 건너뛰기

4월, 2017의 게시물 표시

[C Example] Student Score Average Calculator

#include <stdio.h> //p.230 int main() {     int num_stu=1, score,i, min=1000, max=-1000;     float sum;         do     {     printf("Put a number of students(from 2 to 25) : ");     scanf("%d", &num_stu);     }         while(num_stu<=1 || num_stu>25); //if the condition inside() is false, stop     for (i=1; i<=num_stu; i++)     {         do         {         printf("\nPut the %d (st/th) student's score.\n", i);         scanf("%d", &score);         if(max<=score)    max=score;         if(min>=score)    min=score;         sum+=score;         }             while (score<0 || score>101);     }             printf("The average of student's score is %f.\n", (float)sum/num_stu);     printf("maximum : %d, minimum : %d", max,min);             return 0; }

Pythagoras theorem

#include <stdio.h>//p.228 int main() {     int height, baseline, hypotenuse;         printf("height, baseline, hypotenusev\n");     for(height=1; height<=100; height++)         {         for(baseline=1; baseline<=100; baseline++)             {                    for(hypotenuse=1; hypotenuse<=100; hypotenuse++)             {                 if((hypotenuse*hypotenuse)==baseline*baseline+height*height)                 {                     printf("%d %d %d\n", height, baseline, hypotenuse);                 }             }         }     }     return 0; }

Multiples table from 2 to 9

#include <stdio.h>// p.219 int main() {     int i, line;             for(i=2; i<=9; i++ )     {         for(line=1; line<=9; line++)         {             printf("%d*%d = %d ", i, line, i*line);         }         printf("\n"); }     return 0; }

Finding 3's multiples using while

#include <stdio.h> int main() {     int i;     int num1=1;     int num2=1;         printf("Put two number to find 3's multiples between.(numbers>0)\n");     scanf("%d %d", &num1, &num2);         i = num1;         if(num1<=0 || num2<=0)         {         printf("Put two number which are bigger than 0.\n");         scanf("%d , %d", &num1, &num2);     }             while(i<=num2)     {         if(i%3==0)         {             printf("%d ", i);         }         i++;     }         return 0; }

수열 ㅎㅎ

#include <stdio.h> //교과서 p.216 int main() {     int received, i, suyeol=0;     printf("정수를 입력하시오.");     scanf("%d", &received);         for (i=1; i<=received ; i++)     {         suyeol=i*i+suyeol;      }          printf("%d의 수열 값은 %d이다.", received, suyeol);     return 0; }

Number sum until 0 put

#include <stdio.h> int main() {     int num;     int sum=0;         printf("Put an integers to add.(put 0 to stop.)\n");         do     {            scanf("%d", &num);         sum=sum+num; }     while(num!=0);             printf("The sum of numbers is %d.", sum);     return 0; }

Matching dice 7

#include <stdio.h> int main() {     int num1, num2, dice;         for (num1=1; num1<=6; num1++)     {         for (num2=1; num2<=6; num2++)         {             if(num1+num2==7)             {                 printf("%d %d \n", num1, num2);             }         }     }     return 0; }

Finding minimum

#include <stdio.h> #include <limits.h> int main(void) {     int num, minvalue = INT_MAX;         printf("Put an integers to compare. (ctrl=z to stop)\n");         while(scanf("%d", &num) != EOF)     {         if (num < minvalue)             minvalue = num;     }     printf("The minimum value is %d", minvalue);         return 0; }

Finding aliquot using For

#include <stdio.h> int main() {        int i, received, result;     printf("Put a number to find a aliquot.\n");     scanf("%d", &received);         printf("Aloquots of %d is...\n", received);         for(i=1; i<=received; i++)     {         if(100%i==0)         {             printf("%d ", i);         }     }             return 0; }

Factorial using For

#include<stdio.h> int main() {     int i=1, received=1;     long result=1;         printf("Put an integer to factorial.\n");     scanf("%d", &received);         for(i=1; i<=received; i++)     {         result*=i;     }     printf("%d! is %d.", received, result);     return 0; }

Which integer is bigger-Min

#include <stdio.h> //Min own version int main(void) {     int num1, num2, max;         printf("Put two different integers.");     scanf("%d %d", &num1, &num2);     if(num1==num2)     {         printf("Put DIFFERENT two integer.\n");     }             else if(num1>num2)     {         printf("%d is bigger than %d\n", num1, num2);     }         else if (num1<num2)     {         printf("%d is bigger than %d\n", num2, num1);        }     return 0; }

Which integer is bigger-book

#include <stdio.h> //Text book example version int main() {     int num1, num2, max;         printf("Put two different integers.");     scanf("%d %d", &num1, &num2);         if(num1>num2)     {     max = num1;     }         else if (num1<num2)     {     max = num2;        } printf("The max is %d.\n", max);    return 0; }

Size of

#include <stdio.h> int main() {     printf("short %d\t\n", sizeof(short));     printf("int %d\t\n", sizeof(int));     printf("long %d\t\n", sizeof(long));     printf("longlong %d\t\n", sizeof(long long));     printf("float %d\t\n", sizeof(float));    //longlong (x) long long(o)     printf("double %d\t\n", sizeof(double)); //date type of sizeof = %d     printf("long double %d\t\n", sizeof(long double));     return 0; }

Relational operator examples

#include <stdio.h> //False>>0, true>>1 are printed out int main(void) {     int num1, num2;         printf("Put two integers.");     scanf("%d %d", &num1, &num2);         printf("Result of %d == %d = %d\n", num1, num2, num1 == num2); //Are they same?     printf("Result of %d != %d = %d\n", num1, num2, num1 != num2); //Are they different?     printf("Result of %d > %d = %d\n", num1, num2, num1 > num2); //Is num1 bigger than num2?     printf("Result of %d < %d = %d\n", num1, num2, num1 < num2); //Is num1 smaller than num2?     printf("Result of %d >= %d = %d\n", num1, num2, num1 >= num2);     printf("Result of %d <= %d = %d\n", num1, num2, num1 <= num2); return 0; }

Odd or even?

#include <stdio.h> int main(void) {     int num;     printf("Put the integer.");     scanf("%d", &num);         if (num==0)     printf("Put another number but not 0.\n");             else if(num%2==0)     {         printf("The integer is even.\n");     }     else if(num%2==1)     {         printf("The integer is odd.\n");     }     return 0; }

Multiplication table

#include <stdio.h> int main() {     int num=0, i=1, com=1;     printf("Put a number to see multiple table!\n");     scanf("%d", &num);     com=num;         while(i<10)     {         printf("%d * %d is %d.\n", num, i, num*i);     ++i; } return 0; }

Leap or not?

#include <stdio.h> int main() {     int year;         printf("Input the year.\n");     scanf("%d", &year);         if((year%100!= 0) && ((year%4==0) || (year%400==0 && year%100 != 100)))     {         printf("%d is a leap year.\n", year);     }         else //else if (x), if there is no more condition to attacth after one if.     {         printf("%d is not a leap year.\n", year);     }         return 0;     }

Judging score

#include <stdio.h> //broken but I don't know why.....8ㅅ8 int main() {    char credit;     printf("Input your credit.(in captical letter)");     scanf("%c", credit);         switch('credit') {         case 'A':             printf("Perfect!");             break;         case 'B':             printf("great!");             break;            case 'C':             printf("Nice!");             break;         default :             printf("Is it a CREDIT...?!\n nah kidding, do your best next time!");             break; }     return 0; } //I don't want to judge anyone's score but my text book does :'(

Finding multiples

#include <stdio.h> int main() {    int num, i=0, multiples=0, limit;     printf("Put a number.\n");     scanf("%d", &num);     printf("Between 1 and\n");     scanf("%d", &limit);     while (i<=limit)     {    i++;         if(i%num==0)         {         multiples+=i;            }         ;     }         printf("Sum of all multiples of %d between 1 and %d is %d", num, limit, multiples);     return 0; }

[C Example]Factorial

#include <stdio.h> int main() {     int i=1,num=0, sum=1;     printf("Put a number to do factorial\n");     scanf("%d", &num);         while(i<=num) {     sum*=i;     ++i; }     printf("result is %d",sum); //,value0     return 0; }

Euro change calculator

#include <stdio.h> //change calculator int main(void) {     int price;     int money;     int change;     printf("Put the total price of product.");     scanf("%d", &price);     printf("Put received money");     scanf("%d", &money);     change=money-price;     if (change>0)     {         printf("500euro = %d\n", change/500);         printf("200euro = %d\n", change%500/200);         printf("100euro = %d\n", change%500%200/100);         printf("50euro = %d\n", change%500%200%100/50);         printf("20euro = %d\n", change%500%200%100%50/20);         printf("10euro = %d\n", change%500%200%100%50%20/10);         printf("5euro = %d\n", change%500%200%100%50%20%10/5);     }         else if (change==0)     {     printf("no change is needed.\n");     }         else if (change<0)     {         printf("payment error.\n");     }         return 0; }

Days of months

#include <stdio.h> int main() {     int month, days;         printf("Put the month.");     scanf("%d", &month);         switch(month) //switch(the name of the value {         case 4:     case 6:     case 9:     case 11:         days = 30;         break;         case 2 :         days = 28;         break;                 default :         days = 30;         break; }     printf("Total days of %d is %d",month, days);     return 0; }

Conditional - volume comparing

#include <stdio.h> int main() {     int num1, num2;         printf("Put two integers to compare.\n");     scanf("%d %d", &num1, &num2);         printf("Bigger number is %d\n", (num1>num2)?num1:num2);      printf("Smaller number is %d\n", (num1<num2)?num1:num2); //(condition)?true/false to be printed     return 0;      }

Check your score!! (if practice)

#include <stdio.h> int main() {     int score;         printf("Input your score.");     scanf("%d", &score);         if(score>=90)     {         printf("A");     }         else if(score>=80)     {         printf("B");     }     else if(score>=70)     {         printf("C");     }            else if(score<70)     {         printf("Fail");     }     return 0;     }

Celsious to fahrenheit cal

#include <stdio.h> //change calculator int main(void) {     float celsious;     float fahrenheit;         printf("Put the celsious tempt.");     scanf("%f.", &celsious);         fahrenheit=celsious*(float)9/5+32;         printf("fahrenheit temp is %f", fahrenheit);         return 0; }

Arithmetic operator calculator

#include <stdio.h> int main() {     int num1, num2, expr;     char oper;         printf("Put an expression");     scanf("%d %c %d", &num1, &oper, &num2);         if(oper=='+') //charactor must be blocked by single quotes. ''     {     expr=num1 + num2;     }         else if(oper=='-')     {     expr=num1 - num2;     }         else if(oper=='/')     {     expr=num1 / num2;     }         else if(oper=='*')     {     expr=num1 * num2;     }         printf("%d %c %d is %d", num1, oper, num2, expr);         return 0; }

Area and perimeter calculator

#include <stdio.h> int main() { double r=0; double perimeter=0; double PI=3.14; double area;     printf("Put a radious");     scanf("%lf", &r);         area=PI*r*r;     perimeter=PI*2.0*r;         printf("Area of the circle is %lf.\n", area);     printf("Perimeter of the circle is %lf.\n", perimeter);     return 0;    }

Adding until the count

#include <stdio.h> p.197 int main() { int i=0,count,total=0, value=0;     printf("How much times you want to add?");     scanf("%d", &count); do {     printf("put that number~!\n");     scanf("%d", &value);     total+=value;     ++i; } while (i<count);     {     printf("%d times of added sum is %d.", count, total); }     return 0;    }

Adding from x to y

#include <stdio.h> int main() {     int i=0,num1=0, num2=0, sum=0;     printf("Put the first number and last number to add.\n");     scanf("%d %d", &num1, &num2);     i=num1;         while(i<num2+1) {        sum+=i;     ++i; }     printf("Sum from %d to %d is %d", num1, num2, sum);     return 0; }