Thursday, February 5, 2015

Nested Loop

Input:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b, i;

    scanf("%d%d", &a,&b);

    for (i=a; i<=b;i++)
        printf("%d",i);

      printf("\n" );


    return 0;
}


Output:

1  5
1 2 3 4 5

Loop in another loop (nested loop)
input:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b, i,j;

    for (j=1; j<=5; j++)
    {
         scanf("%d%d", &a,&b);

         for (i=a; i<=b;i++)
                printf("%d",i);

         printf("\n" );
    }



    return 0;
}


Output:

 1  5
 1 2 3 4 5
4 6 
 4 5 6
6  9
6 7 8 9 
8 10 
8  9 10
1 3 
1 2 3

Nested loop using while:

input:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b, i,j;

    for (j=1; j<=5; j++)
    {
         scanf("%d%d", &a,&b);

         while (a<=b)
                printf("%d ",a++);

         printf("\n" );
    }



    return 0;
}
 output:
 1  5
 1 2 3 4 5
4 6 
 4 5 6
6  9
6 7 8 9 
8 10 
8  9 10
1 3 
1 2 3

Tuesday, February 3, 2015

All Loop At a glance

First we see for loop:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a = 5;
    int i;

    for (i=1;i<=5;i++)
    {
        scanf  ("%d",&a);
        printf ("%d\n\n",a);

    }


    return 0;
}


Output:
1
1

3
3

5
5

88
88

0
0

After printing 5 times the loop will be terminated as per condition (i<=5)

We may writer the for loop like this way :

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a = 5;
    int i= 1;

    for (;i<=5;)
    {
        scanf  ("%d",&a);
        printf ("%d\n\n",a);
        i++;

    }


    return 0;
}

Output:

1
1

3
3

5
5

88
88

0
0
 

Note: Important issue, if we remove the condition then the program me will never stop.

While LOOP:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a = 5;
    int i= 1;

    while(i<=5)
    {
        scanf  ("%d",&a);
        printf ("%d\n\n",a);
        i++;

    }

    return 0;
}

         
Output:
                 
1
1

3
3

5
5

88
88

0
0

When use what type of loop:

For loop: when we know how many times the loop run.
while loop: when we do not know how many times the loop run.

while loop example:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a = 5;
    int i= 1;
    scanf ("%d",&a);

    while(a!= 0)
    {
        printf ("%d\n\n",a);
        scanf("%d",&a);

    }


    return 0;
}

 Output:

This programme will not terminate untill you type 0 as per the  condition (a!= 0)

Another advance way to write this while loop:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a = 5;
    int i= 1;

    while(scanf ("%d",&a)&& a!= 0)
    {
        printf ("%d\n\n",a);
        scanf("%d",&a);

    }


    return 0;
}


Output: Same until type 0 , the programme will not terminate.

Monday, February 2, 2015

Do loop and comparison with goto statement

Do loop is same as other loop. Do loop works with only one condition. One feature is, do loop do minimum one time of the work it supposed to do.

Input :

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;

  ////  i=1;

 ///   do
 ////   {
 ////       printf ("%d",i);
   ////     i++;
  ////  }
 ////   while (i<=5);


  i=1;

  start:
      printf ("%d",i);
      i++;

      if (i<=5)
        goto start;

      return 0;

}


OutPut:L
12345

goto statement and while loop

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    i = 1;

    start:
        printf ("%d",i);
        i++;

        if (i<=5)
            goto start;
  ////  while (i<=5)
 ////   {
 ////       printf ("%d",i);
 ////       i++;

 //// }
    return 0;
}


output:
12345

While loop

Input :

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    i = 1;

    while (i<=5)
    {
        printf ("%d",i);
        i++;

    }
    return 0;
}


Output :

12345

Sunday, February 1, 2015

For loop and difference between goto and for loop

For loop and goto statement works in a same way. So actually there is no difference between them. Here will see the probe of this.

Input:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;

    for (i=1; i<=5; i++)
    {
        printf ("%d",i);

    }
    return 0;
}



Output:

12345

Difference: no 

Input

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;

    i=1;
    start:
        printf ("%d",i);
        i++;

        if (i<=5)
            goto start;

 ////   for (i=1; i<=5; i++)
 ////   {
////        printf ("%d",i);
////
 ////   }
    return 0;
}



Output:

12345

Saturday, January 31, 2015

Loop start (goto statement)

Loop is a very important topic on c programming. It is said that if one does not understand loop clearly then he must not understand the next topics on c. So try to understand the loop hardly.

input:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a = 1;

    start:
        printf("%d",a);
        a++;

        if (a<=7)
            goto start;

        return 0;
}

Output :

1234567