Create program with following transformation:
1- Reverse a five digit number
you can only use printf, scanf, arithmetic operators, comparison
operators and selection statements i.e. if and if‐else.
Create program with following transformation:
1- Reverse a five digit number
you can only use printf, scanf, arithmetic operators, comparison
operators and selection statements i.e. if and if‐else.
Last edited by Aamir Masood; 11-05-2010 at 04:26 PM.
You can treat the number as a string. Set proper memory constraints on the string variable. Set up a looping structure to print the string array in reverse order.
Welcome to the forum, Aamir!
Post up your code, and tell us what has you stumped with this problem.
u can only use integers and floats with basic mathematical operands , comparison operations and selection statements if and if-else without using loops and strings
You can do this with the modulus operator then.
Well, it's a little awkward, but right off I'd think some int variables like one, two, three, etc., could be used to hold the digits as you peeled them off the original number:
int one, two, three, num=345;
one=num% 10;
num /10;
two=num%10
num /10
kind of logic.
What method has your instructor suggested for this assignment? What have you tried?
Post up what code you have thus far.
This isn't a homework forum, man...
Actually I had used this code and it also worked but everyone is using this code. I wanted some other way to do it .please.
Code:#include<stdio.h> int main() { int a,b,c,d,e,f,g,i,h,j,sum; float o,p,q,r; printf("Enter the five digit number\n"); scanf("%d",&a); b=a%10; c=a/10; d=c%10; e=c/10; f=e%10; g=e/10; i=g%10; j=g/10; printf("%d%d%d%d%d\n",b,d,f,i,j); sum=b+d+f+i+j; printf("Sum of digits = %d",sum); return 0; }
Without using loops, your options are very limited for doing this. I believe my idea of naming and using variables (posted above), was "cleaner" than your last posted code, but it still amounts to the same basic logic.
There are other ways to do this, but none of them are simple enough.
Working with the individual bits in the number, and using the base 2 number line.
Using recursion
Oh! What about using sscanf() ? That's the string scanning function sister of scanf(). That would be easy.
5-digit number you say? You can do this: 99999 - number
Devoted my life to programming...
?Code:99999 -12345 ====== 87654
I was just joking! I think i saw it somewhere.
Devoted my life to programming...