Created
December 25, 2019 05:10
-
-
Save rajendrakumaryadav/ae68fff0f639bd126b191d796a9a93c9 to your computer and use it in GitHub Desktop.
amstrong_gist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int digit_operation(int, int); | |
int main() { | |
unsigned int number = 0, sum = 0, temp_copy = 0, remainder; | |
printf("Enter a integer : "); | |
scanf("%d", &number); | |
temp_copy = number; | |
while (number > 0) { | |
remainder = number % 10; | |
sum = digit_operation(sum, remainder); | |
number = number / 10; | |
} | |
if (temp_copy == sum) { | |
printf("%d = %d. It's an \"Amstrong number.\"\n", temp_copy, sum); | |
} else { | |
printf("%d = %d. It is \"not\" an Amstrong number.\n", temp_copy, sum); | |
} | |
return 0; | |
} | |
int digit_operation(int sum, int remainder) { | |
sum = (sum + (remainder * remainder * remainder)); | |
return sum; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Amstrong { | |
public static void main(String args[]) { | |
int number = 0, remainder, temp_copy, sum = 0; | |
System.out.print("Enter a Integer : "); | |
Scanner scanner = new Scanner(System.in); | |
number = scanner.nextInt(); | |
temp_copy = number; | |
if(number == 0) { | |
System.out.println("Input is zero."); | |
System.exit(0); | |
} | |
while(number > 0) { | |
remainder = number % 10; | |
sum = digit_operation(sum, remainder); | |
number = number / 10; | |
} | |
if(temp_copy == sum) | |
System.out.println(temp_copy +" = "+ sum + ". It's an \"Amstrong number.\""); | |
else | |
System.out.println(temp_copy +" = "+ sum + ". It's \"not\" an Amstrong number."); | |
} | |
// function to calculate | |
private static int digit_operation(int sum, int remainder) { | |
sum = sum + ( remainder * remainder * remainder); | |
return sum; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment