Lecture 6 Loops
Lecture 6 Loops
LOOPS
Lecture 6
Display your Name
Loops
•}
PRINT TABLE OF 2
for(int i=1;i<=10;i++)
{
cout<<2*i;
}
for(int i=0;i<=20;i=i+2)
{
cout<<i;
}
HOME TASK
• Take the value from the user and print its table.
• Take values from 1 to 5 and perform their addition.
Display your name 10 times
using for loop
Example
#include <iostream>
using namespace std;
int main ()
{ // for loop execution
for( int a = 10; a < 20; a = a + 1 )
{
cout << "value of a: " << a << endl;
}
return 0;
}
For live demo click
https://www.tutorialspoint.com/compile_cpp_online.php
Sum of numbers using for loop
#include <iostream>
using namespace std;
int main(void)
{
int sum=0;
int num=1;
for (int i=0; i<5; i++)
{
sum+=num;
cout<<" Sum is "<< sum<<"\n";
}
}
Sum of numbers using for loop
#include <iostream>
using namespace std;
int main(void)
{
int sum=0;
int num=1;
for (int i=0; i=5; i++)
{
sum+=num;
cout<<" Sum is "<< sum<<"\n";
}
}
Task
Get the sum of five numbers by taking the numbers from the
user.
The for loop
#include <iostream.h>
Output
#include <conio.h> 1 10
void main(void) 29
{ clrscr();
int sum=0; 38
for(int a=1;a<=10;a++)
{ 47
cout<<a<<"\t"<<(11-a)<<endl;
sum += a;
56
} 65
cout<<"Sum is "<<sum;
getch(); 74
83
}
92
10 1
Sum is 55
Display elements of array using for loop
#include <iostream>
using namespace std;
int main()
{
int arr[]={21,9,56,99, 202};
/* set the value of variable i to 0 as the array
index starts with 0 */
for(int i=0; i<5; i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
Operation of for loop
#include <iostream>
int main()
{
for ( int counter = 10; counter >= 0; counter-- )
cout << counter << " \n";
}
Display 2 table using for loop
Factorial !
#include <iostream.h>
#include <conio.h>
void main(void)
{ clrscr();
int num, fact=1;
cout<<"Enter a number ";
cin>>num;
for(int a=num;a>0;a--)
{
fact *= a;
}
cout<<"Factorial of "<<num<<" is "<<fact;
getch();
}
TASK
Output
#include <iostream.h> 1 10
#include <conio.h> 29
38
void main(void) 47
{ clrscr(); 56
65
int a=1;
74
while(a<=10) 83
{ 92
cout<<a<<"\t"<<(11-a)<<endl; 10 1
a++;
}
getch();
}
Operation of while loop
The while loop
#include <iostream.h>
#include <conio.h>
void main(void)
{ clrscr();
int a=0;
while(a!=100)
{
cout<<a<<"\t"<<(100-a)<<endl;
a=a+25;
}
getch();
}
The while loop
#include<iostream.h>
#include<conio.h>
void main(void)
{ clrscr();
int a=15;
while(a<1 || a>10)
{
cout<<"Enter a value (1-10) ";
cin>>a;
}
cout<<"Value entered is between 1 - 10";
getch();
}
The do while loop
}while(a<=10);
getch();
}
Operation of do while loop
Output
Enter Table value 4
The do
#include<iostream.h>
while loop 4x1=4
4x2=8
#include<conio.h> 4 x 3 = 12
4 x 4 = 16
void main(void) 4 x 5 = 20
4 x 6 = 24
{ char ch; int t; 4 x 7 = 28
4 x 8 = 32
do 4 x 9 = 36
{ clrscr(); 4 x 10 = 40
Continue n
cout<<"Enter Table value "; Thanks for using program
cin>>t;
for(int a=1;a<=10;a++)
cout<<t<<" x "<<a<<" = "<<t*a<<endl;
cout<<“Press Y to Continue ";
ch=getche();
}while(ch=='Y' || ch=='y');
cout<<"\nThanks for using program";
}
Output
Enter Table value 4
4x1=4
4x2=8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
Continue n
Thanks for using program