0% found this document useful (0 votes)
9 views52 pages

Iteration - Series Part 1

The document provides an overview of loop structures in programming, detailing entry-controlled and exit-controlled loops, including for, while, and do-while loops. It includes example programs for printing numbers, calculating sums of odd and even numbers, and converting loop types. Additionally, it discusses the execution flow of loops and provides sample outputs for various code snippets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views52 pages

Iteration - Series Part 1

The document provides an overview of loop structures in programming, detailing entry-controlled and exit-controlled loops, including for, while, and do-while loops. It includes example programs for printing numbers, calculating sums of odd and even numbers, and converting loop types. Additionally, it discusses the execution flow of loops and provides sample outputs for various code snippets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

L.

No 6 Iteration
Loop: repeating the steps till the given condition is true

Structure of loop:
1. Control var: var has initial value and it goes till the end of the loop
2. Body: set of statements to be repeated
3. Test condition: condition till which the loop should execute
4. Step value: it is the increase or decrease of control var to reach the
condition
Types of loop:

Entry Controlled Exit Controlled

1. It first checks the condition 1. it first executes and


then executes the statement then checks the condition
2. If the condition is true then 2.It executes atleast once even if
Only executes the statements the condition is false
3. Eg for , while 3. Eg do while
for loop:
while loop: Syntax
Syntax for( initilization ; cond true; Inc/dec )
Initilization of var {
while( cond true) Statements;
{ }
Statements;
do while loop:
inc/dec
Syntax
}
Initilization of var
do
{
Statements;
inc/dec
} while( cond true);
Write a program to print number 1 to 10

import java.util.*; import java.util.*; import java.util.*;


