Selasa, 27 Oktober 2015

Looping in C programming language

Looping in C

in loop, statement will be executed sequentially. there are some syntax in loop. they are :
  1. for loop
    is a repetation control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
    example :
    #include <stdio.h>
    int main ()
    {
    int a;
    for (a=0; a<=10; a++) {
    printf ("%d ", a);
    }
    return 0;
    }

    the result will be "0 1 2 3 4 5 6 7 8 9 10"
  2. While loop
    while loop statement in c repeatdly executes a target statement as long as a given condition is teur.
    example :
    #include <stdio.h>
    int main ()
    {
    int a = 20;
    while (a<30)
    {
    printf ("%d ",a); a++;
    }
    return 0;
    }

    the output will be "20 21 22 23 24 25 26 27 28 29".
  3. Do while loop
    do while will check its condition at the bottom of the loop. but for and while will check the loop condition at the top of the loop.
    example :
    #include <stdio.h>
    int main ()
    {
    int a = 0;
    do
    {
    printf ("%d ",a); a++;
    }
    while (a<10);
    return 0;
    }

    the output will be "0 1 2 3 4 5 6 7 8 9".

Senin, 26 Oktober 2015

Decision Making / Branching in C

Decision / Branch in C

     Decision making is important concepts in c programming language. usually , program should be able to make logical or true/false decision based on the condition. the purpose of decision making/branch is to solve particular problems.
     Branching is the term given to the code executed in sequence as a result of change in the program flow. branching is the process of choosing the right branch for execution, depending on the result of the "statement condition".
     there are some syntax of decision making/branch. they are :
  1. If-else statement
    if the condition is true, the program will be executed. but if the condition expression is false, it will executed other statement.
    example :
    #include <stdio.h>
    int main ()
    int a = 5;
    if (a<10) {  // this will be check the statement condition
    printf ("this is true\n"); // if the condition true the result will print this
    }
    else {
    printf ("this is false\n"); // if the condition false the result will print this
    }
    return 0;
    }

    the result will be "this is true" because it's right that a less than 10 (a<10). that a = 5.
  2. Switch case statement
    switch case statement is used to make a selection towards expression or condition that have constant values. because of that, the expression that is defined must be produced a value of integer or character.
    example :
    #include <stdio.h>
    int main ()
    {
    int how_many_pet = 4;
    switch (how_many_pet) {
    case 5 : case 4 : case 3 : printf ("animallover"); break;
    case 2 : printf ("perfect"); break;
    case 1 : printf ("cool"); break;
    case 0 : printf ("miss"); break;
    default :
    printf ("butoklah");
    return 0;
    }
    }

    the result will be "animallover".
  3. Ternary Operator (?:)
    ternary operator can be used to replace if else statement. the general form :
    condition 1? condition 2 : condition 3
    if condition 1 is true, then condition 2 is evaluated and becomes the value of the expression. but if condition 1 is false, then condition 3 is evaluated and becomes the value of the expression.
    example :
    #include <stdio.h>
    int main ()
    {
    int love = 100;
    int match = (love>100 ? 100 : 50);
    if (love>100) {
    printf ("match = 100");
    }
    else
    {
    printf ("match = 50");
    }
    return 0;
    }

    the result will be "match = 50".

Minggu, 25 Oktober 2015

Data Types and Reading Input From User

C-Data Types, Reading input from user, Writing an output

Data Types
  • Data Types are used to define a variable before its use. but the value of the variable can be change anytime. the definition of variable will assign storage for the variable and define the type that will be held in the location. C-Data Types are :
  1. Char - it used to define a character or strings.
    the strorage size = 8 bytes
    the value range = -128 to 127 (signed char) or 0 to 255 (unsigned char)
  2. Short - target type will be optimized for space and will have width at least 16 bits.
    short storage size = 16 bytes
    the value range = -32768 to 32767 (signed) or 0-65535 (unsigned)
  3. Int - it used to define an integer. for most variables and countable things.
    int storage size = 16-32 bytes
    the value range = -32768 to 32767 or -2147483648 to 2147483647 (signed) or 0 to 65535 or 0 to 4294967295 (unsigned)
  4. Long - 32-64 bytes
  5. Long Long - 64 bytes
  6. float - it can be used for real number such as : 3,14. used to define floating point numbers.
    float storage size = 32 bytes
  7. double - is used to define BIG floating point numbers. it reserves twice the storage for the number.
    double storage size = 64 bytes
