Kamis, Juni 19, 2014

Program C Basic Arithmetic Operation

Basic operator/basic character is used in arithmetic opeartion on C programming language are:
operator description
* multiplication
/ division
+ addition
- substraction

Will be show hoe to make program of multiplication, divison, addition, and substraction of 2 integers, the way is:
  1. Open Dev C++
  2. File menu > New > Source File, then will be created a new worksheet titled Untitled1
  3. File menu > Save As. On Save as type: choose C source files (*.c) and on File name, example we give name program of basic artimetic operation.c
  4. First, type minimal required syntax on C programming:

    main()
    {
    }


  5. Add getch() function in main() function to show program result while be Run:

    main()
    {
      getch();
    }


  6. Define two integers as integer variable signed int

    main()
    {
      signed int num1;
      signed int num2;
      getch();
    }


  7. Define both of the int signed variable as input:

    main()
    {
      signed int num1;
      signed int num2;
      scanf("%d", &num1);
      scanf("%d", &num2);
      getch();
    }


  8. Add sentence so that user of program knows when input integer:

    main()
    {
      signed int num1;
      signed int num2;
      printf("Input first integer! ");
      scanf("%d", &num1);
      printf("Input second integer! ");
      scanf("%d", &num2);
      getch();
    }


  9. For example we want to show result of multiplication of two integers, use printf() function with syntax:

    printf("%d", name_of_var_int_signed1*nama_var_of_var_int_signed2)

    so that program syntax becomes:

    main()
    {
      signed int num1;
      signed int num2;
      printf("Input first integer! ");
      scanf("%d", &num1);
      printf("Input second integer! ");
      scanf("%d", &num2);
      printf("%d", num1*num2);
      getch();
    }


  10. Before show result of multiplication, can also show again both of integers with syntax:

    printf("%dx%d = %d", name_of_var_int_signed1, name_of_var_int_signed2, name_of_var_int_signed1*name_of_var_int_signed2);

    so that program syntax becomes:

    main()
    {
      signed int num1;
      signed int num2;
      printf("Input first integer! ");
      scanf("%d", &num1);
      printf("Input second integer! ");
      scanf("%d", &num2);
      printf("%dx%d = %d", num1, num2, num1*num2);
      getch();
    }


  11. Can also add sentence so that becomes interesting:

    main()
    {
      signed int num1;
      signed int num2;
      printf("Input first integer! ");
      scanf("%d", &num1);
      printf("Input second integer! ");
      scanf("%d", &num2);
      printf("Number1 x Number2 = %dx%d = %d", num1, num2, num1*num2);
      getch();
    }


  12. To show result of divison, addition, and substraction just conform with each operator, for divison use "/", addition use "+", and substraction use "-"

    main()
    {
      signed int num1;
      signed int num2;
      printf("Input first integer! ");
      scanf("%d", &num1);
      printf("Input second integer! ");
      scanf("%d", &num2);
      printf("Number1 x Number2 = %dx%d = %d", num1, num2, num1*num2);
      printf("Number1 : Number2 = %dx%d = %d", num1, num2, num1/num2);
      printf("Number1 + Number2 = %dx%d = %d", num1, num2, num1+num2);
      printf("Number1 - Number2 = %dx%d = %d", num1, num2, num1-num2);
      getch();
    }


  13. So that look dapper, every result of arithmetic operation can be show of each in one line by insert printf("\n")

    main()
    {
      signed int num1;
      signed int num2;
      printf("Input first integer! ");
      scanf("%d", &num1);
      printf("Input second integer! ");
      scanf("%d", &num2);
      printf("Number1 x Number2 = %dx%d = %d", num1, num2, num1*num2);
      printf("\nNumber1 : Number2 = %dx%d = %d", num1, num2, num1/num2);
      printf("\nNumber1 + Number2 = %dx%d = %d", num1, num2, num1+num2);
      printf("\nNumber1 - Number2 = %dx%d = %d", num1, num2, num1-num2);
      getch();
    }


  14. If want to separater 4 result of operation can be add printf("\n") function on line which show result of multiplication:

    main()
    {
      signed int num1;
      signed int num2;
      printf("Input first integer! ");
      scanf("%d", &num1);
      printf("Input second integer! ");
      scanf("%d", &num2);
      printf("\nNumber1 x Number2 = %dx%d = %d", num1, num2, num1*num2);
      printf("\nNumber1 : Number2 = %dx%d = %d", num1, num2, num1/num2);
      printf("\nNumber1 + Number2 = %dx%d = %d", num1, num2, num1+num2);
      printf("\nNumber1 - Number2 = %dx%d = %d", num1, num2, num1-num2);
      getch();
    }


On program syntax above is used int integer variable typed signed. In fact not of all integer variables can be used to make program like this. If used char and short int integer variable, occur false, please try, don't know why (if there is one knows, please share... ^_^), and normall if change with long int integer variable.

Hopefully useful, thank you... : )

0 komentar: