Newbie at C help

Viewing 3 reply threads
  • Author
    Posts
    • #8129
      cyber.spirit
      Participant

      Hi there
      I’m learning C language and i am so new in this world. so i have two problems the first one is i need to print a char two times in a line without using printf twice and i used this code:

      #include 

      int main(void)

          char chr = 'a';
          printf("%2cn", chr);
          return 0;
      }

      as you know the %2 won’t print ‘a’ two times it will show it like this in output ”  a” (with 2 spaces at first) and it is not bad i will need it too
      but i need to declare a variable for it instead of %2 i need something like this:

      #include 

      int main(void)

          int i = 2;
          char chr = 'a';
          printf("%icn", chr);
          return 0;
      }

      but it is not correct so can you please resolve these two problems god is bless you
      regards cyber.spirit

    • #51446
      caissyd
      Participant

      Hey,

      You can do this in many, many different ways. Here are two working examples:

      #include 

      int main(void)

          char chr = 'a';

          for(int i=0; i<2; i++)
          {
      printf("%c", chr);
          }

          return 0;
      }

      Loops twice and there prints the value of ‘chr’ twice. Notice I removed the the new line character “n” (which you can add later)

      #include 

      int main(void)

          char chr = 'a';
          printf("%c%c", chr, chr);

          return 0;
      }

      Another variation, without the loop.

      I used to teach C and C++ at a College. So I would be happy to give you a hand!

    • #51447
      cyber.spirit
      Participant

      @H1t M0nk3y wrote:

      Hey,

      You can do this in many, many different ways. Here are two working examples:

      #include 

      int main(void)

          char chr = 'a';

          for(int i=0; i<2; i++)
          {
      printf("%c", chr);
          }

          return 0;
      }

      Loops twice and there prints the value of ‘chr’ twice. Notice I removed the the new line character “n” (which you can add later)

      #include 

      int main(void)

          char chr = 'a';
          printf("%c%c", chr, chr);

          return 0;
      }

      Another variation, without the loop.

      I used to teach C and C++ at a College. So I would be happy to give you a hand!

      Hi there whats up? Man for my program maybe i need to print it more than 1000 times two was an exmaple so loop is better but i need to use another loop too so it may cause some confilict too any way what about the second one? Thx so much

    • #51448
      caissyd
      Participant

      There’s a joke in French that, if I translate it word by word, it would say: “Could you be less clear? I understand too much…”  😀

      So, could you please tell me what you are trying to achieve? I will be able to help you better.

      Meanwhile, you can always have multiple loops, one within another. For example:

      #include 

      int main(void)

          char chrA = 'a';
          char chrB = 'b';
          char chrC = 'c';

          for(int a=0; a<4; a++)
          {
              printf("%c", chrA);

              for(int b=0; b<3; b++)
              {
                  printf("%c", chrB);

                  for(int c=0; c<2; c++)
                  {
                      printf("%c", chrC);
                  }
              }
          }

          return 0;
      }

      Output:

      abccbccbccabccbccbccabccbccbccabccbccbcc

      Try to see if you understand the output.

      As you can see, you can have multiple loops within each others.
      But let me know exactly what you are trying to do, so I can help you better.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.

Copyright ©2021 Caendra, Inc.

Contact Us

Thoughts, suggestions, issues? Send us an email, and we'll get back to you.

Sending

Sign in with Caendra

Forgot password?Sign up

Forgot your details?