Skip to content

Instantly share code, notes, and snippets.

@jagdish4501
Created March 30, 2021 09:07
Show Gist options
  • Save jagdish4501/64b41702329747ba5bf431b2e5c69937 to your computer and use it in GitHub Desktop.
Save jagdish4501/64b41702329747ba5bf431b2e5c69937 to your computer and use it in GitHub Desktop.
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 )
//π™’π™šπ™©π™π™€π™™ 1
#include <stdio.h>
#include<math.h>
int main()
{
int num, d1, d2, d3, sum, num2;
//d1 d2 d2 is first,second ,third digit of num
// eg num=34 then d1=0 d2=3 ,d3=4
for (num = 1; num <= 500; num++)
{
d3 = num % 10;
num2 = num / 10;
d2 = num2 % 10;
d1 = num2 / 10;
sum = pow(d1, 3) + pow(d2, 3) + pow(d3, 3);
if (sum == num)
{
printf("%d\n",sum);
}
}
}
----->>>
//π™’π™šπ™©π™π™€π™™ 2
#include <stdio.h>
#include <math.h>
int main()
{
int num, sum = 0, temp, rem, cub;
for (num = 1; num <= 500; num++)
{
temp=num;
while (num != 0)
{
rem = num % 10;
cub = pow(rem, 3);
sum = sum + cub;
num = num / 10;
}
if (sum == temp)
{
printf("%d\n", sum);
}
sum = 0;
num=temp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment