public class Armstrong1to1000 {
public static void main(String[] args) {
System.out.println("Amstrong Numbers From 1 to 1000");
int a,r;
for(int i=1; i<=1000;i++)
{
a=0;
int n=i;
while(n!=0)
{
r=n%10;
a=a+r*r*r;
n=n/10;
}
if(a==i)
{
System.out.println(i);
}
}
}
}
OUTPUT:
Amstrong Numbers From 1 to 1000
1
153
370
371
407
Basic Java programs. All programs done in very simple ways. It helps beginners to grow their programming skills and build a programming logic.
Friday, 11 December 2020
Armstrong numbers from 1 to 1000
Armstrong number using while loop
import java.util.*;
public class Armstrong {
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;
while(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 1:
Enter a Number:
591
591 is not an amstrong number
OUTPUT 2:
Enter a Number:
370
370 is an amstrong number
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
Armstrong number using Method
import java.util.Scanner;
public class ArmstrongMethod
{
void Amg(int n)
{
int reminder,amstrong=0,temp=n;
while(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");
}
public static void main(String[] args)
{
AmstrongMethod obj = new AmstrongMethod();
Scanner scan = new Scanner(System.in);
System.out.println("\nEnter a Number: ");
int n = scan.nextInt();
obj.Amg(n);
}
}
OUTPUT:
Enter a Number:
591
591 is not an amstrong number
OUTPUT 2:
Enter a Number:
370
370 is an amstrong number
Monday, 7 December 2020
Factorial of Number without User Interaction
public class FactorialWithoutUserInteraction
{
public static void main(String[] args)
{
int fact=1;
for(int i=1;i<=8;i++)
{
fact=fact*i;
}
System.out.println("\n Factorial of 8 is = "+fact);
}
}
Output:
Factorial of 8 is = 40320
Factorial of Number using Method
import java.util.*;
public class FactMethod
{
void fact(int n)
{
int fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("\n Factorial of "+n+" is = "+fact);
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("\n Enter Number...");
int number = s.nextInt();
FactMethod obj = new FactMethod();
obj.fact(number);
}
}
Output:
Enter Number...
5
Factorial of 5 is = 120
Factorial of Number using Do-While Loop
import java.util.*;
public class FactDoWhile
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("\n Enter Number...");
int number = s.nextInt();
int fact=1,i=1;
do
{
fact=fact*i;
i++;
}while(i<=number);
System.out.println("\n Factorial of "+number+" is = "+fact);
}
}
Output:
Enter Number...
6
Factorial of 6 is = 720
Factorial of Number using While Loop
import java.util.*;
public class FactorialWhile
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("\n Enter Number...");
int number = s.nextInt();
int fact=1,i=1;
while(i<=number)
{
fact=fact*i;
i++;
}
System.out.println("\n Factorial of "+number+" is = "+fact);
}
}
Output:
Enter Number...
7
Factorial of 7 is = 5040
Factorial of Number using Recursion
import java.util.*;
public class FactorialRecursion
{
int fact(int n)
{
if(n==0)
return 1;
else
return(n*fact(n-1));
}
public static void main(String[] args)
{
FactorialRecursion obj = new FactorialRecursion();
Scanner s = new Scanner(System.in);
System.out.println("\n Enter Number...");
int number = s.nextInt();
int fact=1;
fact = obj.fact(number);
System.out.println("\n Factorial of "+number+" is = "+fact);
}
}
Output:
Enter Number...
4
Factorial of 4 is = 24
Factorial of Number using For Loop
import java.util.*;
public class Factotial
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("\n Enter Number...");
int number = s.nextInt();
int fact=1;
for(int i=1;i<=number;i++)
{
fact=fact*i;
}
System.out.println("\n Factorial of "+number+" is = "+fact);
}
}
Output:
Enter Number...
3
Factorial of 3 is = 6
Subscribe to:
Comments (Atom)









