Write a program to find whether the number is Armstrong number.
Example:
Input: 153 Output: Number is Armstrong
Approach
C
#include <stdio.h>#include <math.h>int main(){int num;printf("Enter a number: ");scanf("%d", &num);int n = num, sum = 0;while (n > 0){int temp = n % 10;sum += pow(temp, 3);n = n / 10;}if (sum == num)printf("Number is Armstrong ");elseprintf("Number is not Armstrong ");return 0;}
Java
import java.util.Scanner;public class ArmstrongNumber {public static void main(String[] args) {System.out.println("Enter the number");Scanner sc = new Scanner(System.in);int num = sc.nextInt();int n = num, sum = 0;while (n > 0) {int temp = n % 10;sum += Math.pow(temp, 3);n = n / 10;}if (sum == num)System.out.println("Number is Armstrong ");elseSystem.out.println("Number is not Armstrong ");sc.close();}}
Related posts
- Write a program to print all Armstrong numbers 1 To N.
- Write a program to check number is Armstrong or not
No comments:
Post a Comment