Tag: c loop programs

  • C Program to Reverse a Number using for loop

    In this tutorial, we will write a reverse number in C using for loop. Before that, you may go through the following topics in C.


    C program to reverse a number using for loop

    This is a reverse number program in C using for loop where the program takes the input from the user. And then iterating through for loop, the program calculates the reverse of a number and then finally displays the result.

    #include <stdio.h>
    
    int main()
    {
      int num, rev = 0, rem;
    
      //User Input
      printf("Enter the Number: ");
      scanf("%d", &num);
    
      for (; num != 0; num = num / 10)
      {
        rem = num % 10;
        rev = rev *10 + rem;
      }
    
      printf("Reversed number: %d", rev);
      return 0;
    }

    Output:

    Enter the Number: 1234
    Reversed number: 4321

    As you can see the initialization part in for loop is left empty and only the condition checking and increment/decrement is filled within “()” in reverse number in c using for loop.


  • C – do while loop

    do-while loop in C is just as same as while loop, the only difference is that in do-while the condition is always executed after the loop as shown in the syntax below.

    Unlike while loop, where the body of the loop is executed only if the condition stated is true but in the do-while loop, the body of the loop is executed at least once. do-while is an exit-control loop that is it checks the condition after the first execution of the block.

    do-while Flowchart:

    do-while loop in C

    Syntax of do-while Loop.

    do
     {
      statements..
     }while (condition);

    Example of a do-while loop in C program.

    #include <stdio.h>
    
    int main()
    {
      int num = 1;
    
     	//execution of do-while loop
      do {
        printf("Value of num: %d\n", num);
        num++;
      } while (num <= 10);	//condition
    
      return 0;
    }

    The output of do-while loop.

    Value of num: 1
    Value of num: 2
    Value of num: 3
    Value of num: 4
    Value of num: 5
    Value of num: 6
    Value of num: 7
    Value of num: 8
    Value of num: 9
    Value of num: 10


  • C – while Loop

    A while loop is a straightforward loop and is also an entry-control loop. It evaluates the condition before executing the block of the loop. If the condition is found to be true, only then the body of the loop is executed. The loop continues until the condition stated is found to be false.

    while loop Flowchart:

    While Loop in C

    Syntax of While Loop.

     while(condition)
      {  
        //block of code to be executed  
      } 

    Example of while loop in C Program

    #include <stdio.h>
    
    int main()
    {
     	//varibale
      int num = 1;
    
     	//execution of while loop
      while (num < 10)
      {
        printf("The value of num: %d\n", num);
        num++;
      }
    
      return 0;
    }

    The output of while loop in C programming.

    The value of num: 1
    The value of num: 2
    The value of num: 3
    The value of num: 4
    The value of num: 5
    The value of num: 6
    The value of num: 7
    The value of num: 8
    The value of num: 9


  • C – for loop

    In C programming, for loop is a more efficient loop structure and is an entry-control loop. The iteration continues until the stated condition becomes false.

    It has three computing steps as shown in the syntax below.

    • initialization: The first step is the initialization of the variable and is executed only once. And need to end with a semicolon(;).
    • condition: Second is condition check, it checks for a boolean expression. If true then enter the block and if false exit the loop. And need to end with a semicolon(;).
    • Increment or Decrement: The third one is increment or decrement of the variable for the next iteration. Here, we need to use the semicolon(;).

    for loop Flowchart:

    for loop in C

    Syntax of for loop.

     for(initialization; condition; Increment or Decrement) 
     {
      // Statements
     } 

    Example of for loop in C Program.

    #include <stdio.h>
    
    int main()
    {
      int num;
    
     //execution of for loop
      for (num = 5; num <= 10; num++)
      {
        printf("The values of num: %d\n", num);
      }
    
      return 0;
    }

    The output of for loop in C programming.

    The values of num: 5
    The values of num: 6
    The values of num: 7
    The values of num: 8
    The values of num: 9
    The values of num: 10


  • C – switch statement

    A switch statement allows a variable to be tested for equality against multiple values and each of those values is called a case. It can be used instead of nested if...else.

    Switch expression and case value must be of the same type. There must be at least one case or multiple cases with unique case values. In the end, it can have a default case which is optional that is executed if no cases are matched.

    Syntax

    Syntax of switch statement in C:

    switch (expression)
    {
        case value1:    
        //code to be executed;    
        break;  //optional  
        case value2:    
        //code to be executed;    
        break;  //optional  
        .
        .
        .
        .    
        case valueN:    
        //code to be executed;    
        break;  //optional 
            
        default:     
        code to be executed if all cases are not matched;    
      }

    Switch statement Flowchart:

    switch statement

    Example of switch statement in C.

    #include <stdio.h>
    
    int main()
    {
      //local variable definition
      char grade = 'D';
    
      switch (grade)
      {
        case 'A':
          printf("Excellent!\n");
          break;
        case 'B':
          printf("Very Good!\n");
          break;
        case 'C':
          printf("Well done\n");
          break;
        case 'D':
          printf("GRADE D\n");
          break;
        case 'F':
          printf("Better try again\n");
          break;
        default:
          printf("Invalid grade\n");
      }
    
      printf("You Pass with Grade: %c\n", grade);
    
      return 0;
    }

    The output of the switch statement in C.

    GRADE D
    You Pass with Grade: D


  • C – nested switch statement

    The use of switch statement inside another switch statement is called nested switch statement.

    Syntax

    Syntax of nested switch statement in C:

    switch(ch1) {
    
       case 'A': 
          printf("This A is part of outer switch" );
          
          //use of another switch statement
          switch(ch2) {
             case 'A':
                printf("This A is part of inner switch" );
                break;
             case 'B': /* case code */
          }
    	  
          break;
       case 'B': /* case code */
    }

    Example of nested switch statements in C:

    #include <stdio.h>
    
    int main()
    {
      //local variable
      int a = 10;
      int b = 20;
    
      switch (a)
      {
        case 10:
          printf("The OUTER Switch.\n", a);
    
          //INNER Switch
          switch (b)
          {
            case 20:
              printf("The INNER Switch.\n", a);
          }
      }
    
      return 0;
    }

    The output of nested switch statements in C.

    The OUTER Switch.
    The INNER Switch.


  • C – if statement

    An if statement consists of a Boolean expression followed by one or more statements.
    If the boolean expression is true, the block of code inside the if statement will be executed else not.

    Syntax

    Syntax of if statement in C:

     if (boolean expression)
      {
       /* if expression is true */
       statements... ; /* Execute statements */
      }

    if statement Flowchart:

    if statement

    Example of if statement in C Program:

    #include <stdio.h>
    
    void main()
    {
      int num1, num2;
    
      num1 = 90;
      num2 = 50;
    
      if (num1 > num2)
      {
        printf("num1 is greater than num2");
      }
    }

    The output of if statement in C.

    num1 is greater than num2


  • C – if…else statement

    If the Boolean expression is true then the code inside the if statement block is executed or if it is false then the code inside else statement will be executed. Hence if..else statement.

    Syntax

    Syntax of if...else statement in C Language:

     if (condition) 
     {
      //code executed if condition true 
     }
     else
     {
      //code executed if condition false 
     }

    If…else Flowchart:

    Example of if…else statement in C.

    #include <stdio.h>
    
    void main()
    {
      int num1, num2;
    
      num1 = 56;
      num2 = 89;
    
      if (num1 > num2)
      {
        printf("num1 is greater than num2y");
      }
      else
      {
        printf("num2 is greater than num1");
      }
    }

    The output of if…else statement in C.

    num2 is greater than num1


  • C – nested if statements

    This statement allows the user to use if block inside the other if block. And the inner if statement is executed only if the outer if statement’s condition is true.

    Syntax

    Syntax of nested if statement in C:

    if(condition1)
     {    
        //block of code to be executed    
        if(condition2)
        {  
          //block of code to be executed    
        }    
     } 

    nested if Flowchart:

    nested if

    Example of nested if statement in C:

    #include <stdio.h>
    
    int main()
    {
      // local variable
      int num1 = 60;
      int num2 = 90;
    
      // first if statement
      if (num1 > 50)
      {
        // if inside if statement
        if (num2 > 80)
        {
          printf("Actual value of num1 is : %d\n", num1);
          printf("Actual value of num2 is : %d\n", num2);
        }
      }
    
      return 0;
    }

    The output of nested if statement in C.

    Actual value of num1 is : 60
    Actual value of num2 is : 90