Reading Input From User, Writing Output
  • now i will give example how to read input from user and also writing an output for each data types. here are the examples :
  1. Char

    #include <stdio.h>
    int main ()
    {
    int letter;
    letter = 'A';
    printf("%c",letter);
    }

    you can convert the letter into a number by change the %c into %d.
    if you want to convert from number into a letter, you can change letter = 'A'; into letter = any number and let the %c in the printf. example :

    #include <stdio.h>
    int main ()
    {
    int letter;
    letter = 65;
    printf("%c",letter);
    }
  2. Int/short
    #include <stdio.h>
    int main ()
    {
    int i,j;
    int result;
    scanf("%d %d",&i,&j); // this is how to read input from user
    result = i+j+10; // this is how to write an output
    printf("the result :%d \n",result);
    }

    compare the result when you using short : //remember the possible value range data types
    #include <stdio.h>
    int main ()
    {
    int i,j;
    int result;
    scanf("%d %d",&i,&j); // this is how to read input from user
    result = i+j+10; // this is how to write an output
    printf("the result :%d \n",result);
    }
  3. Float/Double
    #include <stdio.h>
    int main ()
    {
    int a,b,c;
    float result;
    scanf("%d %d %d",&a,&b,&c); // this is how to read input from user
    result = 7.00/3.14 + (a+b+c); // this is how to write an output
    printf("result =%f",result);
    }

    compare the result when you using double.
    #include <stdio.h>
    int main ()
    {
    int a,b,c;
    double result;
    scanf("%d %d %d",&a,&b,&c); // this is how to read input from user
    result = 7.00/3.14 + (a+b+c);  // this is how to write an output
    printf("result =%f",result);
    }

    if you just want to make only 2 numbers after coma, you just need to change %f into %.2f
    if 3 numbers after coma, %.3f, and so on..

Sabtu, 24 Oktober 2015

How To Print Hello World In C Programming Language

How to Create A Hello World Program in C



  •      first of all, you need to begin the program with #include <stdio.h>. it will help you to read the source code. to continue, i will give an example also the explanation :


     #include <stdio.h>

     int main ()
     {
     printf("hello world \n);

     return 0;
     }


  • the main explanation :

  1. #include <stdio.h> - this line of code called as "Standard Input/Output Header File". you need this to begin your program. this will let us to use a certain commands for input or output in our program.
  2. int main () - this int is called the return value or data types (integer, float, double, char, etc). every program must have main ().
  3. {} - these curly bracket (one at the begning and one at the end) are used to make all of your command in one group together. these curly bracket also used to mark the begining and ending of a program.
  4. Return 0; - this is mean that you must return an integer int main (), when we wrote "int main()"
  5. // - this mean anything you write in 1 line after // will be as a comment.
  6. printf ("hello world \n"); - this is used to print a program, or the output. if you want to print a program, you must ended with ";"printf is used to print a program. after printf you must make a bracket (), it used to print the data inside the round bracket. also you need "". this is used to read what do you want to print. the hello world inside the "" also called as a string. this is what do you want to print. \n is called as "escape sequence" 
    1. \n = to make new line
    2. \t = tab
    3. \f = newpage
    4. \v = vertical tab
  • if you already knew and already made the following code, you can compile it by click build, compile current file ( ctrl + shift + F9).
  • if the compilation was succes, you need to save your file. give this name to your file as a beginning "helloworld.c".
  • if you have some error with the compilation. that must be something wrong or you didn't follow the instruction correctly. or the compiler was not detected, you can fix it by following this site http://howtobegininc.blogspot.co.id/2015/10/how-to-install-compiler-in-cc.html
  • after the compilation success, please build and run it by pressing F9
  • if it is succes. you will got this :

Jumat, 23 Oktober 2015

How To Install Compiler in C/C++

How To Install C/C++ Compiler

How to install code::blocks and also the GCC compiler - this tutorial will accompany you how to install code::blocks and the GCC compiler. --almost all of you maybe have a trouble when you want to learn about C/C++ and you have no software to do it. or some of you already have the software but you have no compiler and can't compile and run your program. but don't worry, now i will tell you how to fix your problem. this is the step how to install code::blocks and the compiler:

1.Step 1: Download Code::Blocks

2.Step 2:Install Code::Blocks
  • go to the folder that you save the file.
  • double click the installer and you will see this.
  • wait until it finished.
3.Step 3: Run Code::Blocks
  • after you run the code::blocks, you will see something below :
  • and then there are many options. choose "yes, associate code:blocks with C,C++ file types
  • the compiler will be installed automatically. the compiler is GNU GCC compiler
  • and you can used your code::blocks

  • after that, chose "create new project". and then the following window will be :

  • chose console aplication and click go. click next and you can choose which language do you want yo use:

  • after that you can fill the project title and the project name coloum also where do you want to save your project :

  • after you fill out the file name/title. there will be show a window menu, just click "finish". i suggest you chose your own directory folder. the project will be saved there, and then you can start to make your code by click files, new, empty file (ctr + shift + N).


  • if you already made your code, don't forget saved your file with ".cpp" extension or you can change the value to "all files".
4.The Compiler Didn't Work Properly?
  • if when you want to compile the program and it showed you an error, you set the compiler by click setting, and click compiler and then it will showed :
  • go to "Toolchain Executable" and then click auto detect.
  • or you click "Reset default".
  • now, try to compile your code again. and then run your code.