# Program converting degree to radians from 0 to n. Printing a # of lines

Printable View

• 10-03-2012
tadm123
Program converting degree to radians from 0 to n. Printing a # of lines
Here's the problem:

Write a C program to generate a table of conversion from degrees to radians. Your table should include a third column that contains the sine of the angle. Print 10 lines int he table with starting angle at 0 degree and allow the user to enter the ending angle in degrees.

Here's my code so far:

Code:

```#include <stdio.h> #include <math.h> #define PI 3.1416     int main(void) {       double radians,sinx;       int degrees;       int end;                     printf(" Enter the ending angle in degrees");       scanf("%d",&end);         for(degrees=0;degrees<=end;degrees=degrees+10)           {                                       radians=degrees*PI/180;               sinx= sin(radians);               printf("%d %.3f %.3f\n",degrees,radians,sinx);       }       getch(); }```
But my problem is mainly on what to do to just print 10 lines Here's my attempt.

I tried using this:
Code:

`    for(degrees=0,k=1;degrees<=end,k<=10;degrees=degrees+10,k++)`
I don't know what to do to print 10 lines in the loop proportional to the ending degree value. I know I messed up incrementing it by 10 in here which would mess up the loop, but I don't know what else to do. Please any help would be appreciated.
• 10-03-2012
anduril462
You're on the right track for you loop, printing exactly 10 lines. The problem is, as you stated, figuring out how much to increment by. Work it out on paper first. Start with some simple ones:

From 0 to 10, 10 to 20, 100 to 110, etc.
From 0 to 20, 40 to 60, 100 to 120, etc.

Then onto more complicated ones.
From 10 to 25, 30 to 75, etc.

Figure out what it is you do on paper, paying attention to all the little steps and details involved. Once you understand how to do the problem in general, you can worry about coding it.

Hint: there's subtraction and division involved.
• 10-03-2012
qny
You're almost there -- you just need to tweak the last expression inside the for statement.
Code:

`for (degrees = 0; degrees <= end; degrees = degrees + <WHATEVER IS NEEDED TO GET TO end IN 10 ITERATIONS>)`
~~~~~~~~

But allow me to suggest an alternative.
You know you only want to loop a definite number of times (10), so loop that number and fix the variables inside the loop accordingly
Code:

```for (loopcount = 0; loopcount < 10; loopcount++) {     degrees = /* fix degrees according to loopcount */;     radians = degrees * PI / 180;     sinx = sin(radians);     printf("%d %.3f %.3f\n", degrees, radians, sinx); }```
• 10-03-2012
tadm123
Thanks so much everyone now it works, can't believe I missed something so simple all this time heh.

Thanks again :)
• 10-03-2012
sparkomemphis
Quote:

Originally Posted by tadm123
Code:

```#include <stdio.h> #include <math.h> #define PI 3.1416     int main(void) {       double radians,sinx;       int degrees;       int end,incr;                           printf(" Enter the ending angle in degrees");       scanf("%d",&end);         incr= (end-0)/10;       for(degrees=0;degrees<=end;degrees=degrees+incr)           {                                           radians=degrees*PI/180;               sinx= sin(radians);               printf("%d %.3f %.3f\n",degrees,radians,sinx);       }       getch(); }```
Thanks so much everyone now it works, can't believe I missed something so simple all this time heh.

Thanks again :)

what happens when the user enters an value less than 10 ?
Hint in integer math 9/10 = 0
• 10-03-2012
tadm123
Oh thanks for pointing it out, i'll change it to double.
• 10-03-2012
tadm123
Actually hey man can you edit your last post where I posted the final code? No pressure but when you type the problem in google it's redirected here, I just don't want people trying to copy pasting it since it's for an exam. :o