Solved C Program: Basic Angle Calculation

  • Thread starter Thread starter ming2194
  • Start date Start date
  • Tags Tags
    Base Program
Click For Summary

Discussion Overview

The discussion revolves around a basic C programming problem related to angle calculation, specifically converting degrees to radians and checking if an angle is a right angle. Participants explore syntax and semantic errors in the provided code, as well as clarify the use of certain operators and data types in C programming.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant identifies syntax errors in the code, such as the incorrect use of the ampersand (&) in printf statements and the missing semicolon at the end of a statement.
  • Another participant discusses the distinction between syntax errors and semantic errors, explaining that the former prevent the program from compiling, while the latter lead to incorrect program behavior despite correct syntax.
  • There is a question regarding whether it is acceptable to use decimal fractions in C programming, with a response indicating that floating point literals can be assigned to floating point variables.
  • Participants discuss the necessity of using the ampersand (&) in scanf but not in printf, with some clarification provided about pointer types.

Areas of Agreement / Disagreement

Participants generally agree on the technical aspects of syntax and semantic errors, as well as the use of the ampersand in scanf versus printf. However, there remains some uncertainty regarding the specifics of floating point assignments and the use of pointers.

Contextual Notes

Some participants express uncertainty about the rules governing the use of the ampersand operator and the assignment of floating point values to different variable types, indicating a need for further clarification.

ming2194
Messages
31
Reaction score
0
(*sovled) basic C program problem

Homework Statement


attrached.
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

Homework Equations


attrached


The Attempt at a Solution



#include<stdio.h>
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");
else
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.
 

Attachments

  • c radian.JPG
    c radian.JPG
    22.8 KB · Views: 430
Last edited:
Physics news on Phys.org
ming2194 said:

Homework Statement


attrached.
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

Homework Equations


attrached


The Attempt at a Solution



#include<stdio.h>
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");
else
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.
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.
 
Mark44 said:
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.

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:
ming2194 said:
but i want to ask is,
"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?
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:
Mark44 said:
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 try to assign floating point values to variables that are int, long, short, char, and so on.

can i say the "&" is only needed to be added in scanf?
 
ming2194 said:
can i say the "&" is only needed to be added in scanf?
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."
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
9
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 1 ·
Replies
1
Views
5K