Program 1:-Print the number from 1 to 5.
Here printf()-is used to print the value which is under the double quote.
clrscr()- is used to clear the screen.
getch()- is used to hold the screen.
void main() - It is the main function and void main() function does not return any value.
Console Window -It is the screen where we see output of the program.
stdio.h -It is a header file that contain printf() function.
conio.h -It is also a header file that contain clrscr(),getch() function.
\n - It is used to change the line. // -It is used for single line comment. Comment is not visible on the console window.
In this program, we have to print the number from 1 to 5. This is only five number, so we can repeat the printf function. If we have to print the number from 1 to 2000, we need to repeat function many times and this process becomes complex. This problem is solved by using loop.
What is loop in C language ?
Loop in C language is used to iterate the statements or a part of the program several times.
Types of loop in C
- For loop
- While loop
- Do-While loop
For loop
for(initialization;condition;increment/decrement)
{// body of for loop
}
Program 2:-Print the number from 1 to 20.
- Initialization execute once in a life cycle of for loop.
- for is keyword.
- The statement/statements within the body of for loop will execute till the condition of for satisfied (True).
Program 3:-
In program No.3,line No.5 int i=2 here we assign the value of i = 2.
But in line no.7 for (i=0;i<10;i++), here i=0 replace the value of int i=2 of line no. 5
line no.15 printf value is 10
Can we increment by 2 in a for loop?
Yes, we can increment by any number.To increment by 2 in a for loop, use the “i += 2” as a final or incremental expression statement.
0 Comments