1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Base C program problem

  1. Oct 31, 2009 #1
    (*sovled) basic C program problem

    1. The problem statement, all variables and given/known data
    and one more thing is:
    if the angle is 90, a statement "This is a right angle." should be appeared in the last rows,
    else, "This is not a right angle" instead.

    also, the degee is 90 (interger) but not 90.0

    2. Relevant equations

    3. The attempt at a solution

    int main ()
    { int degree;
    float radian;

    printf("Enter the angle in Degree:\n");
    scanf( "%d", &degree );
    printf("\nDegree is %d",&degree);

    radian= degree/57.295779
    printf("\nRadian is %3d",&radian);

    if (degree!=90)
    printf("\nThis is not a right angle");
    printf("\nThis is a right angle");

    return 0;

    im just a beginner of writing c programe.
    so maybe there many mistakes in my work.
    plx forgiving that.

    Attached Files:

    Last edited: Nov 1, 2009
  2. jcsd
  3. Oct 31, 2009 #2


    Staff: Mentor

    You have one syntax error and several semantic errors. A syntax error is an error in how you have used the C programming language. The compiler will not produce an executable program when it encounters syntax errors. A semantic error is one in which C is used correctly by the program will produce incorrect values.

    1. The line below is incorrect.
      printf("\nDegree is %d",&degree);

      Remove the & from &degree. You are telling printf to print the address of degree, not the value stored there. The ampersand is needed in calls to scanf but not in calls to printf. There's an explanation for this, but you're probably not ready for it just yet.
    2. This line has your syntax error.
      radian= degree/57.295779
      Every statement in C must end with a semicolon.
    3. This line is incorrect for two reasons.
      printf("\nRadian is %3d",&radian);
      a. The & should not be used.
      b. The format specifier, %3d, does not match radian's type. Use %f instead.
  4. Oct 31, 2009 #3
    Thanks vy much for your correction.
    i tested it in c-free programe and the sets work perfect.

    but i want to ask is,

    1. can i say the "&" is only needed to be added in scanf?

    2. "radian= degree/57.295779" <-- it is okay to type decimal fraction in c programe?
    or it is only valid if i specifiy the radian that is float at the beginning?
    Last edited: Oct 31, 2009
  5. Oct 31, 2009 #4


    Staff: Mentor

    I'm not sure I understand your question. You can assign floating point literals (such as 57.295779) to floating point variables, such as float and double. You should not try to assign floating point values to variables that are int, long, short, char, and so on.

    Edit: Added "not" to previous sentence.
    Last edited: Nov 1, 2009
  6. Oct 31, 2009 #5
    can i say the "&" is only needed to be added in scanf?
  7. Nov 1, 2009 #6


    Staff: Mentor

    Most of the time, you need the & with scanf variable parameters but not with those in printf. The only time you don't need & with a scanf parameter is when the variable is already a pointer type. I'm sure you're not working with pointer variables yet, so I won't go into the reasons behind why & is used in scanf.

    Note that I omitted one word in my previous post. I meant to say "You should NOT try to assign floating point values to variables that are int, long, short, char, and so on."
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook