Hello people i got a h.w to do Using nested loops to produce the following pattern:
F

FE

FED

FEDC

FEDCB

FEDCBA


I had idea to do it but that will require array and strlen(char first int of the outer loop) and the thing says that I must do it with char only
but here my code i made problem is that it need to intilise c='F' then its bigger than or = 'A' and i decrement it so it shows from F to A problem that there wont be any thing to intilise the inner loop to the outer integer or that may seem true to me maybe its wrong since i dont have much programming experience and i alawys sucks in nested loop anyways here code now

#include <stdio.h>
int main(void)
{
    int i,x;
    char c;
    for(i=0;i<6;i++) {
        for(c='F',x=i;c>='A' && x<=i;c-- , x++) {
            printf("%c ",c);
            }
        puts("");
        }
    return getchar();
}

as i think in inner loop i intilise c to f then c is bigger or = to a then i make x<=i whish will like first time 1 2 etc but shouldnt that also print
F then FE etc ? since x will be incremeted each time ?

Dani AI

Generated

A simple way to think about this problem is to separate two concerns: (1) how many characters to print on each line, and (2) which characters to print. The outer loop selects the line index (0..5). The inner loop must run row+1 times — the original bug came from initializing the inner counter to i and testing x <= i; after the first iteration x becomes greater than i and the loop stops, so only one character prints. Initializing the inner counter to 0 (or bounding the inner loop by row+1) fixes that.

Here is a compact, idiomatic C version that avoids an extra integer counter by computing the ending character for each row:

#include <stdio.h>

int main(void) {
    const char START = 'F';
    const int LINES = START - 'A' + 1; /* 6 */
    for (int row = 0; row < LINES; ++row) {
        char end = START - row;
        for (char c = START; c >= end; --c)
            putchar(c);
        putchar('\n');
    }
    return 0;
}

Notes and small gotchas: this relies on the execution character set having contiguous uppercase letters (true on virtually all modern systems that use ASCII-compatible encodings). For absolute portability use a string literal like "FEDCBA" if needed. putchar is simpler and faster than printf when printing single characters. If spaces between letters are wanted, print a space after each character.

Good work by getting the inner loop to start at zero, and thanks to for the suggestion to break the problem into smaller pieces. One final caution: do not return getchar() from main — that returns a nonzero exit code (usually indicating failure). Use return 0; or EXIT_SUCCESS for a successful exit.

Recommended Answers

All 5 Replies

Take the pattern out of it and do a simple triangle:

F
FF
FFF
FFFF
FFFFF
FFFFFF

You can add another variable that counts down without changing how the loops work in the triangle code:

for x = 1 to 6
    for y = 0 to x
        print 'F'
    print '\n'
for x = 1 to 6
    c = 'F'
    for y = 0 to x
        print c
        c = c - 1
    print '\n'

Try to take your problems and break them apart into smaller problems and you can see how to do things easier. :)

ohh thanks alot man i got it to work yah i will divide stuff into smaller stuff so i get the result in the end

#include <stdio.h>
int main(void)
{
    int i,x;
    char c;
    for(i=0;i<6;i++) {
        c='F';
        for( x=0 ; x<=i; x++ ) {
            printf("%c ",c);
            c--;
            }
        puts("");
        }
    return getchar();
}

problem that i did x=i; whish set x to the same num as i everytime the loop counts

return getchar();

I forgot to mention, but returning getchar() is a bad idea because it's never going to be 0, and a nonzero return from main means the program failed. The best three values for returning from main are 0 and EXIT_SUCCESS for success, or EXIT_FAILURE for failure. EXIT_SUCCESS and EXIT_FAILURE are defined in <stdlib.h>.

well thats just small program since i m coding that in dev but with big programs i code in other compilers like msv8 i return 0;

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.