Basic operator/basic character is used in arithmetic opeartion on C programming language are:
Will be show hoe to make program of multiplication, divison, addition, and substraction of 2 integers, the way is:
Hopefully useful, thank you... : )
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:
- Open Dev C++
- File menu > New > Source File, then will be created a new worksheet titled Untitled1
- 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
- First, type minimal required syntax on C programming:
main()
{
} - Add getch() function in main() function to show program result while be Run:
main()
{
getch();
} - Define two integers as integer variable signed int
main()
{
signed int num1;
signed int num2;
getch();
}
- Define both of the int signed variable as input:
main()
{
signed int num1;
signed int num2;
scanf("%d", &num1);
scanf("%d", &num2);
getch();
}
- 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();
}
- 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();
}
- 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();
}
- 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();
}
- 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();
}
- 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();
}
- 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();
}
Hopefully useful, thank you... : )
0 komentar:
Posting Komentar