4 Replies - 12651 Views - Last Post: 25 September 2008 - 02:04 AM Rate Topic: -----

#1 Tarquin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-September 08

Armstrong number

Posted 17 September 2008 - 10:28 AM

[size=3]I know the code to check if given number is armstrong or not.....
But what i cannot get is--
To Print All Armstrong Numbers upto Given Number.....i think we have to use for loop and while loop.....plz help!
/*
	WAP to find number is Armstrong Number or not
*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
	int no,dig,temp,sum=0;

	clrscr();

	printf("Enter no : ");
	scanf("%d",&no);

	temp=no;

	while(temp>0)
	{
		dig=temp%10;

		sum=sum+dig*dig*dig;

		temp=temp/10;
	}

	if (no==sum)
		printf("\n %d is an Armstrong Number",no);
	else
		printf("\n %d is not an Armstrong Number",no);

	getch();



plz plz can u ppl help me......practicle xam in 3 days away!! :blink:
I want help on this code

!! To Print All Armstrong Numbers upto Given Number!!

Is This A Good Question/Topic? 0
  • +

Replies To: Armstrong number

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Armstrong number

Posted 17 September 2008 - 01:44 PM

Put a for loop in the code and it will print all the numbers it decides are Armstrong numbers, up to the given number.
for(int no=0; no<=5000; no++) {

    temp=no;

    while(temp>0)
    {
        dig=temp%10;

        sum=sum+dig*dig*dig;

        temp=temp/10;
    }

    if (no==sum)
        printf("\n %d",no);
}



Are 0 and 1 the only Armstrong numbers, up to 5000? That is what this program is telling me. I might have an error somewhere, but I copied your formula, and the for loop should work.

This post has been edited by OliveOyl3471: 17 September 2008 - 01:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 thenovices  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 74
  • Joined: 19-January 08

Re: Armstrong number

Posted 17 September 2008 - 06:52 PM

you forgot to reset the sum on each iteration.

here's my code in C++
 #include<iostream>
using namespace std;

int main() {
	int input;
	int sum = 0;
	int digit;
	int number;
	
	cout << "Find all the armstrong numbers up to number: ";
	cin >> input;
	
	for (int i = 1; i <= input; i++) { //check every number from 1 to num
		number = i;
		while (number > 0) {
			digit = number % 10; //grab the numbers digit
			
			sum += digit*digit*digit;
			
			number /= 10;
		}
		
		if (sum == i) //if the sum of the cubes of the digits equals the number
			cout << i << endl; //print out the armstrong number
		
		sum = 0; //reset the sum
	}
	
	cout << "DONE!" << endl;
	
	return EXIT_SUCCESS;
}


Was This Post Helpful? 1
  • +
  • -

#4 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Armstrong number

Posted 17 September 2008 - 08:18 PM

Should the output now be:
0
1
153
370
371
407

(up to 500) ?
Was This Post Helpful? 0
  • +
  • -

#5 Soura  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-September 08

Re: Armstrong number

Posted 25 September 2008 - 02:04 AM

View PostTarquin, on 17 Sep, 2008 - 10:28 AM, said:

[size=3]I know the code to check if given number is armstrong or not.....
But what i cannot get is--
To Print All Armstrong Numbers upto Given Number.....i think we have to use for loop and while loop.....plz help!

Here is the program to find the armstrongs upto 5 digit. It is long, but correct. Please try this.
# include<stdio.h>
# include<conio.h>
# include<iostream.h>

main()
{
	  int n, d1, d2, d3, d4, d5, s=0, a, o;
	  printf("Press '1' for 3 digit Armstrong numbers.\nPress '2' for 4 digit Armstrong numbers.\nPress '3' for 5 digit Armstrong numbers.\n");
	  printf("Enter your option:");
	  scanf("%d", &o);
	  switch(o)
	  {
			   case 1:
	  while(n<=999)
	  {
					 a=n;
					 d1=a%10;
					 a=a/10;
					 d2=a%10;
					 a=a/10; 
					 d3=a;
					 s=(d1*d1*d1)+(d2*d2*d2)+(d3*d3*d3);
					 if(n==s)
					 printf("The Armstrong numbers are %d\n", n);
					 n=n+1;
	  }
					 break;	
				case 2:
	  while(n<=9999)
	  {
					 a=n;
					 d1=a%10;
					 a=a/10;
					 d2=a%10;
					 a=a/10; 
					 d3=a%10;
					 a=a/10; 
					 d4=a;
					 s=(d1*d1*d1*d1)+(d2*d2*d2*d2)+(d3*d3*d3*d3)+(d4*d4*d4*d4);
					 if(n==s)
					 printf("The Armstrong numbers are %d\n", n);
					 n=n+1;
	  }
					 break;
				case 3:
	   while(n<=99999)
	  {
					 a=n;
					 d1=a%10;
					 a=a/10;
					 d2=a%10;
					 a=a/10; 
					 d3=a%10;
					 a=a/10; 
					 d4=a%10;
					 a=a/10; 
					 d5=a;
					 s=(d1*d1*d1*d1*d1)+(d2*d2*d2*d2*d2)+(d3*d3*d3*d3*d3)+(d4*d4*d4*d4*d4)+(d5*d5*d5*d5*d5);
					 if(n==s)
					 printf("The Armstrong numbers are %d\n", n);
					 n=n+1;
	  }
	  }
	  getch();
}

This post has been edited by Soura: 25 September 2008 - 03:07 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1