How to convert a while loop to a for loop in c

The basic structure of for loop is as following

for(initialization; condition; adjustment){
        statement(s);
      }

So the fo loop has four parts,

  1. Initialization: We need to assign a value for the control variable.
  2. condition: We have to use a conditional expression here. The compiler execute/iterate the for loop until it get 0 or false
  3. adjustment: Adjust the control variable (by increment or decrement ) so that the condition get 0 or false
  4. statement(s): Any type of simple or compound statement(s)

The basic structure of while loop is as following

while(condition){
        statement(s);
      }

We can see that the while loop has only two parts

  1. Condition
  2. statement(s)

So if we wish to use the while loop as the for loop we need to construct it as the following

initialization;
while( condition){
        statement(s);
      adjustment;
       }

Now lets see a program using both for loop and while loop. See the factorial finding program using both for loop and while loop

#include<stdio.h>
int main(void){

int count;

printf("Using for loop\n\n");
for(count = 1;count<=5; count++){
        printf(" \t count is %d\n",count);
        }


printf("\n Using for loop\n\n");
count = 1;
while(count<=5 ){
        printf("\t count is %d\n",count);
        count++;
        }

return 0;
}


Difference between while loop and if statement

The basic structure of while loop is as following

while(condition){
        statement;
      }

The basic structure of if statement is as following

if(condition){
        statement;
      }

So, seeing their structure, it seems that they are same. But no,,, they are not same. if the condition of if is true, the if statement will execute. Otherwise not. Once the condition is true it will execute and will iterate. Where as the while loop will iterate until it get a false or '0'

#include <stdio.h>
int main()
{
    char ch;
    printf("Press any key without N/n to continue the program\n");
    ch = getch();
    while((ch != 'N') && (ch != 'n')){
        printf("\a");
        ch = getch();
    }
    return 0;

}

The above program will not stop till you pressed N or n. As soon as you pressed the N or n the condition will false and the while stop iterating

#include <stdio.h>
int main()
{
    char ch;
    printf("Press n\n");
    ch = getch();
    if(ch == 'n'){
        printf("\a You pressed n\n");
        ch = getch();
    }
    printf("Then you pressed %c\n",ch);
    return 0;

}

To understand better, do as I state, After executing the program, press n, the condition will true, so the statement, You pressed n will show on display. Again press n, that means again the condition is true, but it will not iterate and execute the last statement Then you pressed ...


Factorial program in c using for loop, while loop and function.

Wikipedia define factorial as following

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Math is Fun gives the following definition

The factorial function (symbol: !) means to multiply a series of descending natural numbers.

Example
4! = 4 × 3 × 2 × 1 = 24
7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040
1! = 1 0! = 1, because the following reason.

We can rearrange one thing only one way, as 0 is also an entity so we can rearrange it in one way.
Also we can prove it mathematically with the help of Combination
Let us consider n is the number of different people, if we choose a team consisting with n people then we can only choose it one time, so

Now lets start coding using for loop

#include<stdio.h>
int main(){

    int n,i;
    float fact=1;
    printf("Enter your n\n");
    scanf("%d",&n);
    
    if(n <0 ) printf("\aPlease enter a positive number\n");
    else if((n == 1) || (n == 0)){
        printf("\aThe factorial is 1\n");
    }
   else{
            for(i=1;i<=n;i++){
            fact =fact*i;
            }
        printf("\aThe factorial is %.2f\n",fact);
       }

    return 0;
}

Using while loop

#include<stdio.h>

int main(){

    int n,i=1;
    float fact=1;
    printf("Enter your n\n");
    scanf("%d",&n);
    if(n <0 ) printf("\aPlease enter a positive number\n");
    else if((n == 1) || (n == 0)){
        printf("\aThe factorial is 1\n");
    }
   else{
           while(i<=n){
            fact =fact*i;
                i++;
            }
          printf("\aThe factorial is %.2f\n",fact);
       }

    return 0;
}

Using user defined function

#include<stdio.h>

float factorial( int n){
    if((n == 1) || (n == 0) ) return 1;
    else return n*factorial(n-1);
}

int main(){

    int n;
    printf("Enter your n\n");
    scanf("%d",&n);
    if(n <0 ) printf("\aPlease enter a positive number\n");
    else  printf("\aThe factorial is %.2f\n",factorial(n));

    return 0;
}