Loops
Loops
Loops
Loops
Loop Types:
C++ programming language provides the following type of loops to handle
looping requirements.
#include<iostream>
void main()
{
int a, num;
cout << "Enter any number : ";
cin >> num;
for (a=1;a<=num;a++)
cout << "\nHello...!!"; }
The output:
for(i=1;i<=10;i++)
s+=i;
) اعداد؟ (مجهول10 حساب مجموع
for(i=1;i<=10;i++)
{ cin>>x;
s+=x; }
while ( condition )
{
Statmentes;
--- - -- - -- -- - -
}
for (i=4;i<=100;i+=2)
------------------------------------------------------------------------
i=4;
while (i<=100)
{ cout<<i;
i+=2; } // 4 6 8 … 98 100 by while
---------------------------------------------------------------------------
i=4;
do
{ cout<<i;
i+=2;
} // 4 6 8 … 98 100 by do-while
while (i<=100);
اكتب برنامج لحساب مجموع قائمة من االرقام تنتهي بالرقم صفر؟/س
Q// Write a program to calculate the sum of a list of numbers ending the
number zero?
#include<iostream>
using namespace std;
int main()
{
int x,s=0;
Cin>>x;
While(x!=0)
{s+=x; cin>>x; }
cout<<s;
return 0;}
#include<iostream>
using namespace std;
int main()
{
int x,s=0;
do
{cin>>x;
s+=x;} while(x!=0);
cout<<s;
return 0;
}
اذا كان عدد اولي او ال؟X اكتب برنامج لفحص قيمة/س
Q / Write a program to check X value if the number of first or not?
#include<iostream>
using namespace std;
int main()
{
int x,flag,i; flag=0; cin>>x;
for (i=2;i<x;i++)
if(x%i==0) flag=1;
if (flag==0)
cout<<"x is prime";
else
cout<<"x is NOT prime";
return 0; }
ارقام مختلفة؟10 من5 و4 اكتب برنامج لحساب مجموع االرقام الصحيحة التي تقبل القسمة على/س
Q1//write a program to compute the sum of the integers that divisible by 4 and
5 from 10 different integers?
اكتب برنامج لحساب مجموع االرقام الموجبة لقائمة من االرقام تنتهي بالرقم صفر؟/س
Q2// Write a program to calculate the sum of the positive numbers to the list of
numbers ending the number zero?
الحلقات او التكرارات- Loops OR Iterations :
Removing all the expressions gives us an infinite loop. This loop's condition is
assumed to be always true:
for ( ; ; ) // infinity loop
something;
&&&&&&&&&&&&&&&&&&&&&&&&&
int i,j,n=6;
for (i = 0, j = 0; i + j < n; ++i, ++j)
cout << '(' << i << ',' << j << ")\n";
Output :
/-----------
(0,0)
(1,1)
(2,2)
الحلقات او التكرارات- Loops OR Iterations :
Because loops are statements, they can appear inside other loops. In other
words, loops can be nested. For example,
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j)
cout << "(" << i << "," << j << ")\n";
Output :
-----------
(1,1)
(1,2)
(1,3)
(2,1)
(2,2)
(2,3)
(3,1)
(3,2)
الحلقات او التكرارات- Loops OR Iterations :
(3,3)
Q//write a program used nested loop to calculate "X" ,where:- X= 1!+ 3!+ 5!+
7!+9!
#include<iostream>
using namespace std;
int main()
{ int i,j,f=1,x=0;
for(i=1;i<=9;i+=2)
{ f=1;
for(j=1;j<=i;j++)
f*=j;
cout<<"factorial " <<i<<"= "<<f<<endl;
x+=f; }
cout<<"Sum ="<<x<<endl;
return 0; }
الحلقات او التكرارات- Loops OR Iterations :
Q//what’s output:
#include<iostream>
using namespace std;
int main() Output :
{ int i,j;
for(i=1;i<=4;i++)
{
-----------
for(j=1;j<=4;j++)
cout<<"*";
****
cout<<endl;
} ****
return 0;
} ****
****
الحلقات او التكرارات- Loops OR Iterations :
Output :
Q//what’s output:
#include<iostream>
using namespace std;
-----------
int main()
{ int i,j; *
for(i=1;i<=4;i++)
{ **
for(j=1;j<=i;j++)
cout<<"*";
cout<<endl;
***
}
return 0; ****
}
Example
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a = 10;
// while loop execution
while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}return o;}
The output:
value of a: 10
value of a: 11
يقوم هذا البرنامج بطباعة قيمة
value of a: 12
المتغيرa ( ( طالما الشرط
value of a: 13 a<20 متحقق اوTRUE
.
.
value of a: 19
Homework: trace the following c++ cods and Find the final output for
these cods :
#include <iostream>
int main()
{
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;