8.1.01. Napisati program za izračunavanje sume niza:
1 - 1/2 + 1/3 -+ ... -+ 1/n
Listing programa:
//12173001 izracunavanje sume niza 1 - 1/2 + 1/3 -+ ... -+ 1/n
#include<iostream>
#include<cstdlib>
#include<math.h>
using namespace std;
int main()
{
double s=0,n;
cout<<"n: ";
cin>>n;
for(int i=1; i<=n; ++i)
{
if(i%2==1)
{
s+=(1.0/i);
}else
{
s-=(1.0/i);
}
}
cout<<"Vrijednost sume: "<<s<<endl;
return 0;
}
II Varijanta
//p12173001//
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int i,n;
double z,suma;
cout<<"Do broja?"<>n;
z=1;
suma=0;
if (n < 1 )
{
cout<<"Nekorektan unos!"<<=n;i++)
suma=suma+(z*1/i);
z= - z;
}
cout<<"Suma +- 1/n ="<<suma<<endl;
return 0;
}
Ispis na ekranu:
![](../../../pascal/pascal_prir/081/120_izvedi/12173001.gif)
Index
|