Thread: Printing Armstrong Numbers from 1-500

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    47

    Printing Armstrong Numbers from 1-500

    Code:
    /*
    Write a program to print out all Armstrong numbers between 1 and 500. 
    If sum of cubes of each digit of the number is equal to the number itself, 
    then the number is called an Armstrong number. 
    For example, 
    153 = (1*1*1) + (5*5*5) + (3*3*3)
    */
    
    
    #include<stdio.h>
    main()
    {
    	int number, temp, digit1, digit2, digit3;
    
    	printf("Printing all Armstrong numbers between 1 and 500:\n\n");
    
    	number = 001;
    
    	while (number <= 500)
    	{
    		digit1 = number%10;
    		digit2 = (number%100) - ((number/100)*10);
    		digit3 = number%1000;
    
    		temp = (digit1*digit1*digit1) + (digit2*digit2*digit2) + (digit3*digit3*digit3);
    
    		if (temp == number)
    		{
    			printf("\nAmstrong Number:%d", temp);
    		}
    
    		number++;
    	}
    }

    Whats wrong with this code??

    It does not give any output at all!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your digit extraction perhaps?

    Use a series of /10 and %10
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    47
    Okay..i will try that..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Also try printing out your digits (or examine them with the debugger) as you go.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    47
    Worked

    I changed the digit extraction scheme to this:

    Code:
    digit1 = number - ((number / 10) * 10);
    digit2 = (number / 10) - ((number / 100) * 10);
    digit3 = (number / 100) - ((number / 1000) * 10);

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    47
    But still...


    number%10 --- should extract the last number
    and
    number%100 -- should have extracted the first number (of a three digit number), isn't it?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    123 % 100 gets you 23
    23 / 10 gets you 2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  2. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  4. printing only prime numbers
    By Micko in forum C Programming
    Replies: 30
    Last Post: 06-10-2004, 10:23 PM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM