Anasayfa » C / C++

C / C++ Courses ? 5 (Calculations)

3 Ağustos 2010 1.780 views 4 Yorum

Let’s continue with calculation in C!

First of all, we should know about order of mathematical operations. Basicly, we can say the order is like that:

1) ( ) paranthesis

2) * (product), / (division) , % (remainder of division)

3) + (summation), (difference)

If operations are in the same order ( like + and – ), than the priority belongs to one that comes first.
Example;
int x= 5 * 4 / 2 + ( 4 + 2 );
If we print, it will find x like that;
x= 5 * 4 / 2 + 6 // first priority for paranthesis
x=20 / 2 + 6 // * and / have same order, but higher than +. then which comes first,this is *, will operate first
x=10 + 6
x=16
Result is 16.

When we are dealing with the same type, result has also same type.

9/2 = 4

7/11 = 0

7%8 = 7

9%2 = 1

Ex; Find the value of them

1) int  x= 7+3*6/2-1;                  //x=15

2) int x= 2%2+2*2-2/2;            //x=3

3) int x=3*9+(3+(9*3/3));     //x=39

Let’s write a program which the user enters how many bottles of water there are in the store.A bottle can hold 0,75 liters of water. Program will calculate and print the total amount of water in the store.

#include<stdio.h>

int main(){

int bottle;

float total;

printf("Enter how many bottles there are in the store: ");

scanf("%d",&bottle);

total=0,75*bottle;

printf("Store has %f liter of water", total);

return 0;

}

»Extra:

int x,y;
x=5;
y=4;
float av= (x+y) / 2;

What does av equal to?

Answer is in  the following lesson;)

?Answer of previous lesson’s extra:

float x=3,5;

int y;

y=x;

printf( “%d”, y ); // it will write 3 on the screen. because y is integer and y=3,5 means, it equals to 3.

int x=6;

float y;

y=x;

printf( “%f” ,y); // it will print 6,000000. because y is float and y=6 means, it equals to 6,000000

(after comma, it prints six numbers. If we write 3,67, it will print 3,670000. )

What should we do, if we write this number as 3,67 on the screen?

We will learn these in the future.

Yes folks, that’s it!

4 Yorum »

Yoruma Açığız! :)

Be nice. İçeriği çirkin olmadığı sürece her yoruma, öneriye ve soruya "evet" :)

Etiketler: , , , , , , , , , ,