class loop class loop class loop
{ { {
psvm() psvm() psvm()
{ { {
int ctr=1; for( int ctr=1;ctr<=10 ; ctr++) int ctr=1;
while( ctr<=10) { do {
{ sopln( ctr);
sopln( ctr); sopln( ctr);
ctr++; }// for ctr++;
}//while }} } while( ctr<=10); // do while
}} }}
Write a program to print number 1 to 10
Write a program to print even number 1 to 20
import java.util.*;
import java.util.*; class loop import java.util.*;
class loop { class loop
{ psvm() {
psvm() { psvm()
{ for( int ctr=2;ctr<=20 ; ctr=ctr+2) {
int ctr=2; { int ctr=2;
while( ctr<=20) sopln( ctr); do
{ {
sopln( ctr); }// for sopln( ctr);
ctr=ctr+2; }} ctr=ctr+2;
}//while } while( ctr<=20); // do while
}} }}
Write a program to find sum of odd and even number from 1 to 20;
import java.util.*;
class OE a a<=20 a%2==0 E+a O+a
1 1<=20t 1%2==0f 0+1=1
{ psvm() 2 2<=20t 2%2==0t 0+2=2
{ 3 3 <=20t 3%2==0f 1+3=4
int a=1; int O=0; int E=0; 4 4<=20t 4%2==0t 2+4=6
while(a<=20)
int O=0; int E=0;
{
for( int a=1;a<=20;a++)
if(a%2==0) {
E=E+a; if(a%2==0)
else E=E+a;
O=O+a; else
O=O+a;
a++;
} // for
} Sopln(“Even sum”+E);
Sopln(“Even sum”+E); Sopln(“Odd sum”+O);}} Sopln(“Odd sum”+O); }}
Write a program to find sum of odd and even number from 1
to 20;
class loop a a<=20 a%2==0 E+a O+a
{ 1 1<=20t 1%2==0f 0+1=1
psvm() 2 2<=20t 2%2==0t 0+2=2
{ Scanner…….. 3 3<=20t 3%2==0f 1+3=4
4 4<=20t 4%2==0t 2+4=6
int a=1; int E=0; int O=0;
5 5<=20t 5%2==0 f 4+5=9
while( a<=20)
{ if( a%2==0) int E=0; int O=0;
E=E+a; for( int a=1;a<=20; a++)
else { if( a%2==0)
O=O+a; E=E+a;
a++; else
}// while O=O+a;
Sopln(“Even sum is”+E); }
Sopln(“Odd sum is”+O); Sopln(“Even sum is”+E);
}//main Sopln(“Odd sum is”+O);
}//class }//main
}//class
Write a program to find sum of odd and even number from 1 to 20;
import java.util.*; import java.util.*;
class loop class loop
{ {
psvm() psvm()
{ {int sumo=0; int sume=0;
int ctr=1; int sumo=0; int sume=0; for( int ctr=1;ctr<=20 ; ctr++)
while( ctr<=20) {if (ctr %2==0)
{ sume=sume+ctr;
if (ctr %2==0) else
sume=sume+ctr; sumo=sumo+ctr;
else
sumo=sumo+ctr; }// for
ctr++; }//while sopln(sume); sopln(sumo);
sopln(sume); sopln(sumo);}} }}
Q1. Convert following do-while loop Q2. Convert into equivalent for loop
into for loop. int i=0;
int i = 1; while( i<=20)
int d=5; { sopln(i+ “ “);
do { i++;
d=d*2; }
System.out.println(d);
i++ ; } while ( i<=5); Q3. Convert into while loop
int f=1;int i=1;
for( i=1;i<=5;i++)
{ f*=I;
Sopln(f);}
Q1. int d=1; Q2. for( int i=0;i<=20;i++)
for (int i =1;i<=5 ; i++) {
sopln(i + “ “);
{ }
d= d*2;
System.out.println(d);
} Q3. int f=1;int i=1;
while( i<=5)
{ f*=i;
Sopln(f);
i++;
}
For loop: ways of writing for loop
1. for( int i=0 ; i<=10; )
{ Infinite loop/ endless loop
i++;
}
for( int i=0;i>=0; i++)
{ SOPln(i);
2. int i=0; }
for( ;i<=10;i++)
{ } for( ; true ; )
{ Sopln(“hi’); }

Null or bodyless loop

3. for( int i=0,int j=0; i<=10;i++,j++)


{
}
Q1. Find the output for the following snippets. Also mention how many times the loop will
be repeated.
I i>0 sopln(i) i=i/10
a) for(int i=7392;i>0;i=i/10) 7392 7392>0t 7392 739
{ 739 739>0 t 739 73
System.out.println(i); 73 73>0 t 73 7
} 7 7>0 t 7 0
0 0>0f
loop executes 4 times
b) int a=0; I i>0 sop(i) a=a+(i%10) Sopln(a) i=i/10
for(int i=7392;i>0;i=i/10) 7392 7392>0 7392 0+2=2 2 739
{ System.out.print(i); 739 739>0 739 2+9=11 11 73
a=a+(i%10); 73 73>0 73 11+3=14 14 7
System.out.println(a); 7 7>0 7 14+7=21 21 0
} 0 0>0 f
loop exe 4 times
73922
73911
7314
721
I i>0 a=a+(i%10) sopln(a) i=i/10
c) int a=0; 7392 7392>0 0+2=2 2 739
for(int i=7392;i>0;i=i/10) 739 739>0 2+9=11 11 73
{ a=a+(i%10); 7393 73>0 11+3=14 14 7
System.out.println(a); 7394 7>0 14++7=21 21 0
} 0 0>0
Loop runs 4 times
2
11
14
21
I i>0 sopln(i) sopln(i%10) i/10
73921 73921>0 73921 1 7392
7392 7392>0 7392 2 739
e) for(int i=73921;i>0;i=i/10)
739 739>0 739 9 73
{ System.out.println(i);
73 73>0 73 3 7
System.out.println(i%10);
7 7>0 7 0 0
}
0 0>0 f
Loop runs 5 times
73921
1
7392
2
739 I i<=6 sop(a+ “,”) a=a*i i++
9 1 1<=6 1, 1,2,6,24,120, 1*1=1 2
f) int a=1; 2 2<=6 1*2=2 3
73
for(int i=1;i<=6;i++) 3 3<=6 2*3=6 4
3
{ 4 4<=6 6*4=24 5
7
System.out.print(a+ ", "); 5 5<=6 24*5=120 6
0
a=a*i; 6 6<=6 120*6=720 7
} 7 7<=6 f
loop runs 6 times
1,1,2,6,24,120,
g) int a=1; I i<=6 sop(a+”,”) i++
1 1<=6 1,1,1,1,1,1,7 2
for(int i=1;i<=6;i++) 2 2<=6 3
{System.out.print(a+ ", "); } 3 3<=6 4
a=a*i; 4 4<=6 5
5 5<=6 6
System.out.println(a); 6 6<=6 7
7 7<=6 f Qh)
Loop runs 6 time I i<=6 a=a*i
1,1,1,1,1,1,7 1 1<=6t 1*1=1 2
h) int a=1; 2 2<=6t 1*2=2 3
for(int i=1;i<=6;i++) 3 3<=6t 2*3=6 4
4 4<=6t 6*4=24 5
{ a=a*i; } 5 5<=6t 24*5=120 6
System.out.println(a); 6 6<=6t 120*6=720 7
7 7<=6f
Loop runs 6 times Loop runs 6 times
i) int a=1; 720
1
for(int i=1;i<=6;i++) 2
{ a=a*i; 6
System.out.println(a); 24
120
} 720
Write a program to print the all the odd numbers between the start and
end numbers entered by the user.(inclusive of start and end values)
import java.util.*;
class loop1
{
psvm() s 10 e 15
{ Scanner sc=new Scanner(System.in); I i<=e i%2!=0 sopln(i)
Sopln(“Enter start and end value”);
int s=sc.nextInt(); 10 10<=15t 10%2!=0 ------
int e=sc.nextInt(); 11 11<=15t 11%2!=0t 11
for ( int i=s;i<=e;i++) 12 12<=15t 12%2!=0f ____
{
if( i %2 !=0) 13 13<=15t 13%2!=0 t 13
Sopln(i); 14 14<=15 t 14%2!=0 ____
}//for 15 15<=15t 15%2!=0 t 15
}//class
}//main 16 16<=15 f
Write a program to print all the numbers divisible by 3 and 5
between the range entered by the user
(accept start and end value from the user, inclusive of both the value)
class loop2
{
psvm() s= 10 e = 16
{ Scanner sc=new Scanner(System.in);
Sopln(“Enter start and end value”); I i<=e i%3==0 & i%5==0 sopln(i)
int s=sc.nextInt(); 10 10<=16t 10%3=0f 10%5=0t ------
int e=sc.nextInt(); 11 11<=16t 11%3=0f 11%5=0f------
for ( int i=s;i<=e;i++) 12 12<=16t 12%3=0t 12%5=0 f------
{ if( i%3==0 && i%5==0) 13 13<=16t 13%3=0f 13%5=0 f_____
Sopln( i); 14 14<=16t 14%3=0f 14%5=0f ------
}//for 15 15<=16 t 15%3=0 15%5=0 15
}//main 16 16<=16 t 16%3=0f 16%5=0f---
}// class 17 17<=16f
Write a program to print the sum and average of all the even numbers
between the range entered by the user. Inclusive start value and exclusive
end value.
avg= sum/ctr;
class loop6 sopln(“Sum is “+sum);
{
sopln(“Avg is”+avg);
psvm()
{ Scanner sc=new Scanner(System.in); }//main
Sopln(“Enter start and end value”); }//class
int s=sc.nextInt();
int e=sc.nextInt(); s= 10 e = 17
int sum=0; double avg=0.0d; int ctr=0; I I<=e i%2==0 sum+I ctr++
for ( int i=s;i<=e;i++) 10 10<=17t 10%2==0 t 0+10 1
{ if ( i %2==0) 11 11<=17t 11%2==0f
{ sum=sum+i; 12 12<=17 t 12%2==0t 10+12=22 2
ctr++; 13 13<=17t 13%2==0f
}//if 14
}// for
Write a program to print the sum of all the numbers divisible by x between start
and end value.Accept x,start and end value from the user

