KKot
KKot

Reputation: 163

Can't I get the output in the image more easily with the help of a nested for loop in C?

int i,j,k;


 int sayi=1;
        for(i=1; i<=sayi; i+=2){
             printf("%d",i);
               printf("a");
                printf("%d",i+2);
                 printf("a");
                 printf("%d",i+4);
                  printf("a");
              printf("\n");
            
         for(j=1; j<=sayi; j+=2)
             printf("%d",i);
            printf("a");
            printf("%d",i+2);
             printf("a");
              printf("%d",i+4);
             printf("\n");
             
             for(k=1; k<=sayi; k+=2)
              printf("%d",i);
            printf("a");
            printf("%d",i+2);
             printf("a");
             
             
             printf("\n");
              printf("%d",i);
              printf("a");
            printf("%d",i+2);
             
               printf("\n");
               printf("%d",i);
              printf("a");
              
                 printf("\n");
                  printf("%d",i);
    }

enter image description here

I need to add the letter 'a' between the numbers as the numbers move in the form of 1-3-5 consecutively. I'm trying to get the output on the screen with nested for loops. But I think it's much shorter bit path than my method how can I do it?

Upvotes: 0

Views: 51

Answers (2)

computercarguy
computercarguy

Reputation: 2453

You are already using nested for loops. Unfortunately, you aren't using them correctly.

for(j=1; j<=sayi; j+=2)
for(k=1; k<=sayi; k+=2)

These are both nested within the "for(i=1; i<=sayi; i+=2)" because of your brackets, { and }. These inner loops only have one line within them because you aren't using brackets to define where they start and end, so the compiler's rules automatically defaults to them consisting of only the one valid line of code following the loop.

None of the 3 loops you have in your example are actually running as loops because you aren't incrementing the "sayi" variable and it being 1. When you assign "i", "j", and "k" to 1 also, and compare that to "sayi" with a "less than or equal" comparison, they run because it's true, but then when you increment the loop variables by 2, they become 3 and the "less than or equal to" comparison fails, so the loop is only run once.

In order to shorten this, you'll need to learn quite a few things:

The if statement is a major piece of this. https://www.cprogramming.com/tutorial/c/lesson2.html

How loops actually work. https://www.freecodecamp.org/news/for-loops-in-c/

Mathematical, assignment, and conditional operators. https://www.programiz.com/c-programming/c-operators

The links I gave are just suggested starting points. There's more to learn than just that. I don't have any connection to those sites or people putting them out. They are simply the best of the first search results I looked at.

Here's my version of the code you are trying to write.

int start = 6;

for (int i = start; i > 0; i--)
{
    for (int j = 1; j <= i; j++)
    {
        if (j % 2 == 1)
        {
            printf("%d", j);
        }
        else
        {
            printf("a");
        }
    }

    printf("\n");
}

To be certain, this can be reduced further, but I think this is a good enough starting point for you.

Upvotes: 1

S3gfault
S3gfault

Reputation: 393

This should work. I put in some comments explaining it.

#include <stdio.h>

int main() {
    //up to what number do you want it to go
    int n = 5;

    for (int i = n; i > 0; i--) { //loops over every line you need to print with i being the length of that line minus 1. I starts at n and ends at 0
        for (int j = 1; j <= i; j++) {  // loops over every number in that particular line, starting from 1 up to i.
            if (j % 2) { //if j is even it prints 'a', otherwise it prints the number
                printf("%d", j);
            } else {
                printf("a");
            }
        }
        printf("\n");
    }
}

Upvotes: 1

Related Questions