#include<stdio.h>
int main(){
int num,r,sum,temp;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Armstrong numbers in given range are: ");
for(num=min;num<=max;num++){
temp=num;
sum = 0;
while(temp!=0){
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}
return 0;
}
#include <iostream>
#include <math.h> // for std::pow()
unsigned int get_length(unsigned int num,const unsigned int base=10)
{
unsigned int len=1;
while(num && (num/=base))
++len;
return( len );
}
bool is_armstrong(const unsigned int num,const unsigned int base=10)
{
unsigned int len=get_length(num,base);
unsigned int sum=0;
unsigned int tmp=num;
while(tmp)
{
sum+=(unsigned int)std::pow((double)(tmp%base),(double)len);
tmp/=base;
}
return(num==sum);
}
int main()
{
std::cout << "Armstrong series (base 10):";
for(unsigned int num=0; num<=0xffffffff; ++num)
if(is_armstrong(num))
std::cout << " " << num;
std::cout << std::endl;
return(0);
}
#include<stdio.h>
void main()
int n,m,q,sum=0;
do
{
pf("enter the value of n");
scanf("%d",&n);
q=n%10
sum=sum+(q*q*q)
n=n/10
}
while(n>0);
pf("palindrome");
while(n<0);
pf('not palindrome");
}
}
#include <stdio.h>
#include <conio.h>
void main()
{
int a=1,b,y,m,q;
while(a<=500)
{
b=a;
y=0;
while(b!=0)
{
m=b%10;
q=b/10;
y=y+(m*m*m);
b=q;
}
if(y==a)
{
printf("%d\n",a);
}
a++;
}
getch();
}SOUMYA TALREJA BHOPAL
To find Armstrong numbers using a for loop, the loop simply generates decimal numbers in a given range and tests each of them to see if they are Armstrong numbers in a given base. This is known as a brute force search.
An Armstrong number is any number in base b with n digits such that the sum of each of the digits raised to the power of n is the number itself. Thus 1634 is an Armstrong number because it has 4 digits and (1^4) + (6^4) + (3^4) + (4^4) = 1 + 1296 + 81 + 256 = 1634. Armstrong numbers are dependant upon the base of the number. Thus the number 8 in base 3 is 223, and 2^2 + 2^2 = 4 + 4 = 8. Therefore 223 is an Armstrong number (or, to put it another way, 8 is an Amstrong number in base 3).
The first thing to determine is how many digits there are in a given number for a given base. For instance, the number 3 in base 10 (decimal) has only 1 digit (3), but in base 3 it has 2 digits (103). The number of digits is determined by continually dividing the decimal value of the number by the base until the result is zero (ignoring the remainder of each division). The number of divisions yields the number of digits in that base. Thus 1710 requires 2 divisions: 17/10 = 1, 1/10 = 0, while 1223 (17 decimal) requires 3 divisions: 17/3 = 5, 5/3 = 1, 1/3 = 0. Note that the division is always done on the base 10 value because the denominator (the base) is always a decimal value.
The next problem is to determine the value of any digit at any position in a given number for a given base. The units position is always designated position 0 so for any position greater than 0, we divide the number by the given base raised to the power of the given position. This effectively moves the digit that we are interested in to the units position (position 0). We then divide this new number by the base and take the remainder, which is the digit we are actually looking for. Thus to find the digit in position 2 of the value 3412 decimal, we raise the base by a power of the position, thus 10^2 = 100. We then divide that into 3412 which yields 34 (ignoring the remainder). We then divide by the base and take the remainder. Thus 34/10 = 3 remainder 4. The digit 4 is therefore in position 2.
Once we have these two functions in place, the rest is easy. Given a base, we simply loop through a sequence of numbers. On each iteration we determine the number of digits in the current number, then sum each of the digits in that number raised to the power of the number of digits. If the sum is the same as the number, we've found an Armstrong number and print it. Otherwise we move onto the next number.
The following program will locate all the decimal Armstrong numbers in the range 0 through 268,435,455 (locating the first 29 of the 88 Armstrong numbers). You may change the base to any value in the range 2 through 16 to look at other bases. Note that in base 2, only the values 0 and 1 are Armstrong numbers.
The PrintValue() function is an extra function for dealing with bases other than decimal. It simply prints any decimal value according to the given base. If you want to cater for other bases beyond 16, you must modify the switch statement to cater for all the symbols used by those bases, and adjust the if() statement at the beginning of the function to increase the range.
I've also included a 64-bit unsigned integer definition to cater for huge numbers. If you increase the maximum to 0xffffffffffffffff the program will take many hours to complete. The larger the number, the more calculations that need to be done, thus the time to completion will rise exponentially. Back in 1985, it took about a year to calculate all the Armstrong numbers in base 16.
Note that GetDigit() and GetDigitCount() have been inline expanded within the main() function to improve the overall processing speed. These functions are only actually used by the PrintValue() function when a non-decimal Armstrong number is discovered. The main function is verbosely commented for clarity.
#include <iostream>
typedef unsigned long UINT;
typedef unsigned long long UINT64;
UINT GetDigitCount( UINT64 n, UINT base = 10 )
{
UINT count;
count = 0;
while( n )
{
n /= base;
++count;
}
return( count );
}
UINT GetDigit( UINT64 n, UINT pos, UINT base = 10 )
{
UINT64 div;
div = base;
if( pos )
{
while( --pos )
div *= base;
n /= div;
}
return( n % base );
}
void PrintValue( UINT64 n, UINT base = 10 )
{
UINT digits, digit, pos;
if( base < 2 base > 16 )
return;
if( n )
digits = GetDigitCount( n, base );
else
digits = 1;
for( pos = digits; pos; --pos )
{
digit = GetDigit( n, pos-1, base );
if( digit < 10 )
printf( "%u", digit );
else
{
switch( digit )
{
case( 10 ): printf( "a" ); break;
case( 11 ): printf( "b" ); break;
case( 12 ): printf( "c" ); break;
case( 13 ): printf( "d" ); break;
case( 14 ): printf( "e" ); break;
case( 15 ): printf( "f" ); break;
}
}
}
}
int main()
{
// Constants:
const UINT64 max = 0xfffffff; // maximum possible value is 0xffffffffffffffff
const UINT base = 10;
// Local variables:
UINT64 number, value, accumulator, digit_raised;
UINT digit, digits, pos, raise, count;
// Print the banner:
printf( "Locating base %u Armstrong numbers in the range 0 through %I64u\n\n", base, max );
// Initialise the count:
count = 0;
// Loop through each value in the range.
for( number = 0; number < max; ++number )
{
// Store the value locally.
value = number;
// Determine the number of digits in the value.
digits = 0;
while( value )
{
++digits;
// Reduce the value by an order of the base (ignore the remainder).
value /= base;
}
// Restore the original value.
value = number;
// Initialise the accumulator.
accumulator = 0;
// Loop through each digit position while the accumulator is in range.
for( pos = 0; pos < digits && accumulator < number; ++pos )
{
// Extract the unit digit (right-most digit).
digit = value % base;
// Raise the unit digit by the power of the count of digits.
digit_raised = digit;
for( raise = 1; raise < digits; ++raise )
digit_raised *= digit;
// Update the accumulator.
accumulator += digit_raised;
// Reduce the value by an order of the base (ignore the remainder).
value /= base;
}
// Were all digits processed and does the accumulator equal the original number?
if( pos number )
{
// Found an Armstrong number!
printf( "Armstrong number %.2I64u: ", ++count );
if( base != 10 )
{
PrintValue( number, base );
printf( " (%I64u)\n", number );
}
else
printf( "%I64u\n", number );
}
}
printf( "\n" );
return( 0 );
}
Output:
Locating base 10 Armstrong numbers in the range 0 through 268435455
Armstrong number 01: 0
Armstrong number 02: 1
Armstrong number 03: 2
Armstrong number 04: 3
Armstrong number 05: 4
Armstrong number 06: 5
Armstrong number 07: 6
Armstrong number 08: 7
Armstrong number 09: 8
Armstrong number 10: 9
Armstrong number 11: 153
Armstrong number 12: 370
Armstrong number 13: 371
Armstrong number 14: 407
Armstrong number 15: 1634
Armstrong number 16: 8208
Armstrong number 17: 9474
Armstrong number 18: 54748
Armstrong number 19: 92727
Armstrong number 20: 93084
Armstrong number 21: 548834
Armstrong number 22: 1741725
Armstrong number 23: 4210818
Armstrong number 24: 9800817
Armstrong number 25: 9926315
Armstrong number 26: 24678050
Armstrong number 27: 24678051
Armstrong number 28: 88593477
Armstrong number 29: 146511208
#include <stdio.h>
#include <math.h>
void main()
{
int number, sum = 0, rem = 0, cube = 0, temp;
printf ("enter a number");
scanf("%d", &number);
temp = number;
while (number != 0)
{
rem = number % 10;
cube = pow(rem, 3);
sum = sum + cube;
number = number / 10;
}
if (sum == temp)
printf ("The given no is armstrong no");
else
printf ("The given no is not a armstrong no");
}
#include <iostream>
#include <math.h> // for std::pow()
unsigned int get_length(unsigned int num,const unsigned int base=10)
{
unsigned int len=1;
while(num && (num/=base))
++len;
return( len );
}
bool is_armstrong(const unsigned int num,const unsigned int base=10)
{
unsigned int len=get_length(num,base);
unsigned int sum=0;
unsigned int tmp=num;
while(tmp)
{
sum+=(unsigned int)std::pow((double)(tmp%base),(double)len);
tmp/=base;
}
return(num==sum);
}
int main()
{
std::cout << "Armstrong series (base 10):";
for(unsigned int num=0; num<=0xffffffff; ++num)
if(is_armstrong(num))
std::cout << " " << num;
std::cout << std::endl;
return(0);
}
#include <iostream>
#include <math.h> // for std::pow()
unsigned int get_length(unsigned int num,const unsigned int base=10)
{
unsigned int len=1;
while(num && (num/=base))
++len;
return( len );
}
bool is_armstrong(const unsigned int num,const unsigned int base=10)
{
unsigned int len=get_length(num,base);
unsigned int sum=0;
unsigned int tmp=num;
while(tmp)
{
sum+=(unsigned int)std::pow((double)(tmp%base),(double)len);
tmp/=base;
}
return(num==sum);
}
int main()
{
std::cout << "Armstrong series (base 10):";
for(unsigned int num=0; num<=0xffffffff; ++num)
if(is_armstrong(num))
std::cout << " " << num;
std::cout << std::endl;
return(0);
}
This is not a question, it is a request for someone to do your homework.
import java.io.*; public class Jerry { public static void main(String as[]) throws IOException { int k=Integer.parseInt(as[0]); int n=k; int d=0,s=0; while(n>0) { d=n%10; s=s+(d*d*d); n=n/10; } if(k==s) System.out.println("Armstrong number"); else System.out.println("not Armstrong number"); } }
write a c++ program to convert binary number to decimal number by using while statement
write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program
how to write a program that counts automorphic number from 1 to 999
To write a C program to determine if something is odd or even you need to be a programmer. To write a program in C is complicate and only done by programmers.
import java.io.*; public class Jerry { public static void main(String as[]) throws IOException { int k=Integer.parseInt(as[0]); int n=k; int d=0,s=0; while(n>0) { d=n%10; s=s+(d*d*d); n=n/10; } if(k==s) System.out.println("Armstrong number"); else System.out.println("not Armstrong number"); } }
Write a program to convert a 2-digit BCD number into hexadecimal
import java.io.*; class Armstrong { public static void main()throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a number"); int a=Integer.parseInt(in.readLine()); int n1=a,rev=0,d=0; while(a!=0) { d=a%10; rev=rev+d*d*d; a=a/10; } if(rev==n1) System.out.println("It is a armstrong number "); else System.out.println("It is not a armstrong number "); } }
WRITE A PROGRAM TO CONVERT A 2-DIGIT bcd NUMBER INTO HEXADECIMAL
Write a program which takes any number of days from the user. the program should display the number of years, number of months formed by these days as well as the remaining days.
write a c++ program to convert binary number to decimal number by using while statement
write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program
Steven Armstrong = ستيفن ارمسترونغ
write a c++program by using if statement to read a number and check whether it is positive or negative
pongada punda vayanungala ..................
how to write a program that counts automorphic number from 1 to 999
Write a function that implements an algorithm that checks to see if a particular integer is prime (returning a boolean). Write a program that uses that function on each number from 1 to 100, and if true, displays that number.