Repetition Structures: CSE115: Computing Concepts
Repetition Structures: CSE115: Computing Concepts
Repetition Structures
Example
Calculate the sum of the following series ( and
are user inputs).
Example
Calculate the sum of the following series ( and
are user inputs).
#include <stdio.h>
int main()
{
int x, m, term = 1, sum = 1;
scanf("%d%d", &x, &m);
for(i=1; i<=m; i++)
{
term = term * x;
sum = sum + term;
}
printf("Sum = %lf", sum);
return 0;
}
Example
Calculate the sum of the following series ( is
user input).
Example
The continue
statement forces
next iteration of the
loop, skipping any
remaining
statements in the
loop
Enter a number:
1
1 is added
Enter a number:
2
2 is added
Enter a number:
3
3 is added
Enter a number:
-4
Enter a number:
-5
Enter a number:
6
6 is added
Total = 12
Nested Loop
What is the output of the following program?
for (i=1; i<=5; i++)
{
for (j=1; j<=4; j++)
{
printf("*");
}
printf("\n");
}
Nested Loop
What is the output of the following program?
Output
****
****
****
****
****
Nested Loop
What is the output of the following program?
for (i=1; i<=5; i++)
{
for (j=1; j<=i; j++)
{
printf("*");
}
printf("\n");
}
Nested Loop
What is the output of the following program?
Output
*
**
***
****
*****
Nested Loop
Write a program that generates the following
pattern.
Output
*
++
***
++++
*****
Nested Loop
Write a program that generates the following
pattern.
Output
int i, j;
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
{
if (i % 2 == 0)
printf("+");
else
printf("*");
}
printf("\n");
}
*
++
***
++++
*****
Nested Loop
Write a program that generates the following
pattern.
Output
*
**
***
****
*****
Nested Loop
Write a program that generates the following
pattern.
Output
*
**
***
****
*****
Home-works
Calculate the sum of the following series,
1.
where is provided as user input.
2. Write a program that calculates the factorial
of a positive integer n provided as user input.
3. Write a program that calculates , where and
are provided as user inputs.
4. Calculate the sum of the following series,
where and is provided as user input.
Home-works
Write programs that generate the following
patterns. In each case, number of lines is the
input *****
*
*********
****
***
**
*
**********
**** ****
***
***
**
**
*
*
**
**
***
***
**** ****
**********
***
*****
*******
*********
*
* *
*
*
*
*
*
*
* *
*
*******
*****
***
*
*
**
***
****
*****
****
***
**
*