class loop7
{
psvm() Sopln(“ sum is”+ sum);
{ Scanner sc=new Scanner(System.in); }//main
Sopln(“Enter start and end value”); }//class
int s=sc.nextInt();
int e=sc.nextInt();
Sopln(“Enter the value of X”);
s= 10 e 17 x=3 12, 15
int x=sc.nextInt();
I i<=e i%x==0 sum+I
int sum=0;
for ( int i=s;i<=e;i++)
{ if( i%x==0)
sum=sum+i;
}//for
1. x= 1+2+3+4+-------n
i position of the term
• Series x= term value
import….
class
{ psvm()..
{ scanner….
Sopln(“Enter the number of terms”);
int n=sc.nextInt();
int sum=0;
int x=1;
for( int i=1;i<=n;i++)
{ sum=sum+x; // sum=sum+i
x++;
} sopln(“Sum is”+sum);}}
2. 1+ ½ +1/3+1/4+……1/n
Sopln(“Enter the number of terms”);
int n=sc.nextInt();
double sum=0.0d;
int x=1;
for( int i=1;i<=n;i++)
{ sum=sum+(1.0/x);
x++;
}
sopln(“Sum is”+sum);}}
3 ) 1-2+3- 4+-------n
i 1 2 3 4
Sopln(“Enter the no of terms”);
int n=sc.nextInt();
Int sum=0;
int x=1;
for( int i=1;i<=n;i++)
{ if (i%2==0)
sum=sum-x;
else
sum=sum+x;
x++;
}
Sopln(sum);
4. 1+
Sopln(“Enter the number of terms”);
int n=sc.nextInt();
Sopln(“Enter the value of a”);
int a=sc.nextInt();
double sum=1.0;
int x=2;
for( int i=2;i<=n;i++)
{ sum=sum+ Math.pow(a,x);
x++;
}
sopln(“Sum is”+sum);}}
4. a/2+a/3+a/4+---------a/(n+1)

