:-/
Now, I'm trying practice on c++ basics, since few minutes I tried practice "for loop" especially, in console screen, I wrote this trivial program to calculate sin angles between angle (0 to 90) with for loop,
but when i build it, it gave me one result like my attach image, why program gave me one result..?

//sin angles from 0 to 90 | loop for
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double angle=0;
	double sinAngle = sin(angle);

	for (angle=0; angle<=90; angle++)
		cout << sinAngle << " " << endl;
	
	return 0;
}

Dani AI

Generated

Quick diagnosis: the program computes sin once and prints that same value repeatedly. explained this — the call to sin was evaluated before the loop, so the loop just prints the stored result 91 times. Also remember C++ sin expects radians, so a 0–90 degree range must be converted to radians first.

A clear, practical pattern is to loop over degrees (or a fractional step), convert to radians inside the loop, compute sin, and then print. The example below shows one safe way to do that and formats the output for readability:

#include <iostream>
#include <cmath>
#include <iomanip>

int main() {
    const double PI = 3.14159265358979323846;
    for (int deg = 0; deg <= 90; ++deg) {
        double rad = deg * PI / 180.0;
        double s = std::sin(rad);
        std::cout << std::setw(3) << deg << " deg: "
                  << std::fixed << std::setprecision(6) << s << '\n';
    }
}

Additional tips and cautions:

  • Use a double result computed inside the loop (not a single precomputed value). This addresses the OP ’s problem directly and follows ’s advice to recompute per iteration.
  • M_PI is not guaranteed by the standard; prefer your own PI constant or std::numbers::pi if your compiler supports C++20.
  • If you need fractional steps (e.g., 0.5 degrees), iterate with a double step but avoid exact equality checks — use a tolerance or count steps.
  • For reference on the math functions and the radians requirement, see std::sin - cppreference.

Stepping through the code in a debugger or printing deg, rad, and s each iteration makes the behavior obvious — this is what helped diagnose the original post.

Recommended Answers

All 9 Replies

Can you please post your code?

I edit post and added code ..

You only did the calculation once:

double angle=0;
double sinAngle = sin(angle);

This doesn't magically assign that function to sinAngle, it evaluates it at angle 0 and prints it over and over again in your loop. Leave the declaration double sinAngle; outside and move the sinAngle = sin(angle); inside your loop.

commented: Indeed it does :) +19

I confused ..!
please, can you edit my code ..

Step through the code to see:

int main()
{
	double angle=0;       //angle = 0
	double sinAngle = sin(angle);  //sinAngle =  0 at this point

	for (angle=0; angle<=90; angle++)  
		cout << sinAngle << " " << endl;  //sinAngle hasn't changed
	                                       //and won't change so the value ^^^
                                              //is printed 91 times
	return 0;
}

Compare with (skip the other two variables):

for (double angle=0; angle<=90; angle++)  //since you're going by 1 anyway you could use int
		cout << sin(angle) << " " << endl;  //angle is changing each 
                                                                          //pass through

P.S. the math library uses radians so you need to go from 0 to Pi/2 if you want 0-90 in degrees

Thanks JONSCA for helping me ..

Do you understand it now (is the important question)?

SURE and thanks to you

This doesn't magically assign that function to sinAngle

Indeed it does :) - Salem

Clarification:
What I meant was that OP couldn't say double sinAngle = sin(angle); and then have the function sin(angle) (in the abstract) substitute back in for the value sinAngle down below like the OP wanted it to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.