Lab 08 Loop
Lab 08 Loop
Lab 08
Repetition Structures I
In this lab, we will learn about repetition control structures in C using the for and while loops. The following
examples will help you remember the syntax.
Example 1: Print all the numbers from 10 to 20 Example 2: Print all the numbers from 10 to 20
inclusive using the while loop inclusive using the for loop
#include <stdio.h> #include <stdio.h>
int main(void) int main(void)
{ {
int a = 10; int a;
Example 3: Print all the numbers from a to b inclusive Example 4: Print all the numbers from a to b inclusive
using the while loop using the for loop
#include <stdio.h> #include <stdio.h>
int main(void) int main(void)
{ {
int a, b; int a, b;
printf(“Enter start> ”); printf(“Enter start> ”);
scanf(“%d”, &a); scanf(“%d”, &a);
printf(“Enter end> ”); printf(“Enter end> ”);
scanf(“%d”, &b); scanf(“%d”, &b);
Task 1: Write a program in C to read 10 numbers from keyboard and print their sum and average.
Test Data:
Input:
Input the 10 numbers:
Number-1: 2
...
Number-10: 2
Expected Output:
The sum of 10 no is 55
The Average is 5.500000
Task 2: Write a program in C to display the n terms of square natural number and their sum.
1 4 9 16 ... n Terms
Test Data:
Intput:
Input the number of terms: 5
Expected Output:
The square natural up to 5 terms are 1 4 9 16 25
The Sum of Square Natural Number up to 5 terms = 55
Test Data:
Intput:
Input number of terms to display: 10
Expected Output:
Here is the Fibonacci series up to 10 terms:
0 1 1 2 3 5 8 13 21 34