The for loop bhi ek entry-controlled loop hai like while loop. Ye hame aur bhi zyada concise loop control structure provide karti hai. The general form of the for loop is:
for(initialization; test-condition; increment)
{
body of the loop
}
for loop ka execution kuch aise hota hai.
- Pehle control variable ko initialize karte hai, using assignment statements such as i = 1 aur count = 0. Ye i aur count loop-control variable hote hai.
- Phir control variable test hota hai using the test-condition. The test-condition ek relational expression hoti hai, such as i < 10 ye tab determine karte hai jab loop exit hogi hai. Agar condition true hai, to loop ki body execute hogi, otherwise loop terminate ho jaygi aur program control direct us statement par jayega jo loop ke immediately baad likhi hogi.
- Jab loop ki body execute ho jayegi, the control transferred back ho jayega to for statement after evaluating the last statement in the loop. Ab control-variable increment hoga using increment statement such as i++; aur ab again new value of control-variable test hogi is cheez ko dekhne ke liye ki ye ab bhi loop ki condition ko satisfy kar raha hai ya nhi. Agar condition ab bhi satisfied hoti hai , to loop ki body again execute hogi. Ye process tab tak hoga jab tak control variable test condition ko satisfy karta hai.
Syntex :
for(int i = 0; i <= 9; i++)
{
printf("%d ",i);
}
printf("
");
Ye for loop 10 times execute hogi aur 0 to 9 numbers print karegi in same line. for loop mein three section hote hai jo ki parentheses se enclosed hote hai aur inhe separete karna zaruri hota hai using semicols. Increment section mein koi semicolon nhi hota hai.
The for statement negative increments ko bhi allow karti hai. For example,
for(int i = 9; i >= 0; i--)
printf("%d ", i);
printf("
");
Ye loop bhi 10 times execute hogi, but iska output 9 to 0 hoga instead of 0 to 9. Braces optional hote hai jab body mein single statement hoti hai.
Let solve factorial of a number problem using for loop.
Intuition :
Factorial kiya hota hai ? Factorial product hota un sabhi positive integers ka jo less then hote hai given number se. For example factorial of 5 is 1 x 2 x 3 x 4 x 5 = 120, yaha hamne zero ko include nhi kara kioki zero ka factorial 1 hota hai, isiliye ham code mein zero ko include hi nhi karenge.
Logic : Iska bahut hi basic sa logic hai, isme ham for loop ka use karenge jo 1 to n (n is the given number) tak run hogi, aur ek 'factorial = 1' variable ko manage karenge jo loop ki body mein har bar i(control variable) se multiply hokar 'factorial' variable ko hi assign ho jayega.
#include <stdio.h>
int main()
{
int num, factorial = 1;
printf("Enter the number : ");
scanf("%d", &num);
for (int i = 1; i <= num; i++)
{
factorial = factorial * i;
}
printf("Factorial of %d is %d ", num, factorial);
return 0;
}
Output:
Enter the number : 5
Factorial of 5 is 120
Explaination
- Pehle ek number user se input lenge.
-
factorial = 1
Factorial variable ko 1 se assign karenge jisse ko factorial variable koi garbage value na contain kare. -
for(int i = 1; i <= num; i++)
yahai
ko 1 se assign ki kioki agar 0 se karenge to factorial variable mein 0 assign ho jayega jisse output zero ho jayega -
factorial = factorial * i;
Ye hamare factorial ko compute karega aur har baarfactorial
variable mein hi assign ho jayega printf("Factorial of %d is %d ", num, factorial);
Ye 'num' aur 'num' ke factorial ko print kara dega.
Comments