# Thread: Writing math in C

1. ## Writing math in C

Hello everyone, this is my first time posting here so any help would be appreciated. i am currently taking a class on C language, its an intro class. I have no previous knowledge on how to work with C language and i was wondering if anyone could help me with the following problems. i am using Codeblocks.

Write a C program that outputs to the screen the following arithmetic operations
¡ 7 + 3 * 5 – 2
¡ 4 + 7 / 3
¡ 8 % 3 * 6
¡ (7 + 3) * 5 – 2

Write a program to calculate distance between two points.
In your code do the following:
Include <math.h>
Ask the user to enter 2 points (x1,y1) and (x2,y2).
Calculate distance using the formula: sqrt ( (y2-y1)^2 + (x2-x1)^2)
Use sqrt and pow functions

2. Originally Posted by powerfox0
Hello everyone, this is my first time posting here so any help would be appreciated. i am currently taking a class on C language, its an intro class. I have no previous knowledge on how to work with C language and i was wondering if anyone could help me with the following problems. i am using Codeblocks.

Write a C program that outputs to the screen the following arithmetic operations
¡ 7 + 3 * 5 – 2
¡ 4 + 7 / 3
¡ 8 % 3 * 6
¡ (7 + 3) * 5 – 2

Write a program to calculate distance between two points.
In your code do the following:
Include <math.h>
Ask the user to enter 2 points (x1,y1) and (x2,y2).
Calculate distance using the formula: sqrt ( (y2-y1)^2 + (x2-x1)^2)
Use sqrt and pow functions
Hi,
Have you attempted to solve this?
Announcements - General Programming Boards

3. Originally Posted by bos1234
Hi,
Have you attempted to solve this?
Announcements - General Programming Boards
well i have done it mathematically, I just have no prior knowledge of writing anything in code, what would be the basic steps?

4. Please go to the top of this website, and click on the "C Tutorial" button - it's an excellent tutorial. (Follow C, not C++).

As far as the match goes, remember the order of operations from math. C is very similar. If you don't have the table of precedence of these operations, download one from the net (Google is your friend). The Tutorial may also have that table.

That might get you through this, but do post back (with your code), if you run into problems. Without code, your questions can't be specifically and authoritatively, addressed. It's a huge waste of time, if we can't see the actual code you're talking about.

5. this is for the second algorithm, but I keep encountering an error at last line, says error: expected declaration or statement at end of input. Any help?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main ()
{
float x1, x2, y1, y2, distance;
printf ("Enter value of x1: ");
scanf ("%f", &x1);
printf ("Enter value of x2: ");
scanf ("%f", &x2);
printf ("Enter value of y1: ");
scanf ("%f", &y1);
printf ("Enter value of y2: ");
scanf ("%f", &y2);
distance=sqrt(pow(y2-y1,2)+pow(x2-x1,2));
printf ("Value of distance: %g\n", distance);
putchar ('\n');
system ("pause");
return 0;

6. Where is your closing brace at the end?

7. Originally Posted by Salem
Where is your closing brace at the end?
THANK YOU! yes got the second part working perfectly, now just to get the first part underway

8. Originally Posted by powerfox0
THANK YOU! yes got the second part working perfectly, now just to get the first part underway
Hi,
With the first question, do you want to display the answer?

1. declare a variable
2. assign the arithmetic operation to that variable
3. print variable

9. here is what I got for the first part, but once again i am encountering errors, specifically 2 errors at line 12 saying error: stray '\226' in program and error: expected ';' before numerical constant

any help? this is the code below:

Code:
```int main()
{
int sum;
sum = 7+3*5-2;
printf("the sum of the first equation is %d", sum);
int sum2;
sum2 = 4+7/3;
printf("the sum of the second equation is %d", sum2);
int sum3;
sum3 = 8%3*6;
printf("the sum of the third equation is %d", sum3);
int sum4;
sum4 = (7+3)*5–2;
printf("the sum of the fourth equation is %d", sum4);
return EXIT_SUCCESS;
}```

10. also how can i get each answer to display on a seperate line?

11. Originally Posted by powerfox0
also how can i get each answer to display on a seperate line?
You had it in yout first code...
printf ("Value of distance: %g\n", distance);

12. Originally Posted by Salem
You had it in yout first code...
printf ("Value of distance: %g\n", distance);
understood, now can you help me with the errors? it seems to be only with the last sum.... what might it be?

13. Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int sum1;
sum1 = 7+3*5-2;
printf("the first equation is 7+3*5-2 \n");
printf("the answer of the first equation is %d\n", sum1);
int sum2;
sum2 = 4+7/3;
printf("the second equation is 4+7/3 \n");
printf("the answer of the second equation is %d\n", sum2);
int sum3;
sum3 = 8%3*6;
printf("the third equation is 8/3*6 \n");
printf("the answer of the third equation is %d\n", sum3);
int sum4;
sum4 = (7+3)*5-2;
printf("the fourth equation is (7+3)*5-2 \n");
printf("the answer of the fourth equation is %d\n", sum4);
return EXIT_SUCCESS;
}```
thank you to everyone for their help.
this is my final code for part 1, it seems to be working. can someone please verify that this is correct?

14. Originally Posted by powerfox0
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int sum1;
sum1 = 7+3*5-2;
printf("the first equation is 7+3*5-2 \n");
printf("the answer of the first equation is %d\n", sum1);
int sum2;
sum2 = 4+7/3;
printf("the second equation is 4+7/3 \n");
printf("the answer of the second equation is %d\n", sum2);
int sum3;
sum3 = 8%3*6;
printf("the third equation is 8/3*6 \n");
printf("the answer of the third equation is %d\n", sum3);
int sum4;
sum4 = (7+3)*5-2;
printf("the fourth equation is (7+3)*5-2 \n");
printf("the answer of the fourth equation is %d\n", sum4);
return EXIT_SUCCESS;
}```
thank you to everyone for their help.
this is my final code for part 1, it seems to be working. can someone please verify that this is correct?
Does it run for you? It does not run for me unless I declare the variables at the top (see code). I think its something to do with the different C standards. With C89, you have to declare variables at the top of the scope.

Also, look at sum3. There is a mistake there. Can you find it? HINT: sum3 is the correct value but the sentence is incorrect

Everything else seems to be good. Perhaps add paragraphs and comments to finish it off?

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
int sum1, sum2, sum3, sum4;

sum1 = 7+3*5-2;
printf("the first equation is 7+3*5-2 \n");
printf("the answer of the first equation is %d\n", sum1);

//int sum2;
sum2 = 4 + 7/3;
printf("the second equation is 4+7/3 \n");
printf("the answer of the second equation is %d\n", sum2);

//int sum3;
sum3 = 8%3*6;
printf("the third equation is 8/3*6 \n");
printf("the answer of the third equation is %d\n", sum3);

//int sum4;
sum4 = (7+3)*5-2;
printf("the fourth equation is (7+3)*5-2 \n");
printf("the answer of the fourth equation is %d\n", sum4);

getchar();getchar();

return 0;
}```

15. Originally Posted by powerfox0
this is my final code for part 1, it seems to be working. can someone please verify that this is correct?
Judging from the assignment you're probably supposed to verify with a calculator or from pencil and paper. In C for example the following will give an unexpected result

5 + 7/8

the reason for the result is because of integer division and there is no rounding.