Primeri-ciklične strukture

Pr1.

Napravite program koji prikayuje brojeve od 10 do 100 sa korakom 10?

Rešenje:

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
int i;
cout<<„brojevi od 10 do 100 sa korakom 10 su:“<<endl;
for (i=10;i<=100;i+=10)
{
cout<<i<<“ „;

}
system(„PAUSE“);
return EXIT_SUCCESS;
}

Pr2:

Napravite program koji prikazuje parne i neparne brojeve prve stotine?

Rešenje:

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
int i;
cout<<„neparne brojevi prve desetice“<<endl;
for (i=1;i<=100;i+=2)
{
cout<<i<<“ „;
}
cout<<“ „<<endl;
cout<<„parne brojevi prve desetice“<<endl;
for (i=2;i<=100;i+=2)
{
cout<<i<<“ „;

}
system(„PAUSE“);
return EXIT_SUCCESS;
}

Pr3: 

Napravite program koji izračunava zbir prvih n prirodnih brojeva?

Rešenje:

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
int i,n,s;
cout<<„Koliko brojeva sabires“<<endl;
cin>>n;
s=0;
for (i=1;i<=n;i++)
{
s+=i;
}
cout<<“ suma prvih „<<n<<„prirodnih brojeva je „<<s<<endl;

system(„PAUSE“);
return EXIT_SUCCESS;
}

Pr4:

Napravite program koji prebrojava  sve prirodne brojeve djeljive sa 7 iz intervala  M, N. Ispis neka bude:

Upisi pocetnu vrednost intervala:

Upisi zavrsnu vrednost raspona:

U rasponu od … do … ima …  brojeva djeljivih sa 7.

Rešenje:

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
int i,m,n,broj;
cout<<„Unesi pocetnu vrednost intervala“<<endl;
cin>>n;
cout<<„Unesi krajnu vrednost intervala“<<endl;
cin>>m;

broj=0;
for (i=n;i<=m;i++)
{
if (i%7==0)
{
broj=broj+1;
}

}
cout<<„U intervalu [„<<n<<„,“<<m<<„]“;
cout<<„postoje „<<broj<<“ deljivih sa 7″<<endl;

system(„PAUSE“);
return EXIT_SUCCESS;
}

Pr 5:

Unesite N brojeva preko tastature (N nije unapred poznato), zatim prikaži najvećeg od unetih N brojeva?

Rešenje:

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
int i,x,max;
cout<<„Unesi broj“<<endl;
cin>>x;
max=x;
do
{
if(max<x)
{
max=x;
}
cout<<„Unesi broj/za kraj unesi 0″<<endl;
cin>>x;
}
while(x!=0) ;
cout<<„max=“<<max<<endl;
system(„PAUSE“);
return EXIT_SUCCESS;
}

Pr 6.

Napravi program koji ispisuje  tablicu množenja odabranog broja sa brojevima od 1 do 10. Broj bira korisnik. Oblik ispis :

Upisi broj sa kojim zelis mnoziti:
… * 1 = …
… * 2 = …



… * 10 = …

 Rešenje:

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
int i,x;
cout<<„Unesi broj“<<endl;
cin>>x;
for (i=1;i<=10;i++)
{
cout<<x<<„*“<<i<<„=“<<i*x<<endl;
}

system(„PAUSE“);
return EXIT_SUCCESS;
}

Pr:

Napravite program koji prikazuje i sumira sve trocifrene brojeve deljive sa 3 ?

Rešenje:

 

#include <iostream>

using namespace std;

int main()
{
int i,s;

for (i=100;i<=999;i++)
{
if(i%3==0)
{
cout<<i<<“ „;
s+=i;
}
}
cout<<“ „<<endl;
cout<<„suma trocifrenih brojeva deljivih sa 3 je“<<s<<endl;

system(„PAUSE“);
return EXIT_SUCCESS;