Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. my problem is output for this weight conversion pounds to kilogram that starts from 100 to 300.

my problem is output for this weight conversion pounds to kilogram that starts from 100 to 300.

Scheduled Pinned Locked Moved C / C++ / MFC
help
9 Posts 4 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_15633472
    wrote on last edited by
    #1

    #include

    int main()
    {
    int i,j;
    float pound, kilogram;

    printf("Pound --------- Kilos\\n\\n");
    
    
    
       do{
    j=1;
    

    do{
    printf("%5d\t",i*j);
    printf("%.2f lbs = %.2f Kg\n", pound, kilogram);
    j++;
    }while(j<=100);
    printf("\n");
    i++;
    }while(i<=300);
    return 0;
    }

    OriginalGriffO 1 Reply Last reply
    0
    • M Member_15633472

      #include

      int main()
      {
      int i,j;
      float pound, kilogram;

      printf("Pound --------- Kilos\\n\\n");
      
      
      
         do{
      j=1;
      

      do{
      printf("%5d\t",i*j);
      printf("%.2f lbs = %.2f Kg\n", pound, kilogram);
      j++;
      }while(j<=100);
      printf("\n");
      i++;
      }while(i<=300);
      return 0;
      }

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Start by indenting your code so it's readable:

      #include

      int main()
      {
      int i,j;
      float pound, kilogram;

      printf("Pound --------- Kilos\\n\\n");
      do
          {
          j=1;
          do
              {
              printf("%5d\\t",i\*j);
              printf("%.2f lbs = %.2f Kg\\n", pound, kilogram);
              j++;
              } while(j<=100);
          printf("\\n");
          i++;
          } while(i<=300);
      return 0;
      }
      

      It makes it so much more obvious what is going on. Then look at your code: where do you modify pound or kilogram? Since they do not change, it will always print the same values. By the way, a better loop format for your application would be a for loop:

      for (int i = 0; i <= 300; i++)
      {
      for (int j = 1; j <= 100; j++)
      {
      ...
      }
      }

      Since you don't initialize i in your code at all, the value can be random depending on which compiler and / or compiler options you use.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Start by indenting your code so it's readable:

        #include

        int main()
        {
        int i,j;
        float pound, kilogram;

        printf("Pound --------- Kilos\\n\\n");
        do
            {
            j=1;
            do
                {
                printf("%5d\\t",i\*j);
                printf("%.2f lbs = %.2f Kg\\n", pound, kilogram);
                j++;
                } while(j<=100);
            printf("\\n");
            i++;
            } while(i<=300);
        return 0;
        }
        

        It makes it so much more obvious what is going on. Then look at your code: where do you modify pound or kilogram? Since they do not change, it will always print the same values. By the way, a better loop format for your application would be a for loop:

        for (int i = 0; i <= 300; i++)
        {
        for (int j = 1; j <= 100; j++)
        {
        ...
        }
        }

        Since you don't initialize i in your code at all, the value can be random depending on which compiler and / or compiler options you use.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        M Offline
        M Offline
        Member_15633472
        wrote on last edited by
        #3

        i try for loop you suggest code:

        #include
        # define POUNDTOKG 0.453592;

        int main()
        {
        int i,j;
        float pound, kilogram;

        printf("Pound --------- Kilos\\n\\n");
        

        for (int i = 0; i <= 300; i++)
        {
        for (int j = 1; j <= 100; j++)
        {
        kilogram = pound * POUNDTOKG;

            }
            printf("%.2f pound = %.2f Kilogram\\n", pound, kilogram);
        }
        return 0;
        

        }

        but my expected output is like this,

        ======================
        Pounds Kilos

        100.00 45.45
        101.00 45.91
        120.00 46.36
        103.00 46.62
        104.00 47.27
        105.00 ........
        .
        .
        .
        300.00 136.08

        but thanks

        Richard DeemingR Michael HulthinM 2 Replies Last reply
        0
        • M Member_15633472

          i try for loop you suggest code:

          #include
          # define POUNDTOKG 0.453592;

          int main()
          {
          int i,j;
          float pound, kilogram;

          printf("Pound --------- Kilos\\n\\n");
          

          for (int i = 0; i <= 300; i++)
          {
          for (int j = 1; j <= 100; j++)
          {
          kilogram = pound * POUNDTOKG;

              }
              printf("%.2f pound = %.2f Kilogram\\n", pound, kilogram);
          }
          return 0;
          

          }

          but my expected output is like this,

          ======================
          Pounds Kilos

          100.00 45.45
          101.00 45.91
          120.00 46.36
          103.00 46.62
          104.00 47.27
          105.00 ........
          .
          .
          .
          300.00 136.08

          but thanks

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          You never initialize the pound variable, so it will always have the same (random) value.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          • M Member_15633472

            i try for loop you suggest code:

            #include
            # define POUNDTOKG 0.453592;

            int main()
            {
            int i,j;
            float pound, kilogram;

            printf("Pound --------- Kilos\\n\\n");
            

            for (int i = 0; i <= 300; i++)
            {
            for (int j = 1; j <= 100; j++)
            {
            kilogram = pound * POUNDTOKG;

                }
                printf("%.2f pound = %.2f Kilogram\\n", pound, kilogram);
            }
            return 0;
            

            }

            but my expected output is like this,

            ======================
            Pounds Kilos

            100.00 45.45
            101.00 45.91
            120.00 46.36
            103.00 46.62
            104.00 47.27
            105.00 ........
            .
            .
            .
            300.00 136.08

            but thanks

            Michael HulthinM Offline
            Michael HulthinM Offline
            Michael Hulthin
            wrote on last edited by
            #5

            Can't you make simpler? Like this? Hm, this was not supposed to be a reply to Richard, but to the OP. Sorry.

            #include #define POUNDTOKG 0.453592

            int main() {
            printf("Pound --------- Kilos\n\n");
            for (int pound = 100; pound <= 300; pound++) {
            printf("%.2f pound = %.2f Kilogram\n", pound, pound * POUNDTOKG);
            }
            return 0;
            }

            M 2 Replies Last reply
            0
            • Michael HulthinM Michael Hulthin

              Can't you make simpler? Like this? Hm, this was not supposed to be a reply to Richard, but to the OP. Sorry.

              #include #define POUNDTOKG 0.453592

              int main() {
              printf("Pound --------- Kilos\n\n");
              for (int pound = 100; pound <= 300; pound++) {
              printf("%.2f pound = %.2f Kilogram\n", pound, pound * POUNDTOKG);
              }
              return 0;
              }

              M Offline
              M Offline
              Member_15633472
              wrote on last edited by
              #6

              thanks, but i want like this output below, but when I run it in dev C++, pounds is not start 100 only kilos has a value I want to put 100 to 300 but how can I do that. but thanks a lot ====================== Pounds Kilos ====================== 100.00 45.45 101.00 45.91 120.00 46.36 103.00 46.62 104.00 47.27 105.00 ........

              1 Reply Last reply
              0
              • Michael HulthinM Michael Hulthin

                Can't you make simpler? Like this? Hm, this was not supposed to be a reply to Richard, but to the OP. Sorry.

                #include #define POUNDTOKG 0.453592

                int main() {
                printf("Pound --------- Kilos\n\n");
                for (int pound = 100; pound <= 300; pound++) {
                printf("%.2f pound = %.2f Kilogram\n", pound, pound * POUNDTOKG);
                }
                return 0;
                }

                M Offline
                M Offline
                Member_15633472
                wrote on last edited by
                #7

                your code is almost correct but in pound there is a no 100 to 300, how can I put it in my code, but thanks a lot

                Michael HulthinM 1 Reply Last reply
                0
                • M Member_15633472

                  your code is almost correct but in pound there is a no 100 to 300, how can I put it in my code, but thanks a lot

                  Michael HulthinM Offline
                  Michael HulthinM Offline
                  Michael Hulthin
                  wrote on last edited by
                  #8

                  Well it is, kindof. :) The format specfier to printf is wrong. Try this

                  #include
                  #define POUNDTOKG 0.453592

                  int main() {
                  printf("Pound --------- Kilos\n\n");
                  for (int pound = 100; pound <= 300; pound++) {
                  printf("%d pound = %.2f Kilogram\n", pound, pound * POUNDTOKG);
                  }
                  return 0;
                  }
                  Pound --------- Kilos

                  100 pound = 45.36 Kilogram
                  101 pound = 45.81 Kilogram
                  102 pound = 46.27 Kilogram
                  103 pound = 46.72 Kilogram
                  ...
                  298 pound = 135.17 Kilogram
                  299 pound = 135.62 Kilogram
                  300 pound = 136.08 Kilogram

                  ...Program finished with exit code 0
                  Press ENTER to exit console.

                  M 1 Reply Last reply
                  0
                  • Michael HulthinM Michael Hulthin

                    Well it is, kindof. :) The format specfier to printf is wrong. Try this

                    #include
                    #define POUNDTOKG 0.453592

                    int main() {
                    printf("Pound --------- Kilos\n\n");
                    for (int pound = 100; pound <= 300; pound++) {
                    printf("%d pound = %.2f Kilogram\n", pound, pound * POUNDTOKG);
                    }
                    return 0;
                    }
                    Pound --------- Kilos

                    100 pound = 45.36 Kilogram
                    101 pound = 45.81 Kilogram
                    102 pound = 46.27 Kilogram
                    103 pound = 46.72 Kilogram
                    ...
                    298 pound = 135.17 Kilogram
                    299 pound = 135.62 Kilogram
                    300 pound = 136.08 Kilogram

                    ...Program finished with exit code 0
                    Press ENTER to exit console.

                    M Offline
                    M Offline
                    Member_15633472
                    wrote on last edited by
                    #9

                    That's great, thanks a lot for your help and finally I will pass my pre-final exam in c programming, thank you

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups