• “Counter Controlled Repetation” looping in C language using ‘while’ statement and counter

    by  • 3 March, 2013 • C programming, Education • 7 Comments

    Hello my friends. To get to know what is “counter controlled repetition” we should first get to know the terms related to it.

    What is a counter?

    A counter is a counting mechanism. A counter is a variable to which a value is set  and used along with while statement or other conditional  statement. Unless and until the condition remains true the value of the counter keeps increasing/decreasing/changing until the condition becomes false. It is advised to  initialize the counter accordingly while declaring.

    Consider the following program:

    #include<stdio.h>

    #include<conio.h>

    void main()

    {

    int counter=1, n,product;

    printf(“The a number whose mathematical table you want: “);

    scanf(“%d”,&n);

    printf(“\nThe table generated is given below.”);

    while(counter<=10)

         {

         product=n*counter;

         printf(“%d * %d = %d\n”,n,counter,product);

         counter=counter+1;

        }

    getch();

    }

    Here, while statement is a control statement. It has been used to check the condition weather counter is less the or equal then 10 or not(counter<=10). Until this statement is true the following block of statement will execute. This is called looping.

    Take the first loop here i.e., counter=1, then the condition while(counter<=10) will hold true and the block of code will execute and it will generate the product i.e., product=n*counter. Let us assume 5 as the value of  ’n’ given as input to generate the table of 5. Then the first loop execution of code will be this 5*1 = 5(since, n(5)*counter(1) will give 5 which will assigned to product). And the output of the first loop execution will be like this:

    5*1 = 5

    Also there is one more statement in the block – counter=counter+1. It is evident from the statement that it has been made to increment the value of the counter by 1. So, at the end of the execution of the first block the value of the counter will increase and the value of counter will be 2 now. It will again go to while statement and check the condition. If the while condition will hold true it will keep executing the block of code until the condition will become false i.e., the value of counter will become 11. At this point the block will not be executed and execution control will be passed to the next block of code or the next statement in the program for execution.

    The output of the program:

    The a number whose mathematical table you want:5

    The table generated is given below.

    5*1 = 5

    5*2 = 10

    5*3 = 15

    5*4 = 20

    5*5 = 25

    5*6 = 30

    5*7 = 35

    5*8 = 40

    5*9 = 45

    5*10 = 50

     

    • Twitter
    • del.icio.us
    • Digg
    • Facebook
    • Technorati
    • Reddit
    • Yahoo Buzz
    • StumbleUpon

    About

    Hello everyone, My name is Amit Kumar. I am a student of SVIT collage, secunderabad. Right now I am pursuing my B.Tech III year-II sem in CSE branch . I am a website designer also and like to work in the field of website creation. Besides this I was also an active member of Scout and Guide movement in my school and achieved Rajya Puruskar certificate . Except for this I was also the house captain in my school. I like adventurous sports, boxing and football.

    http://www.cagsworld.com

    7 Responses to “Counter Controlled Repetation” looping in C language using ‘while’ statement and counter

    1. 29 March, 2013 at 2:03 am

      Hi! I know this is kinda off topic but I was wondering which blog platform are you using for this site? I’m getting sick and tired of WordPress because I’ve had issues with hackers and I’m looking at options for another platform. I would be fantastic if you could point me in the direction of a good platform.

      • Joe Sanders
        8 April, 2013 at 12:46 am

        WordPress is a good platform. Try to activate plugins which are required. Follow all safty measures…Good luck.

    2. 26 April, 2013 at 9:13 am

      Its such as you read my thoughts! You seem to understand so much approximately this, such as you wrote
      the e-book in it or something. I believe that you just could do with a few % to power the message house a little bit, however other than that, this is great blog. A fantastic read. I will definitely be back.

    3. 27 April, 2013 at 3:21 am

      Thanks for putting in the time to give us this particular style of nfo. You did a fantastically good assignment you also seriously really helped me out. Reg Zooka

    4. Pingback: Mikal Miko

    5. Pingback: gurtredlop

    6. Pingback: anonyomous

    Leave a Reply

    Your email address will not be published. Required fields are marked *


    *Have a nice day.