際際滷

際際滷Share a Scribd company logo
While petlja
Petla While se naje邸e koristi ako broj ponavlanja petle nije unaprijed poznat, nego zavisi o ispunjenju zadanog
uslova. Osnovni oblik While petle je:
while (uslov)
{
blok naredbi
}
naredba iza bloka
Kod ove vrste petli uslov se ispituje na poetku, a blok naredbi petle
se ponavla sve dok je uslov istinit (razliit od 0). Kada uslov postane
neistinit (false, 0), program se nastavla prvom naredbom iza petle.
Uslov mo転e biti promjenjiva, aritmetiki izraz ili logiki izraz.
Kako se kod petle while uslov ispituje na poetku mo転e se dogoditi
da se blok naredbi u petli ne izvr邸i niti jednom.
Po邸to petla for i while imaju ispitivanje uslova na poetku vrijedi da
se svaka programska petla for mo転e zamijeniti petlom while i
obrnuto. Kada e se koristiti koja petla zavisi od samog programera i
njegovih navika.
1. Napi邸i program (While) koji ispisuje prirodne brojeve do broja n.
#include <iostream>
using namespace std;
int main()
{
int i,n;
cin>>n;
i=1;
while(i<n)
{
cout<<i<<" ";
i++;
}
return 0;
}
2.Napi邸i program (While) koji rauna i ispisuje sumu prirodnih brojeva do broja n.
#include <iostream>
using namespace std;
int main()
{
int i,s,n;
cout<<"n= ";
cin>>n;
s=0;
i=1;
while(i<n)
{
s=s+i;
i++;
}
cout<<"Suma do "<< n <<" je "<<s<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i,s,n,br;
cout<<"n= ";
cin>>n;
i=1;
br=0;
while(i<n )
{
if(i%2==0)
cout<<i<<" ";
if(i%2==0)
br=br+1;;
i++;
}
cout<<endl;
cout<<"parnih ima "<<br;
return 0;
}
Napi邸ite program koji ispisuje parne brojeve i njihov broj do unesenog broja n.
Napi邸ite program koji ispisuje cifre unesenog broja n.
#include <iostream>
using namespace std;
int main()
{
int i,n;
cout<<"n= ";
cin>>n;
while(n>0)
{
i=n%10;
n=n/10;
cout<<i<<" ";
i++;
}
cout<<endl;
return 0;
}
Do while petlja
U ovoj petli blok naredbi se izvodi sve dok je uslov
ispunjen, a izve邸e se bar jedanput bez obzira na
tanost uslova.
Uslov se ispituje nakon izvoenja bloka naredbi petle.
U ovome i jeste osnovna razlika izmeu petli for i
while, i petle do while.
Zadatak 1. Napi邸i program koji rauna i ispisuje sumu
parnih brojeva do broja n.
#include <iostream>
using namespace std;
int main()
{
int i,n,s;
cout<<"n= ";
cin>>n;
i=0;
s=0;
do
{
if(i%2==0)
s=s+i;
i++;
}
while(i<n);
cout<<"s= "<<s<<endl;
return 0;
}
Zadatak 2: Napi邸i program koji ispisuje sve unesene brojeve dok ne unesete 0. Nakon
uno邸enja 0 program prekida rad.
#include <iostream>
using namespace std;
int main()
{
int i;
cin>>i;
do
{
cin>>i;
}
while(i>0);
return 0;
}
Zadatak 3: Napi邸i program koji ispisuje najvei uneseni broj . Unos brojeva je sve dok ne unesete 0.
#include <iostream>
using namespace std;
int main()
{
int i,max;
cin>>i;
max=i;
do
{
cin>>i;
if(i>max) max=i;
}
while(i>0);
cout<<endl;
cout<<max;
return 0;
}
Zadaca 8.
1. Napisi program (While) koji ispisuje prirodne brojeve do broja n.
2. Napi邸ite program koji ispisuje parne brojeve i njihov broj do unesenog broja n.
Ulaz:9 Izlaz: 2 4 6 8
Ulaz:9 Izlaz: 4
3. Napi邸i program koji ispisuje cifre unesenog broja n. Npr. N=305 Izkaz: 3 0 5
Do while
1. Do while! Napi邸i program koji ispisuje sumu parnih brojeva do broja n.
2. Napi邸i program koji ispisuje sve unesene brojeve dok ne unesete 0. Nakon uno邸enja 0 program prekida rad.
3. Napi邸i program koji ispisuje najvei uneseni broj . Unos brojeva je sve dok ne unesete 0.
4. Napi邸i program koji unosi brojeve sve dok ne uneste 0 i rauna aritmeticku sredinu unesenih brojeva.
5. Napi邸i program koji sabira pozitivne brojeve sve dok ne unesete negativan broj. Nakon unosa negativnog broja program ispisuje sumu i
prekida rad.
6. Ispisati prirodne brojeve od 1 do 5 koji su djeljivi sa a.
7. Izraunati i ispisati sumu kvadrata prvih n prirodnih brojeva
8. Napisati program za ispis sume reciprocnih vrijednosti prvih n prirodnih brojeva.
9. Prebrojati prirodne brojeve u intervalu od 1 do n koji nisu djeljivi sa a.
10. Uitati visine i te転inu za n osoba. Ispisati prosjenu, najmanju i najveu visinu i te転inu.

More Related Content

Featured (20)