Sopln(“Enter the nos of terms”);


int n= sc.nextInt();
Sopln(“Enter a”);
int a=sc.nextInt();
double sum=0.0d;
int x=2;
for( int i=1; i<=n;i++)
{ sum=sum+(double)(a/x);
x++;
}
Sopln(“Sum is “+sum);
}} n , a int to read the value
I ,x int position of term
sum double to find sum
x y
y z

5.(1+2)/(2*3) + 2+3/3*4 +--------n

Sopln(“Enter the nos of terms”);


int n= sc.nextInt();
double sum=0.0d; int x=1;int y=2; int z=3;
for( int i=1;i<=n;i++)
{
Sum=sum+(double)((x+y)/(y*z))
x++;y++;z++;
} Sopln(“Sum is “+sum);
1+(x2/2)+(x3/3)+------xn/n
Sopln(“Enter the nos of terms”); 1 2 3
int n= sc.nextInt();
Sopln(“Enter x”);
int x=sc.nextInt();
double sum=1.0;
int y=2;
for( int i=2; i<=n;i++)
{ sum=sum+(Math.pow(x,y)/y);
y++;
}
Sopln(“Sum is “+sum);
(a+ 1)/2 + (a+3)/4 + (a+5)/6+-------n

Sopln(“Enter the nos of terms”);


