# Using the (int) cast

• 09-22-2010
minyoungan
Using the (int) cast
Hello,

I solved some exercises on the textbook called A First Book of ANSI C by myself and really happy about that. I have a major problem with one exercise. It says i have to use the (int) cast, devise a method to round the values in the variables a and b to the nearest hundredth (penny value) before they are added.

I have no idea what the question means!!! Any advice? I read a different book i brought from Korea to solve it in my own way but I dont know its right or not.

I tried to find solutions for the textbook but cant find it.
Question
Code:

```    double a, b, c;     a = 1.674;     b = 1.322;     printf("\n%4.2f",a);     printf("\n%4.2f",b);     printf("\n----");     c = a + b;     printf("\n%4.2f\n",c);```
My code:
Code:

```#include<stdio.h> #include <math.h> int main() {     double a, b, c;     a = 1.674;     b = 1.322;     double  round (double x);     a = round(a*100)/100;     b = round(b*100)/100;     printf("\n%4.2f",a);     printf("\n%4.2f",b);     printf("\n----");     c = a + b;     printf("\n%4.2f\n",c);         return 0; }```
• 09-22-2010
Salem
double a = 12.3456;

a = a * 100;
becomes 1234.56

a = (int)a;
becomes 1234.00
• 09-24-2010
minyoungan
I got it:)
Thank you very much!!

Code:

``` #include<stdio.h> main() {     double a, b, c;     a = 1.674;     b = 1.322;         printf("\n%4.2f",a);     printf("\n%4.2f",b);         a = a * 100;     b = b * 100;     printf("\n----");         //round the values in the variables a and b to the nearest hundredth     a = (int) a;     b = (int) b;         //add the variables a and b     c = a + b;     c = c / 100;         //provide the required result     printf("\n%4.2f\n",c);         return 0; }```
Quote:

Originally Posted by Salem