0% found this document useful (0 votes)
46 views12 pages

For Loop

The document discusses for loops in C programming. It explains that for loops repeat a block of code until a specified condition is met, and C has three types of loops: for, while, and do-while. It provides the syntax for a for loop which includes an initialization statement, test expression, and update statement. The initialization statement runs once, then the test expression is evaluated - if false, the loop ends, if true, the body runs and the update statement updates before re-evaluating the test expression. This repeats until the test expression is false. An example for loop is provided to print numbers from 1 to 10.

Uploaded by

Lei Casiple
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)
46 views12 pages

For Loop

The document discusses for loops in C programming. It explains that for loops repeat a block of code until a specified condition is met, and C has three types of loops: for, while, and do-while. It provides the syntax for a for loop which includes an initialization statement, test expression, and update statement. The initialization statement runs once, then the test expression is evaluated - if false, the loop ends, if true, the body runs and the update statement updates before re-evaluating the test expression. This repeats until the test expression is false. An example for loop is provided to print numbers from 1 to 10.

Uploaded by

Lei Casiple
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/ 12

for Loop

 In programming, loops are used to repeat a block of code until a


specified condition is met.
 C programming has three types of loops:
 for loop
 while loop
 do...while loop
 The syntax of the for loop is:

for (initializationStatement; testExpression; updateStatement)


{
// statements inside the body of loop
}
 The initialization statement is executed only once.
 Then, the test expression is evaluated. If the test expression is evaluated to
false, the for loop is terminated.
 However, if the test expression is evaluated to true, statements inside the
body of for loop are executed, and the update expression is updated.
 Again the test expression is evaluated.

This process goes on until the test expression is false. When the test
expression is false, the loop terminates.
# Print numbers from 1 to 10
#include <stdio.h>
int main() {
int i;
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
The following program will ask
the user to input 5 numbers and
print out the maximum and
minimum numbers from the set.
1. Write a program in C to display the n terms of odd natural number and their sum .
Test Data

Input number of terms : 10

Expected Output :

The odd numbers are :1 3 5 7 9 11 13 15 17 19


The Sum of odd Natural Number up to 10 terms : 100
Write a c program to check whether a given number is a perfect number or not.
In number theory, a perfect number is a positive integer that is equal to the sum of its
positive divisors, excluding the number itself. For instance, 6 has divisors 1, 2 and 3
(excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect number.

Test Data :
Input the number : 56
Expected Output :
The positive divisor : 1 2 4 7 8 14 28
The sum of the divisor is : 64
So, the number is not perfect.

You might also like