Code Scrappers

for Loop

The for loop is the most popular looping instruction.

Syntax :

for (initialization;condition; updatation)

 {

       statements;

       statements;

       statements;

  }

The for allows us to specify 3 things about a loop in a single line:
(a)Setting up of a loop counter to an initial value.
(b)Setting up a condition to determine if its value has reached the number of repetitions desired.
(c) Updating the value of loop counter either by incrementing or decrementing.

Consider the below example

#include<stdio.h>

void main()

{

  int n;

  printf("number: cube of number\n");

  for (n = 1; n <= 6; n++)
  {

      printf("%5d: %5d\n", n, n*n*n);
  }

}

The program prints the integers 1 through 6 and their cubes.

number: cube of number
1: 1
2: 8
3: 27
4: 64
5: 125
6: 216

NOTE 1:The first line of the for loop tells us immediately all the information about the loop parameters:

The starting value of num

The final value of num

The amount that num increases on each looping

2.Write a C program to print the sum of 1st N natural numbers using for loop

#include<stdio.h>

void main()

{

 int i,num;

 int sum=0;

 printf("Enter the limit of number:\n ");

 scanf("%d", &num);

 for(i=1;i<=num;i++)

  {

     sum = sum +i;

  }

 printf("The summation of n natural numbers is :%d", sum);

}

Output: Enter the limit of number:
5
The summation of n natural numbers is :15.

2.Write a C program to find the reverse of a number using for loop


#include<stdio.h>

void main()

{

  int n,remainder;
  int rev=0;

  printf("Enter the number: ");

  scanf("%d",&n);

  for(;n!=0;n=n/10)

  {

      remainder=n%10;

      rev=rev*10+remainder;
  }

 printf("The reversed number is : %d\n",rev);

}

Output: Enter the number:
12
The reversed number is :
21

TOPIC 9: NESTING OF LOOPS

C programming language permits the usage of one loop inside another loop.

Nested for loop syntax :

for ( initialization; condition; updation )

 {

    for ( initialization; condition; updation)

    {

          statements....;

    }

    statements.....;
}

Syntax of Nested while loop :

while(condition)

{
    while(condition)

     {

       statements....;

     }

    statements....;
}

Syntax of do-while loop:

do

{

    statements....;

    do

    {

      statements....;

    } while(condition);

} while(condition); 

NOTE 1:A final note on loop nesting is that you can put any type of loop inside of any other type of loop.For example, a for loop can be inside a while loop or vice versa.

Assignment Operators

There are various other operators supported by c language.

Operator : ( = )

Description: It is a simple assignment operator, Assigns values from right side operands to left side operand

Example: C = A + B will store the value of A + B into C

Operator : ( += )

Description: Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand

Example:C += A is equal to C = C + A

Operator : ( -= )

Description: Subtract AND assignment operator, The right operand is subtracted from the left operand and the result is stored into left operand

Example: C -= A is equal to C = C – A

Operator : ( *= )

Description:Multiply AND assignment operator,The right operand is multiplied with the left operand and the result is stored in the left operand

Example:C *= A is equal to C = C * A

Operator : ( /= )

Description::Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand

Example:C /= A is equal to C = C / A

Operator : ( %= )

Description:Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand

Example:C %= A is equal to C = C % A

Operator : ( <<= )

Description:Left shift AND assignment operator

Example:C <<= 2 is similar to C = C << 2

Operator : ( >>= )

Description:Right shift AND assignment operator

Example:C >>= 2 is similar to C = C >> 2

Operator : ( >>= )

Description:Right shift AND assignment operator

Example:C >>= 2 is similar to C = C >> 2

Operator : ( &= )

Description:Bitwise AND assignment operator

Example:C &= 2 is similar to C = C & 2

Operator : ( ^= )

Description:bitwise exclusive OR and assignment operator

Example:C ^= 2 is similar to C = C ^ 2

Operator : ( |= )

Description:bitwise inclusive OR and assignment operator

Example:C |= 2 is similar to C = C | 2

<< Back Next >>