Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Introduction:

In C programming, loops are control structures that allow you to execute a block of code
repeatedly as long as a specified condition is true. They are fundamental for tasks that require
repetitive execution, such as iterating through arrays, processing data, and implementing
algorithms. There are three main types of loops in C: the ‘for’ loop, the ‘while’ loop, and the
‘do-while’ loop.
1. For Loop:
The ‘for’ loop is commonly used when you know the number of iterations in advance. It
consists of three parts enclosed in parentheses: initialization, condition, and update. The
syntax is as follows:
for (initialization; condition; update) {
// code to be executed repeatedly
}
For loop works in these steps:
• Initialization: It initializes the loop control variable.
• Condition: It specifies the condition for the loop to continue. If this condition evaluates
to true, the loop continues; otherwise, it terminates.
• Update: It updates the loop control variable after each iteration.

2. While Loop:
The ‘while’ loop is used when you don't know the number of iterations beforehand. It
continues to execute the block of code as long as the specified condition is true.
while (condition) {
// code to be executed repeatedly
}
Here, `condition` is any expression that evaluates to true or false. If the condition evaluates to
true, the loop continues; otherwise, it terminates.

3. Do-While Loop:
The ‘do-while’ loop is similar to the ‘while’ loop, but the condition is evaluated after the
block of code is executed. This ensures that the block of code executes at least once,
regardless of the condition.
do {
// code to be executed repeatedly
} while (condition);
Loops can be nested within one another, and they provide mechanisms for iterating over
arrays, processing input until a certain condition is met, and implementing various algorithms
like sorting and searching. They are essential for writing efficient and concise code in C
programming.

Simulation:
1. printing all odd and even numbers between 1 to 2, counting the numbers of odds and
making a sum.

Output:

2. sum the squares of all the numbers between 1 to 5.


Output:

3. Program to show table of first four powers of numbers 1 to 9.

Output:

4.Write a program to display Fibonacci series of last term up to 1000.

Output:
5. Write a program to display Fibonacci series of last term up to 1000 (using for loop).

Output:

6.Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user.

Output:

7. Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user (using While).
Output:

8. Write a program to calculate the value of sin function using Maclaurin series up to n term
where n is taken as an input from keyboard.

Output:

9. Write a program to calculate the value of cos function using Maclaurin series up to n term
where n is taken as an input from keyboard.

Output:
10.Write a program to calculate the value of log function using Maclaurin series up to n term
where n is taken as an input from keyboard.

Output:

11. Write a program to convert an integer into a roman numeral.


Output:

12. Write a program to convert an integer into a written number.

Output:

13. Write a program to display the following:


Output:

14. Write a program to display the following:

Output:

15. Write a program to display the following:


Output:

You might also like