I want to Print Characters ABCD.. in Pattern but this code is not printing output on screen , Please Guide me and Tell me where is my mistake ?

#include<stdio.h>
#include<conio.h>
main()
{
    int i,j;
    int count=65;
 for(i=0;i<=10;i++)
 {
     for(j=65;j<=i;j++;count++)
     {
         printf("%c",count);

     }
     printf("\n");
 }
}

Recommended Answers

All 2 Replies

for(j=65;j<=i;j++;count++)

Too much. Should have only three statements.

There are a number of problems with this code, but the most important is that there is a syntax error in the increment part of the inner for() loop.

     for(j=65;j<=i;j++;count++)

As things stand, this code shouldn't compile; the third semi-colon should be just a comma.

     for(j=65;j<=i;j++,count++)

This isn't the cause of the problem, however, assuming it compiles at all. The real problem is that you are setting j to 65, then comparing whether that is less than or equal to i, the value of which is known to be less than 10 (because of the condition of the outer loop). This means that the body of the inner loop will never execute.

Even if it did, there are problems with that loop body. You are declaring count as an int value, which means that it is at least 2 bytes wide; but then you are passing it to printf()'s formatting string as a char value, which is defined as being no more than 1 byte wide. While this may appear to work correctly in some compilers when running on a little-endian processor (such as the PC), it is not really correct, and not guaranteed to work. Most modern C compilers will check the formatting string and warn against this kind of risky conversion, in fact. It would make more sense simply to declare count as a char value, especially since you are dealing with values below 0xF0 (decimal 127).

There are also some problems stemming from outdated practices. For example, implicit function returns were eliminated from the language with the C99 standard if I recall correctly. In all modern C compilers, you must declare main() explicitly as returning an int value, and have a return statement at the end of the function.

A second archaism is the reference to <conio.h>, which is both non-standard and obsolete. Even if that weren't the case, you aren't using anything that requires the header; therefore, you should remove the reference to it as unnecessary.

Putting this all together, you probably wanted something like this:

#include <stdio.h>

int main()
{
    int i, j;
    char letter;

    for(i=0; i < 10; i++)
    {
        letter = 'a';

        for(j = 0; j <= i; j++, letter++)
        {
            printf("%c", letter);

        }
        printf("\n");
    }

    return 0;
}

Now, whether or not this actually gives the pattern you are looking for isn't clear, because you don't explain what the pattern is supposed to look like; but at least it should put you on the right track.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.