0% found this document useful (0 votes)
60 views

Lab 08 Loop

This document describes Lab 08 which covers repetition structures like for and while loops in C programming language. It provides basic syntax of loops including initialization, condition, and update expression. It gives examples to print numbers from 10 to 20 using while and for loops. It also shows how to print numbers between user defined range a and b using while and for loops. The document lists tasks to write programs to find sum and average of 10 numbers, display square of natural numbers and their sum, and print Fibonacci series for given number of terms.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Lab 08 Loop

This document describes Lab 08 which covers repetition structures like for and while loops in C programming language. It provides basic syntax of loops including initialization, condition, and update expression. It gives examples to print numbers from 10 to 20 using while and for loops. It also shows how to print numbers between user defined range a and b using while and for loops. The document lists tasks to write programs to find sum and average of 10 numbers, display square of natural numbers and their sum, and print Fibonacci series for given number of terms.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CSE115L – Programming Language I Lab

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.

Basic syntax of the loops


initialization
while (condition)
{
loop body
update expression
}

for(initialization; condition; update expression)


{
loop body
}

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;

while(a <= 20) for(a = 10; a <= 20; a++)


{ {
printf("%d\n", a); printf("%d\n", a);
a++; }
}
return 0;
return 0; }
}

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);

while(a <= b) for( ; a <= b; a++)


{ {
printf("%d\n", a); printf("%d\n", a);
a++; }
}
return 0;
return 0; }
}
Perform the following tasks.

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

Task 3: Write a program in C to display the first n terms of Fibonacci series.


Fibonacci series 0 1 2 3 5 8 13 .....

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

You might also like