Printing Armstrong numbers from 1 to 500. (C Programming)
Code:
/* Whats wrong with the program? It does not give a single number in the output! |
Per the rules:
Do not expect LQ members to do your homework - you will learn much more by doing it yourself. However, providing hints is acceptable. Have you verified that digit1, digit2 and digit3 equations actually produce the correct values of your number? |
Hey..i was not expecting you to do my homework!!
I worked all the way upto this step..and still the program is not working..so I asked for help! well.I changed the Digit Extraction scheme and it worked :) Changed it to this: Code:
digit1 = number - ((number / 10) * 10); |
Well done.
|
Just to comment on what you have done, Whilst your second approach is correct, it is a little ungainly, the first approach to get the digits actually had some merit. With a little more thought, you can make that code much cleaner.
And for the moment I'll leave it there as a poser... :) |
slightly alternate solution-
#include<stdio.h>
void main() { int number,temp,digit1,digit2,digit3; for(number=001;number<=500;number++) { digit1 = number - ((number/10)*10); digit2 = (number - ((number/100)*100))/10; digit3 = number/100; temp = (digit1*digit1*digit1) + (digit2*digit2*digit2) + (digit3*digit3*digit3); if(temp==number) printf("\nArmstrong number: %d",temp); } } Here in my code, digit2 and digit3 values are a little different- digit1 = number - {(number/10)*10} digit2 = {number - ((number/100)*100)}/10 digit3 = number / 100 |
Welcome to LQ!
You have posted to an 11 year dead thread, best to leave them at rest! Please start your own threads for your own questions. See the LQ posting guidelines foru guidance in posting your questions as well. And when you post code, please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls. |
I'm glad he did, I enjoyed having a go at that one. :)
BTW, watch out for those leading zeros! Code:
for(number=001;number<=500;number++) |
Quote:
This code ... Code:
seq -w 500 \ Code:
001 |
Execution time is always of interest.
The original problem statement (in post #1) is ... Code:
Write a program to print out all Armstrong numbers between 1 and 500. Code:
echo "Method #1 of LQ Member danielbmartin." The command seq -w 500 creates a stream of lines in the form Code:
001 Code:
echo "Method #2 of LQ Member danielbmartin." Now, another surprise. A solution which uses only awk looks verbose and clumsy but turned out to be the fastest of the three. Code:
echo "Method #3 of LQ Member danielbmartin." Code:
Method #1 of LQ Member danielbmartin, repeated 1000 times. Daniel B. Martin |
Yeah, this is fun. Didn't time it.
Code:
awk 'BEGIN{for (h=0;h<8;h++) |
Quote:
Code:
BEGIN{for (j=1;j<=500;j++) { Code:
BEGIN{for (j=1;j<=500;j++) { |
Timings ...
Code:
Method #1 of LQ Member danielbmartin, repeated 1000 times. Daniel B. Martin |
There's two basic approaches -- try to calculate every Armstrong number between 1 and 500 or check every number between 1 and 500 to see if it's an Armstrong number. I assume this has already been made clear.
Which is faster would need testing. |
Quote:
... this requires an algorithm. Do you know one? check every number between 1 and 500 to see if it's an Armstrong number ... this is exhaustive enumeration and is the basis of all solutions posted so far. Granted, avoiding 8s and 9s is a small shortcut but all the posted solutions fall into the Brute Force category. Daniel B. Martin |
All times are GMT -5. The time now is 04:27 PM. |