Home » C programming language

for loop find output of C programs

Find output of C programs (for loop) in C: Here, you will find some of the C programs based on for loop with the outputs and explanations.

Program-1

#include <stdio.h>

int main()
{
    int i;
    for(i=1; i<=10;i++)
        printf("i=%d\n",i);
	return 0;
}

Output

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

Explanation

It’s a simple loop format, which will print value from 1 to 10.


Program-2

#include <stdio.h>

int main()
{
    int i;
    for(i=1; i<=10;i++);
    printf("i=%d\n",i);
	return 0;
}

Output

i=11

Explanation

See the semicolon, after the for loop statement (loop is terminating without anybody), the printf statement is not a body of for loop.

Loop will be executed and when the value of i will be 11, program's execution reaches to the printf statement. Thus, the output will be i = 11.


Program-3

#include <stdio.h>

int main()
{
    int i;
    for(i=65; i<(65+26);i++)
        printf("%c ",i);
	return 0;
}

Output

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Explanation

See the printf statement, here we are printing the value in character format (using "%c" format specifier) and the value of i is 65 to 90, which are the ASCII codes of Uppercase alphabets from A to Z. Thus, the output will be "A B C ... Z".


Program-4

#include <stdio.h>

int main()
{
    int i;
    for(i=1; -1; i++)
        printf("%c ",i);
	return 0;
}

Output

1 2 3 4 ... infinite times

Explanation

See the conditional part of the loop statement, it is "-1" which is a non zero value and in C programming language a non zero value considered as "true", so this is a infinite loop.






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.