2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design ProcessStorytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
Artificial Intelligence, Data and Competition SCHREPEL June 2024 OECD dis...
Artificial Intelligence, Data and Competition  SCHREPEL  June 2024 OECD dis...Artificial Intelligence, Data and Competition  SCHREPEL  June 2024 OECD dis...
Artificial Intelligence, Data and Competition SCHREPEL June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
2024 State of Marketing Report by Hubspot
2024 State of Marketing Report  by Hubspot2024 State of Marketing Report  by Hubspot
2024 State of Marketing Report by Hubspot
Marius Sescu
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design ProcessStorytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
2024 State of Marketing Report by Hubspot
2024 State of Marketing Report  by Hubspot2024 State of Marketing Report  by Hubspot
2024 State of Marketing Report by Hubspot
Marius Sescu
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray

4. while do while

  • 1. While petlja Petla While se naje邸e koristi ako broj ponavlanja petle nije unaprijed poznat, nego zavisi o ispunjenju zadanog uslova. Osnovni oblik While petle je: while (uslov) { blok naredbi } naredba iza bloka Kod ove vrste petli uslov se ispituje na poetku, a blok naredbi petle se ponavla sve dok je uslov istinit (razliit od 0). Kada uslov postane neistinit (false, 0), program se nastavla prvom naredbom iza petle. Uslov mo転e biti promjenjiva, aritmetiki izraz ili logiki izraz. Kako se kod petle while uslov ispituje na poetku mo転e se dogoditi da se blok naredbi u petli ne izvr邸i niti jednom. Po邸to petla for i while imaju ispitivanje uslova na poetku vrijedi da se svaka programska petla for mo転e zamijeniti petlom while i obrnuto. Kada e se koristiti koja petla zavisi od samog programera i njegovih navika.
  • 2. 1. Napi邸i program (While) koji ispisuje prirodne brojeve do broja n. #include <iostream> using namespace std; int main() { int i,n; cin>>n; i=1; while(i<n) { cout<<i<<" "; i++; } return 0; }
  • 3. 2.Napi邸i program (While) koji rauna i ispisuje sumu prirodnih brojeva do broja n. #include <iostream> using namespace std; int main() { int i,s,n; cout<<"n= "; cin>>n; s=0; i=1; while(i<n) { s=s+i; i++; } cout<<"Suma do "<< n <<" je "<<s<<endl; return 0; }
  • 4. #include <iostream> using namespace std; int main() { int i,s,n,br; cout<<"n= "; cin>>n; i=1; br=0; while(i<n ) { if(i%2==0) cout<<i<<" "; if(i%2==0) br=br+1;; i++; } cout<<endl; cout<<"parnih ima "<<br; return 0; } Napi邸ite program koji ispisuje parne brojeve i njihov broj do unesenog broja n.
  • 5. Napi邸ite program koji ispisuje cifre unesenog broja n. #include <iostream> using namespace std; int main() { int i,n; cout<<"n= "; cin>>n; while(n>0) { i=n%10; n=n/10; cout<<i<<" "; i++; } cout<<endl; return 0; }
  • 6. Do while petlja U ovoj petli blok naredbi se izvodi sve dok je uslov ispunjen, a izve邸e se bar jedanput bez obzira na tanost uslova. Uslov se ispituje nakon izvoenja bloka naredbi petle. U ovome i jeste osnovna razlika izmeu petli for i while, i petle do while. Zadatak 1. Napi邸i program koji rauna i ispisuje sumu parnih brojeva do broja n. #include <iostream> using namespace std; int main() { int i,n,s; cout<<"n= "; cin>>n; i=0; s=0; do { if(i%2==0) s=s+i; i++; } while(i<n); cout<<"s= "<<s<<endl; return 0; }
  • 7. Zadatak 2: Napi邸i program koji ispisuje sve unesene brojeve dok ne unesete 0. Nakon uno邸enja 0 program prekida rad. #include <iostream> using namespace std; int main() { int i; cin>>i; do { cin>>i; } while(i>0); return 0; }
  • 8. Zadatak 3: Napi邸i program koji ispisuje najvei uneseni broj . Unos brojeva je sve dok ne unesete 0. #include <iostream> using namespace std; int main() { int i,max; cin>>i; max=i; do { cin>>i; if(i>max) max=i; } while(i>0); cout<<endl; cout<<max; return 0; }
  • 9. Zadaca 8. 1. Napisi program (While) koji ispisuje prirodne brojeve do broja n. 2. Napi邸ite program koji ispisuje parne brojeve i njihov broj do unesenog broja n. Ulaz:9 Izlaz: 2 4 6 8 Ulaz:9 Izlaz: 4 3. Napi邸i program koji ispisuje cifre unesenog broja n. Npr. N=305 Izkaz: 3 0 5 Do while 1. Do while! Napi邸i program koji ispisuje sumu parnih brojeva do broja n. 2. Napi邸i program koji ispisuje sve unesene brojeve dok ne unesete 0. Nakon uno邸enja 0 program prekida rad. 3. Napi邸i program koji ispisuje najvei uneseni broj . Unos brojeva je sve dok ne unesete 0. 4. Napi邸i program koji unosi brojeve sve dok ne uneste 0 i rauna aritmeticku sredinu unesenih brojeva. 5. Napi邸i program koji sabira pozitivne brojeve sve dok ne unesete negativan broj. Nakon unosa negativnog broja program ispisuje sumu i prekida rad. 6. Ispisati prirodne brojeve od 1 do 5 koji su djeljivi sa a. 7. Izraunati i ispisati sumu kvadrata prvih n prirodnih brojeva 8. Napisati program za ispis sume reciprocnih vrijednosti prvih n prirodnih brojeva. 9. Prebrojati prirodne brojeve u intervalu od 1 do n koji nisu djeljivi sa a. 10. Uitati visine i te転inu za n osoba. Ispisati prosjenu, najmanju i najveu visinu i te転inu.