help please.how to do this?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khajeddin
    New Member
    • Nov 2006
    • 51

    help please.how to do this?

    hello:
    i suppose to write a C program which takes a float number and invert it (turn it).
    for example :
    input number 643.21 and mak it: 12.346 .
    i have to write this with basic formations.
    please help me to solve this because i dont know how to do it.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by khajeddin
    hello:
    i suppose to write a C program which takes a float number and invert it (turn it).
    for example :
    input number 643.21 and mak it: 12.346 .
    i have to write this with basic formations.
    please help me to solve this because i dont know how to do it.
    The first way that comes to mind is to convert it into a string, and then print out, or have a tmpVar to hold the variables while you flip them. I think there might be some clever way using the bitwise operators, but I can't remember.

    Found another way, didn't try it yet:

    Comment

    • sivadhas2006
      New Member
      • Nov 2006
      • 142

      #3
      Hi,

      This may be satisfy your need.

      Code:
      #include <stdio.h>
      #include <string.h>
      
      int main(int argc, char* argv[])
      {
         double
            dValue =  643.21;
         char
            szTemp[10];
      
         // Convert the float to the string.
         sprintf(szTemp, "%.2f", dValue);
      
         // Reverse the string.
         strrev(szTemp);
      
         // To print the result.
         printf(szTemp);
         return 0;
      }
      Regards,
      M.Sivadhas.

      Comment

      • khajeddin
        New Member
        • Nov 2006
        • 51

        #4
        thank you all:
        i have wrote this one.please read this and tell me your opinions.



        /*This program takes number and turns and at last print that */
        #include<stdio. h>
        #include<conio. h>
        void main()
        {
        double number ,variable ,lastnumber ;
        float subtract ;
        int counter1 ,counter2 ;
        lastnumber=0 ;
        counter1 = counter2=0;
        printf("Please enter a number,then press the Enter key:");
        scanf("%lf",&nu mber);
        variable=number ;
        while(subtract) /*make a float number to the integer one*/
        {
        number*=10;
        subtract= number - (long)number;
        }
        while((long)var iable) /*calculate number of digits in decimal part of the lastnumber*/
        { variable/=10;
        counter1++;
        }
        while(number>1) /*turn the input number*/
        { lastnumber= (lastnumber*10) +(long)number%1 0;
        number/=10;
        }
        for(;counter2<c ounter1 ;counter2++ )
        lastnumber/=10;
        printf("the result is %lf",lastnumber );
        getch();
        }

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          khajeddin-

          Why do you include <conio.h>? You don't really need it, and it's not standard, so your program will not be as portable if you remove it, and getch() at the end.
          I would also use int main() and then put a return 0; at the end, but that's not necessary (though ti might give you a compiler warning).

          Code:
           while(subtract) /*make a float number to the integer one*/
          Why are you using just a float as the condition? Personally I think you only need one while loop in the program, and breaking them up like that causes the incorrect functionality.

          Check this out for more info on conditional expressions:


          And then if you put the functions all in the same loop, and don't worry about those counters, just print out the number, you should be okay.

          Comment

          • khajeddin
            New Member
            • Nov 2006
            • 51

            #6
            dear sicarie:
            thank you for your answers.
            i use sevral loops and " void main( ) "because i'm limited in using structures to solve this problem.,for instance: i'm not allowed to use any function in any loop or using " int main( ) "& actually it's better to say i have to write this program with simple and basic C structures.thes e are just conditions placed by my professor.
            well i have wrote another program ,wish to know your opinions about this one too.
            Regards.

            Code:
            #include<stdio.h>
            #include<conio.h>
            void main()
             {
              clrscr();
              double number ,lastnumber ,sum ,variable2 ,digit ,subtract;
              int variable1,counter1 ,counter2;
              sum=lastnumber=0;
              counter1=counter2=0;
              printf("Please enter a number,then press the Enter key:");
              scanf("%lf",&number);
              variable1=(int)number;
              number-=(int)number ;
              while(variable1)
            		 {digit=variable1%10;
            		  variable1/=10;
            		  counter2=--counter1;
            		  while(counter2)
            				 {variable2=digit/10.0;
            				  digit=variable2;
            				  counter2++;}
            				  sum+=variable2;}
            	while(subtract)
            		  {number*=10;
            		  subtract=number-(int)number;
            		  if(subtract<0.0000001)
            		  break;}
            	do{
            		lastnumber=(lastnumber*10)+(int)number%10;
            		number/=10;}
            	while(number>1) ;
            	lastnumber+=sum;
            	printf("the result is %lf",lastnumber);
            	getch();
            	}

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              Originally posted by khajeddin
              dear sicarie:
              thank you for your answers.
              i use sevral loops and " void main( ) "because i'm limited in using structures to solve this problem.,for instance: i'm not allowed to use any function in any loop or using " int main( ) "& actually it's better to say i have to write this program with simple and basic C structures.thes e are just conditions placed by my professor.
              well i have wrote another program ,wish to know your opinions about this one too.
              Regards.
              If you're not allowed to use loops, then your current code is illegal as well - you use while several times.

              Could your instructor be wanting you to create functions outside of main to help solve this?

              Other than that, I'd suggest a copout behind the conditional operators ? :

              They mimick an if statement on a single line, without the loop format.

              This is the website of Grant James (ZEUS) This website is dedicated to bring you tutorials on c++, win32, java, photoshop, 3dsmax, visual basic.net, xhtml, php, directx and opengl. My projects, 3D modelling and graphics are also placed here. An explanation of the ternary operator and how it can be used effectively.

              Comment

              • khajeddin
                New Member
                • Nov 2006
                • 51

                #8
                i meant from the sentence :"im limited to use sebral loops".
                that i have to use sevral loops ,and also im not allowed to use any function in the main function or not allowed to use " int main( )".and to answer your question i should say that i can't define any function before or aftre main or use them in main itself.

                Comment

                • sicarie
                  Recognized Expert Specialist
                  • Nov 2006
                  • 4677

                  #9
                  Originally posted by khajeddin
                  i meant from the sentence :"im limited to use sebral loops".
                  that i have to use sevral loops ,and also im not allowed to use any function in the main function or not allowed to use " int main( )".and to answer your question i should say that i can't define any function before or aftre main or use them in main itself.
                  Ok, if you are limited to using several loops, how can you not use one in main itself?

                  I'd go with the ternary operator I mentioned above.

                  Comment

                  • vpawizard
                    New Member
                    • Nov 2006
                    • 66

                    #10
                    Hello,
                    Separate the integer n decimal part of the number. Seperate the digits from both parts of number and put it in array. Print the array backwards.

                    Comment

                    Working...