complete newbie here. For my homework at school, I was given the opportunity to write a program that displays -
s = 1 + 1/2 + 1/3 + 1/4 ..... + 1 / n
Here is what I did -
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a;
float s=0, n;
cin>>a;
for(n=1;n<=a;n++)
{
s+=1/n;
}
cout<<s;
getch();
}
It perfectly displays what you need. However, in the past I only had written programs that use the int data type. In my opinion, the int data type does not contain a decimal place, whereas float does. So I don’t know much about swimming yet. Later that night, I watched a video on YouTube in which he wrote the same program, but in a slightly different way. The video was in some foreign language, so I could not understand it. What he did was declared as an "integer".
int a, n;
float s=0;
instead
int a
float s=0, n;
. . -
s+=1.0f/n;
s+=1/(float)n;
, 'n' ( ?). , : , ? "n" float, 1.0f n.f f.n. , . 1 (float)/n 1/(float) n? , float 1. , 1.f 1.0f?
, . , , - "n" ? . 's' float. , . , !
.