int n= sc.nextInt();
Sopln(“Enter a”);
int a=sc.nextInt();
double sum=0.0d;
int x=1; int y=2;
for( int i=1; i<=n;i++)
{ sum=sum+(double)((a+x)/y);
x=x+2; y=y+2;
}
Sopln(“Sum is “+sum);
2+5+10+17+26+------n 2+5+10+17+26+------n
1 2 3 4 5 ni 1 2 3 4 5 ni
Sopln(“Enter the nos of terms”);
Sopln(“Enter the nos of terms”);
int n=sc.nextInt(); int x=1;
int n=sc.nextInt(); int sum=0;
int sum=0; for( int i=1;i<=n;i++)
for( int i=1;i<=n;i++) { sum=sum+(x*x+1); x++; }
Sopln(“Sum is “+sum);}}
{ sum=sum+(i*i+1); }
Sopln(“Sum is “+sum);}}
Sopln(“Enter the nos of terms”);
int n=sc.nextInt();
Int sum=0;
for( int i=1;i<=n;i++)
{ sum=sum+(i*i+1);
}
Sopln(sum);
0+3+8+15+------n i*i-1
1 2 3 4
Sopln(“Enter the nos of terms”);
int n=sc.nextInt();
int sum=0;
for( int i=1;i<=n;i++)
{ sum=sum+(i*i-1);
}
Sopln(“sum is “+sum);
1+12+123+1234+-------n
Sopln(“Enter the nos of terms”);
int n=sc.nextInt();
int sum=0; int c=0;
for( int i=1;i<=n;i++)
{ sum=sum*10+i);
c=c+sum;
}
Sopln(c);
1+11+111+1111+-----n
int n=sc.nextInt();
int sum=0; int c=0;
for( int i=1;i<=n;i++)
{ sum=sum*10+1;
c=c+sum;
}
Sopln(c);
1,4,9,16,25…….n
int n=sc.nextInt();
for( int i=1;i<=n;i++)
{ Sop(i*i)+”,”; }
1,8,27,64,125…..n
int n=sc.nextInt();
for( int i=1;i<=n;i++)
{ Sop(i*i*i); }
0,7,26,63,124…….n
int n=sc.nextInt();
for( int i=1;i<=n;i++)
{ Sop(i*i*i-1); }
0,3,8,15,24…..n
int n=sc.nextInt();
for( int i=1;i<=n;i++)
{ Sop(i*i -1); }
1+(1+2)+(1+2+3)+……n
1 3 6
int n=sc.nextInt(); I i<=4 sum+I
1 1<=4 0+1=1
t+sum
0+1=1

int sum=0; int T=0; 2 2<=4 1+2=3


3 3<=4 3+3=6
1+3=4
4+6=10

for( int i=1;i<=n;i++)


{ sum=sum+i;
T=T+sum
} sopln(T);
1+(1*2)+(1*2*3)+……n
int n=sc.nextInt();
int sum=0;
int mult=1;
for( int i=1;i<=n;i++)
{ mult=mult*i;
sum=sum+mult;
} sopln(sum);
(1+3)+(1+3+5)+------n
int n=sc.nextInt();
int sum=0; int x=1;
int t=0;
for( int i=1;i<=n;i++)
{ sum=sum+x;
t=t+sum;
x=x+2;}
Sopln(sum)
Practice Series
1. x1+x2+x3+-------xn
2. 1/a2+ 4/a5+7/a8 +10/a11+-----n
3. a/2+a/3+a/4+------a/n
4. a+a2/2+a3/3+------an/n
5. a/4+ a/4+a/8+------n
6. 1-a/2+3-a/4+5-a/6+-----n
7. a-a3/5+a5/9+a7/13+-----n
8. 1/2-2/3+3/4-4/5+-----n
9. 2-4+6-8+----20
10.x/2+x/5+x/8+-----x/20
11.9+99+8+89+7-------n
12.1+3/4+5/9+7/16----n
13.1/12+1/22+1/32+----n
14.0,1,1,2,3,5,8…..m
15.1,3,7,15,31,63---------n

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy