C Tutorial – for loop, while loop, break and continue

In every programming language, thus also in the C programming language, there are circumstances were you want to do the same thing many times. For instance you want to print the same words ten times. You could type ten printf function, but it is easier to use a loop. The only thing you have to do is to setup a loop that execute the same printf function ten times.
There are three basic types of loops which are:

  • “for loop”
  • “while loop”
  • “do while loop”

The for loop

The “for loop” loops from one number to another number and increases by a specified value each time.
The “for loop” uses the following structure:


      for (Start value; continue or end condition; increase value)
     		statement;

Look at the example below:


	#include<stdio.h>

	int main()
	{
     		int i;
     		for (i = 0; i < 10; i++)
     		{
          		printf ("Hello\n");
         		printf ("World\n");
     		}
     	return 0;
	}

Note: A single instruction can be placed behind the “for loop” without the curly brackets.
Note: For those who don’t know printf or need to know more about printf format specifiers, then first a look at our printf C language tutorial.

Let’s look at the “for loop” from the example: We first start by setting the variable i to 0. This is  where we start to count. Then we say that the for loop must run if the counter i is smaller then ten. Last we say that every cycle i must be increased by one (i++).

In the example we used i++ which is the same as using i = i + 1. This is called incrementing. The instruction i++ adds 1 to i. If you want to subtract 1 from i you can use i--. It is also possible to use ++i or --i. The difference is is that with ++i (prefix incrementing) the one is added before the “for loop” tests if i < 10. With i++ (postfix incrementing) the one is added after the test i < 10. In case of a for loop this make no difference, but in while loop test it makes a difference. But before we look at a postfix and prefix increment while loop example, we first look at the while loop.

The while loop

The while loop can be used if you don’t know how many times a loop must run. Here is an example:


	#include<stdio.h>

	int main()

	{
    		int counter, howmuch;

    		scanf("%d", &howmuch);
    		counter = 0;
    		while ( counter < howmuch)
    		{
          		counter++;
          		printf("%d\n", counter);
    		}
    		return 0;
	}

Let’s take a look at the example: First you must always initialize the counter before the while loop starts ( counter = 1). Then the while loop will run if the variable counter is smaller then the variable “howmuch”. If the input is ten, then 1 through 10 will be printed on the screen. A last thing you have to remember is to increment the counter inside the loop (counter++). If you forget this the loop becomes infinitive.

As said before (after the for loop example) it makes a difference if prefix incrementing (++i) or postfix incrementing (i++) is used with while loop. Take a look at the following postfix and prefix increment while loop example:


	#include<stdio.h>

	int main(void) {
		int i;
	
		i = 0;
		while(i++ < 5) {
			printf("%d\n", i);
		}
		printf("\n");
		i = 0;
		while(++i < 5) {
			printf("%d\n", i);
		}
		return 0;
	}

The output of the postfix and prefix increment example will look like this:


	1
	2
	3
	4
	5

	1
	2
	3
	4

i++ will increment the value of i, but is using the pre-incremented value to test against < 5. That’s why we get 5 numbers.
++i will increment the value of i, but is using the incremented value to test against < 5. That’s why we get 4 numbers.

The do while loop

The “do while loop” is almost the same as the while loop. The “do while loop” has the following form:


	do
	{
    		do something;
	}
	while (expression);

Do something first and then test if we have to continue. The result is that the loop always runs once. (Because the expression test comes afterward). Take a look at an example:


	#include<stdio.h>

	int main()
	{
     		int counter, howmuch;
     		scanf("%d", &howmuch);
     		counter = 0;
     		do
     		{
          		counter++;
          		printf("%d\n", counter);
     		}
     		while ( counter < howmuch);
     		return 0;
	}

Note: There is a semi-colon behind the while line.

Break and continue

To exit a loop you can use the break statement at any time. This can be very useful if you want to stop running a loop because a condition has been met other than the loop end condition. Take a look at the following example:


	#include<stdio.h>

	int main()
	{
     		int i;

		i = 0;
		while ( i < 20 )
     		{
          		i++;
          		if ( i == 10)
               		break;
     		}
     		return 0;
	}

In the example above, the while loop will run, as long i is smaller then twenty. In the while loop there is an if statement that states that if i equals ten the while loop must stop (break).

With “continue;” it is possible to skip the rest of the commands in the current loop and start from the top again. (the loop variable must still be incremented). Take a look at the example below:


	#include<stdio.h>

	int main()
	{
		int i;

		i = 0;
     		while ( i < 20 )
     		{
          		i++;
          		continue;
          		printf("Nothing to see\n");
     		}
     		return 0;
	}

In the example above, the printf function is never called because of the “continue;”.

That was all for now. Don’t forget to make some example programs of your own, just for practice!

Update: You can also take a look at one of the following example(s) that also use for loops and while loops:

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

There are currently 92 responses to “C Tutorial – for loop, while loop, break and continue”

Why not let us know what you think by adding your own comment!

  1. shelven on December 5th, 2009:

    the examples given is good but I have to know first the program that will output , “Do you want to try again?”.
    please help me.

  2. supriya bose on December 5th, 2009:

    Write a program to print all the ASCII value using While Loop.

  3. admin on December 6th, 2009:

    Looks like a home-work question, so we won’t give you a while-loop answer, instead we give a for loop example:

    To find ASCII value of character in C, you just have to remember the following: a variable of type char is just an 8-bit int.

    #include<stdio.h>
    void main(void)
    {
    int i;
    for (i = 0; i < 128; i++)
    {
    printf("%d = %c\n", i, i);
    }
    }

    For an ASCII-table look at http://www.asciitable.com/

    You can figure out the rest yourself!

  4. virgoptrex on January 19th, 2010:

    @shelven,

    You can try something like

    do{

    /* blah blah */

    printf(“\n Do you want to continue? \n”);

    } while(getchar()!=’n’ && getchar()!=’N’); /* Continue while the response does *not* equal ‘n’ or ‘N’

  5. virgoptrex on January 19th, 2010:

    don’t forget parenthesis while using “&&” operator

    while( (getchar()!=’n’) && (getchar()!=’N’) );

  6. saurabh dubey on January 29th, 2010:

    hello sir

    i want to know how we can use two FOR loop.

    which out put must be-

    A B C D E D C B A
    A B C D D C B A
    A B C C B A
    A B B A
    A A

    please help me

  7. akhter on March 4th, 2010:

    hello
    i need this out put some one help
    here a and b are variables
    this output should be through loops.
    a=1, b=0 a=5 , b=2 ;
    a=3, b=1 a=7 , b=3 ;
    a=1, b=0 a=5 , b=2 ;
    a=3, b=1 a=7 , b=3 ;

  8. Sachin dwivedi on February 22nd, 2011:

    Perfect

  9. Teacher on March 7th, 2011:

    ********** code for Akhter*********
    enjoy but try to understand programming

    void main()
    {
    clrscr();

    int a=1,b=0,c=0;
    while (c<2)
    {
    cout<<endl;
    cout<<"a="<<a<<" ,b ="<<b<<", a="<<a+4<<", b= "<<b+4;
    a+=2;
    b+=1;
    cout<<endl;
    cout<<"a="<<a<<" ,b ="<<b<<", a="<<a+4<<", b= "<<b+4;
    c++;
    a=1;
    b=0;

    }
    getch();

    }

  10. nooruddin on April 15th, 2011:

    hi i am nooruddin i am IT teacher you are going good in programming, i like your lessons

  11. Prithvi Macherla on April 27th, 2011:

    it’s helpful :)

  12. phillip Gonzaga on May 2nd, 2011:

    you are doing well to reply our qns and to teche programming thanks

  13. Ashish on May 13th, 2011:

    This helps me to understand d concept of while,do while and for loop …
    thanx…

  14. Deepak b.l on June 28th, 2011:

    the above examples are very good

  15. shaker pamu on July 27th, 2011:

    Thank u so much sir hv is vr use ful 2 me. Iam a b.com student…

  16. sukhi on August 1st, 2011:

    thank u so much sir this is vry heplful for me am a btech student

  17. ankit on August 5th, 2011:

    any body tell me — when i am run a for loop like for(i=0;i<=10;i++)thn result is 012345678910
    i wan to know how's the 0 come;

  18. kabumski on August 14th, 2011:

    (i=0;i<=10;i++) —-
    i=0; is the start make it (i=1;
    i<=10 is the end process

  19. Nikhila on August 29th, 2011:

    reply for Ankit:

    Hi Ankit
    the loop is (i=0;i<=10;i++)
    i is the variable which stands for starting value,ending value and inc or dec.
    here u hav started with 0 only.Thts y it came 0.If u want to atrt with 1 means u should write ur loop like ths (i=1;i<=10;i++)

  20. via cals on September 6th, 2011:

    so good!

  21. kid on September 8th, 2011:

    All,

    Can any one help me to code a program using while loop, that will count the charecters,numbers, alphanumerical.

  22. alankar on September 24th, 2011:

    for ankit
    hello ankit,in the 1st step of for loop the value are intialise ok,then second test the condition, then in third incre/decrement ok
    now if initialise i=0
    then 1st it will check condition then if condition is true then it will execute the block or a single line statement.
    after that the value of i will going for incr/decrement ok
    that’s why ur output will showing like that-
    012345678910 ok

  23. tarun on November 10th, 2011:

    Hello Sir,

    i want to know that how to use for loop this statement
    *
    * A *
    * A * A *
    * A * A * A *

  24. admin on November 10th, 2011:

    @tarun: take a look at C Tutorial a star pyramid an string triangle using for loops. In that tutorial we do almost what you’re asking. You should be able to make the additional changes yourself.

  25. Rakshith kumar on November 24th, 2011:

    1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
    i want output like this
    conditions
    1)using single loop(for or while)
    2)can use maximum of 2 variables

  26. dhananjay on December 8th, 2011:

    i read break statement in book. But didnt got it. i read here and instantly got it because of your simple language. thnx

  27. plzzz help ( loops and arrays) on December 14th, 2011:

    [...] C Tutorial – for loop, while loop, break and continue | CodingUnit Programming Tutorials C Programming – Arrays Basicly : Code: [...]

  28. manas maity on January 8th, 2012:

    **decimal to binary using while loop **

    #include
    #include
    #include
    void main()
    {
    int n,s=0,r,x=0;
    clrscr();
    printf(“these programe is develope by manas maity.BCA \n”)
    printf(“\n enter number:”);
    scanf(“%d”&n);
    while(n!=0)
    {
    n=n%2;
    s=s+r*pow(10,x);
    n=n/2;
    x++;
    printf(“the number is : %d”,s);
    }
    getch();
    }

  29. manas maity on January 8th, 2012:

    **write a programe to print a pascal trangle using for loop. **

    #include
    #include

    void main()
    {
    int
    clrscr();
    printf(“these programe is develope by manas maity.BCA \n”);
    pintf(“enter number which u want: “);
    scanf(“%d”,&n);
    for(i=1;i<=n;i++)
    {
    c=1;
    for(j=0;j<=n-i;j++)
    printf("%3c",32);

    for(j=0;j<=i;j++)
    {
    printf("%6d",c);
    c=c*(i-j)/(j*1)
    }
    printf("\n");
    }
    getch();
    }

  30. it is very helpfull;;;;;;thanks on January 19th, 2012:

    It is very helpfull

  31. shalizy on February 3rd, 2012:

    this site is realy owsm…:)))

  32. shalizy on February 3rd, 2012:

    A A+2 A+4 A+6
    3 5 7 9
    6 8 10 12
    9 11 13 15
    12 I7 19 21
    how can i get this out put using loops. plz help me….:)

  33. Bharath on February 26th, 2012:

    It is very easy to understand

  34. Shane on February 29th, 2012:

    Work in progress but it only loops once… What did i enter wrong or am I missing something?

    # include
    # include
    using namespace std;
    int main()
    { //Declaring the variables needed
    double numyears;
    int count = 0;
    int month;
    string monthName[]={“January”,”Febuary”,”March”,”April”,”May”,”June”,”July”,”August”,”September”,”October”,”November”,”December”};
    int averageNum[11];

    for (int month = 0; month <= 11; month++); //Getting the averages for each month
    {
    cout << "Enter the average for " << monthName[month] <> averageNum[month];

    }
    return 0;

    }

  35. ayaz memon on March 10th, 2012:

    hello sir
    iwant to know how we can use two FOR loop.
    * * * * *
    * * * *
    * * *
    * *
    *

  36. admin on March 10th, 2012:

    @ayaz memon: Take a look at the following C language tutorial star pyramid

  37. Nivetha on March 24th, 2012:

    #include
    void main()
    {
    int num,i=0;
    do
    }
    cout<>num;
    if(num2==0){
    cout<<"\n even"<<endl;
    i++;}
    else
    cout<<"\n odd"<<endl;
    }
    while(num!=999);
    cout<<"\n number of even number="<<i;
    }

    plz help is it correct coding,,,

  38. Sazzad Hossain on March 31st, 2012:

    For Nivetha,

    What was ur question ?

    U can probably get some hints from following code :

    #include
    using namespace std;

    int main()
    {
    int num,i=0;
    do
    {
    cout<>num;
    if(num%2==0){
    cout<<"\n Number is even!! "<<endl;
    i++;
    }
    else
    cout<<"\n Number is odd !! "<<endl;
    }

    while(num<=999);

    cout<<"\n number of even number="<<i;

    return 0;
    }

  39. diraviyaa on April 8th, 2012:

    hello sir it is very usefull thanks toooooooooo

  40. Kiran on June 3rd, 2012:

    Nice but, i need understanding of working of two for loop. Awaiting for Quick Ans

  41. hawa farisha iman on June 12th, 2012:

    nice note ! thank :::

  42. 3shin on June 19th, 2012:

    can u pls help me how make a program that inputs a string of keyboard characters and outputs the characters in reverse order using for loop statement.

    Sample Output:
    Input : Syntacs College
    Output : egelloC scatnyS

  43. mahesh rathore on July 15th, 2012:

    very nice helpful

  44. saddam hussain on August 6th, 2012:

    hi this is simply superb…very nice examples … thanks for this postings ..

  45. mahesh rathore on August 17th, 2012:

    For Nivetha,

    What was ur question ?

    U can probably get some hints from following code :

    #include
    using namespace std;

    int main()
    {
    int num,i=0;
    do
    {
    coutnum;
    if(num%2==0){
    cout<<"\n Number is even!! "<<endl;
    i++;
    }
    else
    cout<<"\n Number is odd !! "<<endl;
    }

    while(num<=999);

    cout<<"\n number of even number="<<i;

    return 0;
    }

  46. Samantha on September 18th, 2012:

    How to create a function to convert kg to pound and from pound to kg.

  47. admin on September 18th, 2012:

    @Samantha: it isn’t that difficult, so try to make it yourself. That is the only way to learn.
    Here are some hints: 1 kilogram = 2.2046 pounds, hence to kilograms into pounds: pounds = 2.2 * kilograms.
    To create a program do something like this: create two floats (one for pounds and one for kilograms), get some user input using scanf (for instance), use formula pounds = 2.2 * kilograms and print the result of pounds.
    Good luck, I know you can do it with the hints above!

  48. sebastien on October 13th, 2012:

    I have a problem I did my program and my teacher wants it to exit When I press Q there is my program
    #include
    #include

    int main()
    {
    char type;
    int lenght, quantity=0.0;
    float total=0.0,Ecost,Ucost;

    while(1)
    {
    printf(“Enter store code and quantity :”);
    scanf(” %c%d%d”,&type,&lenght,&quantity);

    if (type ==’Q’)
    {

    break;

    }
    else if(type==’E’)
    {
    if(lenght==25)

    {

    Ecost= quantity *18 ;
    total= Ecost;
    printf(“%d Ethernet cables (25 foot) costs %3.2f (totalt = %5.2f)\n”,quantity,Ecost,total);
    }
    else if(lenght==50)
    {

    Ecost = quantity * 30;
    total= Ecost;
    printf(“%d Ethernet cables (50 foot) costs %3.2f (total = %5.2f) \n”,quantity,Ecost,total);
    }
    }
    if (type == ‘U’)
    {
    if(lenght==6)
    {

    Ucost=quantity*24;
    total = Ecost +Ucost;
    printf(“%d USB cables (6 foot) costs %3.2f (total = %5.2f)\n”,quantity,Ucost,total);
    }
    else if(lenght==10)
    {

    Ucost=quantity*36;
    total = Ecost+Ucost;
    printf(“%d USB cables (10 foot) costs %3.2f (total = %5.2f)”,quantity,Ucost,total);
    }

    {

    }
    }

    }

    system(“pause”);
    return 0 ;
    }

  49. Reema on October 29th, 2012:

    /*coding for below output
    *
    * A *
    * A * A *
    * A * A * A * */
    #include
    #include
    void main()
    {
    int i;,k=0;
    clrscr();
    for(i=1;i<=4;i++)
    {
    for(j=1;j<=i+k;j++)
    {
    if(j%2==0)
    printf(" A");
    else
    printf(" *");
    }
    }
    getch();
    }

  50. Jack goh on November 9th, 2012:

    Can any1 help me fix this plz…
    help me clear out Leap year. and direct return 0…

    #include
    #include

    int main()
    { int year=0;
    do{
    printf(“\n Enter year:”);
    scanf(“%d”,&year);

    if
    (((year%4==0)&&(year%100!=0))||((year%4==0)&&(year%100==0)&&(year%400==0))||(year<400))
    printf(" LeapYear.\n”,year);

    else
    printf(” Not LeapYear. \n”,year);
    }

    while(year > 0);
    return 0;

    }

  51. admin on November 10th, 2012:

    @jack goh: I’m not exactly sure what you are asking, but I assume something like this piece of code -

    #include<stdio.h>

    #define TRUE 1
    #define FALSE 0

    int main() {
    int year;

    printf(“Please enter a year (example: 1999) : “);
    scanf(“%d”, &year);

    if(year% 4 == FALSE && year%100 != FALSE || year%400 == FALSE) {
    printf(“%d is a Leapyear. \n”, year);
    } else {
    printf(“%d is NOT a Leapyear. \n”, year);
    }
    return 0;
    }

    Also see the tutorial: How to make a calendar in C language for additional information.

    I hope that this helps!

  52. FMMK on November 16th, 2012:

    (#include )

    int main ()
    {

    float Kilogram;

    scanf(“%f”, &Kilogram);

    stdin;

    float Pounds = Kilogram * 2.20046;

    printf(“%f Kilograms = %f Pounds.\n”, Kilogram, Pounds);

    float POUNDS;

    scanf(“%f”, &POUNDS);

    stdin;

    float KILOGRAMS = POUNDS / 2.20046;

    printf(“%f POUNDS = %f KILOGRAMS”, POUNDS, KILOGRAMS);

    return 0;

    }

  53. ranjan on November 20th, 2012:

    Write a program to calculate the sum of all the numbers less than a given number n.
    ex: output
    Enter number : 3
    6
    Enter number : 5
    15

  54. varshu on November 22nd, 2012:

    really its so amazing for learn….

  55. anirban on December 20th, 2012:

    it was really helpfull :)

  56. sivi on December 26th, 2012:

    nice

  57. rohan on December 31st, 2012:

    hey ..could any body write a function to solve simultaneous equations ..
    in have great difficulties with this

  58. Carlo on January 27th, 2013:

    Hello sir

    Can you please help me to output the sum of numbers from 1 to 15 (inclusive) and sum of odd numbers from 15-45 (inclusive) ?

  59. vinshade21 on January 30th, 2013:

    @Rakshith kumar:

    #include
    int main()
    {
    int i=1,j=10;
    while(j>1)
    {

    if(i<11)
    {
    printf("%d ",i);
    ++i;
    }
    else
    {
    –j;
    printf("%d ",j);
    }
    }
    }

  60. mujeeb on January 30th, 2013:

    good job sir

  61. Mohamed F. on February 24th, 2013:

    While testing on continue; i discovered it could be used to skip a certain number too, like when using this:
    i = 0;
    while ( i++ < 20 )
    {
    if ( i == 16 ) continue;
    printf("number %d\n", i );
    }
    it will skip the number 16 but will print every number in the range 1 to 20

  62. prince pandey on March 7th, 2013:

    sir this is very nice tutorial by you

  63. kundan on March 16th, 2013:

    thankyou

  64. M.Salman on March 17th, 2013:

    void main(void)
    {
    for(a=2;a<=400;a+2)
    {
    printf("%d",a);
    }

    i m not getting the series of even num help me???? output showing only 400 nd its blinking !! :S

  65. werke ayalew on March 22nd, 2013:

    #include
    void main(void)
    {
    int a;
    for(a=2;a<=400;a+2)
    {
    printf("%d",a);
    }
    }

  66. werke ayalew on March 22nd, 2013:

    your simple error is only on the declaration of ‘a’ that must be an integer.otherwise the compiler doesn’t know what variable ‘a’ is.

  67. ush on March 31st, 2013:

    it was good n easy..
    plzz tell the purpose of getch and also explain nested loops if possible..
    thankyou

  68. hemanth on April 12th, 2013:

    nice gather

  69. manoj on April 24th, 2013:

    #include
    using namespace std;
    int main()
    {
    int n,factorial=1;
    cout<>n;
    for(int i=n;i>0;i–){
    factorial=i*factorial;
    }
    cout<<factorial<<endl;
    return (0);
    }

  70. obazee ruth on June 12th, 2013:

    can all for loops be rewritten using a while loop?

  71. Eustacia on July 1st, 2013:

    Thanks so much for your tutorials! I’m doing a basic course in C Code, but it’s taught in Japanese so these are really saving my grade!

    I have a question: I’m supposed to build a program where I enter an integer below a hundred, and all numbers smaller than said integer and containing the number “3″ appear on the screen (etc, if I enter 14, the numbers “3, 13″ should appear).

    However, there’s something wrong with my code, please help! Thank you!

    The code:

    #include

    int main(int argc, const char * argv [])
    {
    int wholenumber;
    printf(“百以内の整数を入力してください\n”);
    scanf_s(“%d”, &wholenumber);

    while(0 <wholenumber)
    {
    wholenumber–;

    while(0 < wholenumber){
    wholenumber = 3 %10;
    wholenumber = 3 /10;

    if (wholenumber == 3);
    {printf("%3d",wholenumber);}
    }
    }
    return 0;
    }

  72. bhawana on July 14th, 2013:

    piz clear these concept also…
    while(n!=0)
    {
    n/=10
    ++count
    }

  73. akj on July 20th, 2013:

    #include
    int main()
    {
    int i;
    for (i=400;i>=1;i–)
    {
    printf(“%d\n”,i);
    }
    return 0;
    }

    y can’t i see 400, the output is starting from 298 and continues till printing 1

  74. Carl on August 7th, 2013:

    Thx for this! I understand loops clearly

  75. Mahesh on September 5th, 2013:

    Program for 12345678910987654321

    #include
    main()
    {
    int i,j;
    for(i=1,j=9;i=1;i++){
    if(i<=10)
    printf("%d",i);
    else{
    printf("%d",j);
    j–;
    }
    }
    getch();
    }

  76. mike swabe on September 17th, 2013:

    how to make 5 * diomond?

  77. Stephen on September 23rd, 2013:

    Excuse me!
    How shall I program two integers then display the factors of the integers and then get their greatest common factors(GCF)…
    May I ask anyone to help me?

  78. Bhagyashree on September 23rd, 2013:

    Thank you so much! I had been looking so long for a proper C tutorial for beginners. Your explanations and examples make it so much easier to understand.

  79. avanish singh on October 13th, 2013:

    hi, sir i want a loop statement then are perform a working
    that are i know you loop dry run

  80. kuldeep on October 16th, 2013:

    can we possibly write this way.

    for(scanf(“%d”, &i)); i<10; i++)
    printf("%d",i);

  81. kuldeep on October 16th, 2013:

    and also this way
    for(i = 1; i<10; printf("%d",i++))

  82. kuldeep on October 16th, 2013:

    or if i’ll use

    for(i=0;i++<10;)
    printf("%d\n",i);

    what will first done in for statement (i++<10) incrementation or comparison

  83. THULASI THAMBI on October 16th, 2013:

    IT WAS HELPFULL & EASIER TO UNDERSTAND….

  84. jakir on November 28th, 2013:

    #include
    int main()
    {
    int num=3,i,a;

    a=0;

    for(i=1;i<=num;i++) {
    a=a+2;
    printf("%d ",a);
    }
    printf("\n");

    for(i=1;i<=num;i++) {
    a=a+4;
    printf("%d ",a);
    }
    printf("\n");

    for(i=1;i<=num;i++) {
    a=a+6;
    printf("%d ",a);
    }
    return 0;
    }

  85. jakir on November 28th, 2013:

    /*the ans of M.Salman question*/
    /*the series of even no.*/

    #include
    int main()

    {
    int num;
    int n;
    //printf(“enter ur number: “);
    //scanf(“%d”,&num);
    for(n=0;n<=100;n++){
    if(n%2==0){
    printf("%d ",n);
    }
    printf("\n");
    }
    return 0;
    }

  86. jhonmark on December 15th, 2013:

    thanks for the codes i will study this codes

  87. J.Aravind Amuthuraj on December 15th, 2013:

    Any one tell me i cant understand the difference between ++i and i++.Please explain in the way of easy understanding.

  88. abhijeet on December 30th, 2013:

    @ Mr. J.Aravind Amuthuraj

    i++ and ++i both are similar…
    but it depends upon the situation… when you are using them,

    lets take an example,

    #include

    int main()
    {

    int x = 10,y;
    int v = 10,z;

    y = ++x + x++;
    z = ++v + ++v;

    printf(“x = %d y = %d”,x,y);
    printf(“\n\nv = %d z = %d”,v,z);

    return 0;
    }

    OUTPUT – x = 12 y = 22

    v = 12 z = 23

    // here as you can see, the values of y and z are changed

    because of the difference of position of ‘++’ increment operator…

    if ++ operator is after the variable then it is executed later… and if ++ operator is before the operator then it is executed first..
    so the value of x is first incremented to 11 and as x = 11 therefore the x++ will result as 11…
    and addition of 2 eleven is 22 so y = 22,

    but in z the value of v is ++v = 11 and then as v = 11 so again ++v = 12
    therefore addition of ++v + ++v will be 23
    hence z will result in 23.

    i hope you understand the difference between ++x and x++. :)

  89. Nik on February 1st, 2014:

    A B C D E D C B A
    A B C D D C B A
    A B C C B A
    A B B A
    A A
    code for this one
    #include
    int main( )
    {
    int i,j,k=65,l;
    for(i=5;i>0;i–)
    {
    for(l=5;l>i;l–)
    {
    printf(” “);
    }
    for(j=0;j<i;j++)
    {
    printf("%c",k+j);
    }
    for(j=0;j<i;j++)
    {
    printf("%c",k-j+i-1);
    }
    printf("\n");
    }
    getchar();
    return 0;

    }

  90. jasveer on February 15th, 2014:

    dear Rakshith kumar:
    i have the answer to your problem:

    #include
    #include
    void main()
    {
    for(int i=1;i<=10;i++)
    {
    cout<=1;i–)
    {
    cout<<i;
    }
    getch();
    }

  91. vaio on March 8th, 2014:

    are,
    for(i=1;i<=5;i++)
    and for(i=1;i++,<= operators because i tend to make mistakes when = are inserted in loops.
    also,for(int i=0;i<10;i++) prints from 0 whereas for(i=1;i++<=5;) prints from 2 to 6.
    i mean if i is initialized from 1 it should start from 1.

  92. chala on April 7th, 2014:

    Really helpful sir

Leave a Reply: