c/c++ having problem with * operator

March 26, 2010 at 23:12:02
Specs: Windows XP
it is program to reverse the digit position of a five digit number.....i m having problem in the 22nd line as the multiplication operator is working strangly......to test it i applied a addition opertaor in the 24th line and it worked fine

int fi,sec,thi,fo;
int num,num1;

printf("\nEnter the num : ");

fi = (num%10000)/1000;
sec =((num%10000)%1000)/100;
thi =(((num%10000)%1000)%100)/10;
fo = (((num%10000)%1000)%100)%10;

printf("\nnum %d",num);
printf("\nfi %d",fi);
printf("\nsec %d",sec);
printf("\nthi %d",thi);
printf("\nfo %d",fo);

num1 = (sec * 10000);


printf("\nReversed Number : %d",num1);

See More: c/c++ having problem with * operator

Report •

March 28, 2010 at 04:00:31
There are much better solutions to the problem at hand.

I suggest using a "while" loop, then it's possible to use the same code for any number a int can hold.

I understand that you are doing a correspondence course(from prior posts) so I am reluctant to post full code. Instead I will post a "psuedo" code of sorts.

// This is a made up language to show logic
// It can't be used as is

initialize variables
     input      = 0;
     reverse = 0;
     counter = 1; //can be zero based if you like
showtext("Enter a number\n");
getinput(int, input);

while(input isn't 0)
    showtext("position =var1=, value =var2=\n", counter, input % 10);
    reverse += num % 10;
    input /= 10;
    if (input isn't 0)
        reverse *=10;
        increment counter;
showtext("Reversed number =var1=, No. digits: =var2=\n" reverse, counter);

To be perfectly honest I just made it too easy for you.

I know your new here but it is customary to close topics with something to let people it' finished. All it need be is:

"Thanks, it worked"
"I found another solution"
"I don't need it anyomre"

(This is going by a couple of you previous posts)

Report •

March 28, 2010 at 04:15:50
I forgot to mention if the reversed number causes leading zero's they won't be accessible from an int.

If the input number is, say, "10000" the the int will only hold "1".

To get around this you will either need to pad the output to "x"(5 per original description) or use a "c string"(char array) to get around it.

Report •

Related Solutions

Ask Question