Write a program to print all the Armstrong number between 100 to 500
Example:
Input: lower = 100, upper = 500 Output: Armstrong numbers are 153 370 371 407
Approach
C
#include <stdio.h>#include <math.h>int main(){printf("Armstrong numbers are ");for (int i = 100; i <= 500; i++){int num = i;int n = i;int sum = 0;while (n > 0){int temp = n % 10;sum += pow(temp, 3);n = n / 10;}if (sum == num)printf("%d ", i);}}
Java
import java.util.Scanner;import java.lang.Math;public class Armstrong{public static void main(String[] args) {for(int i=100;i<500;i++){int num =i;int n=i;int sum = 0;while (n > 0){int temp = n % 10;sum += Math.pow(temp, 3);n = n / 10;}if (sum == num){System.out.print(i+" ");}}}}
No comments:
Post a Comment