program wyswietla czały czas 1
#include <iostream>
using namespace std ;
main()
{
int s,k,n;
cout<<"podaj liczbe"<<endl;
cin>>n;
s=1;
k=1;
for(int i=1; i<=n ; i++)
s=s*k;
k++;
cout<<"silnia to "<<s<<endl;
system("pause");
}
program wyswietla czały czas 1
#include <iostream>
using namespace std ;
main()
{
int s,k,n;
cout<<"podaj liczbe"<<endl;
cin>>n;
s=1;
k=1;
for(int i=1; i<=n ; i++)
s=s*k;
k++;
cout<<"silnia to "<<s<<endl;
system("pause");
}
A teraz powiedz mi skąd kompilator ma wiedzieć które instrukcje chciałeś mieć "wewnątrz pętli" a które poza nią. W jaki sposób to zdefiniowałeś? Czemu wydaje ci się że instrukcje
s=s*k;
k++;
powinny wykonać się w pętli a
cout<<"silnia to "<<s<<endl;
system("pause");
już nie?
Jak widzisz brak tu elmentarnej logiki. Otóż C++ pozwala na wykonanie tylko jednej instrukcji wewnątrz pętli. Ale blok instrukcji otoczony { i } jest traktowany jako jedna instrukcja.
#include <iostream>
using namespace std;
int main()
{
unsigned n,s=1;
cout<<"podaj liczbe"<<endl;
cin>>n;
while(n>1) s*=n--;
cout<<"silnia to "<<s<<endl;
cin.sync();
cin.get();
return 0;
}