Friday, 11 December 2020

Armstrong number using For loop

import java.util.Scanner;
public class ArmstrongFor {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("\nEnter a Number: ");
		int n = scan.nextInt();
		int reminder,amstrong=0,temp=n;
			for(;n>0;)			
			{
				reminder=n%10;
				amstrong=amstrong+reminder*reminder*reminder;
				n=n/10;
			}
		if(temp==amstrong)
		{
			System.out.println("\n"+temp+" is an amstrong number");
		}
		else
		{
			System.out.println("\n"+temp+" is not an amstrong number");
		}
	}
}

OUTPUT:

Enter a Number: 
591

591 is not an amstrong number

OUTPUT 2:

Enter a Number: 
370

370 is an amstrong number

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32