C language > Beginner questions

My first steps in learning C

(1/3) > >>

Adrian Rizeanu:
Hello
I try to implement in C the following: I wish to organize the following number: 1234 in a triangle form, let's say :   3
                                                                                                                                                                                   23
                                                                                                                                                                                  1234
The idea is to break the number in 4 digits , divide every digit in modulo 2 and keep the rest , getting rid of the digit and going to the following one, starting from x^0 , to x^3 ( units, tenth, hundreds , thousands), and finally printing them in the order i choose.
So, my program is:
#include<stdio.h>
int main(void)
{
   unsigned long x;
   unsigned x0,x1,x2,x3;
   printf("please print your number:\n");
   scanf("%lu",&x);
   x0%10;  //keep the units digit
   x/10; // get rid of the units
   x1%10;  //keep the tenth
   x/10;  // get rid of tenth
   x2%10;  //keep the hundreds
   x/10;  //get rid of hundreds
   x3%10;  // keep the hundreds
   x/10; // in x remain the thousands
   printf("   %u\n",x2);  // space , space, x2
   printf(" %u%u\n",x2,x1);  /space x2.x1
   printf("%lu%u%u%u\n, x3,x2,x1,x0);  /no space, x3,x2,x1,x0
   return 0; }

And  the result after input : 1234 , compile is : 3
                                                                        34
                                                                      1233
I can not understand where i am wrong , please help.
Thank you
Adrian
   




algernon_77:
Hello Adrian!

I'm not sure I understand exactly what you want to accomplish (you say modulo 2, but you use modulo 10 in code); However, I changed the code a bit to get to the result I believe you seek:


--- Code: ---    unsigned long x;
    unsigned x0,x1,x2,x3;

    printf("please print your number:\n");
    scanf("%lu",&x);

    x0 = x % 10;  //units
    x  = x / 10;

    x1 = x % 10;  //tens
    x  = x / 10;

    x2 = x % 10;  //hundreds
    x  = x / 10;
 
    x3 = x % 10;  //thousands

    printf("  %u\n", x1);
    printf(" %u%u\n", x2, x1);
    printf("%lu%u%u%u\n", x3, x2, x1, x0);

--- End code ---

Please note the use of the equal sign to actually assign the result to your variables and the use of x1 variable instead of x2 in the first printf.
Good luck!

Adrian Rizeanu:
Hello Algernon_77
Thank you very much for your help!
As soon as i finish my reply to you I will try it on my C compiler ( Pelles of corse )
I feel surely very confused as i am learning from a book that used Borland compiler for explaining and I can not have that for the moment.
May I kindly ask you to let me know where can i find a free tutorial for C ? It is obvious i need to start again my study, confusing %modulo 2 with modulo 10 is not acceptable...
Kindest regards,
Adrian

Adrian Rizeanu:
/*This program is intended to organize the digits of a number in a triangle shape */

#include<stdio.h>
int main(void)
 {
   unsigned long x;
   unsigned x0,x1,x2,x3;

   printf("please print your number:\n");

   scanf("%lu",&x);

   x0=x%10;  //units
   x=x/10;

   x1=x%10;  //tens
   x=x/10;

   x2=x%10;
   x=x/10;

   x2=x%10; //hundreds
   x/10;

   x3=x%10;  //thousands

   printf("   %u\n",x1);
   printf("  %u%u\n",x2,x1);
   printf(" %u%u%u\n",x3,x2,x1);
   printf("%lu%u%u%u\n",x3,x2,x1,x0);

   return 0;}

So, after building the project i get : 3
                                                       13
                                                     113
                                                   1134
Could you please help me understand where is my mistake ? ( my number is : 1234 )
Thank you very much
Adrian

algernon_77:
Hello!

It does not work because you assign x mod 10 to x2, then divide x by 10, then assign x mod 10 to x2 again, overwriting it:


--- Code: ---   x2=x%10;
   x=x/10;

   x2=x%10; //hundreds
   x/10;

--- End code ---


But I also made a mistake :) in the first printf it should be x3, not x1... sorry for the confusion:


--- Code: ---int main(void)
{
    // you can make them all unsigned, it's the same size as unsigned long (32 bit)
    unsigned x,x0,x1,x2,x3;

    printf("please print your number:\n");
    scanf("%lu",&x);
 
    x0 = x % 10;  //units
    x  = x / 10;

    x1 = x % 10;  //tens
    x  = x / 10;

    x2 = x % 10;  //hundreds
    x  = x / 10;
 
    x3 = x % 10;  //thousands

    printf("   %u\n", x3);
    printf("  %u%u\n", x2, x1);
    printf(" %u%u%u\n",x3,x2,x1);
    printf("%u%u%u%u\n", x3, x2, x1, x0);

    return 0;
}

--- End code ---

As for books, "The C programming language" by Kernighan and Ritchie would be nice, but there's also a lot of tutorials online.

Navigation

[0] Message Index

[#] Next page

Go to full version