15 Java Programs with Output
Check Armstrong Number
Code:
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int original = num, result = 0, digits = 0;
while (original != 0) {
original /= 10;
digits++;
}
original = num;
while (original != 0) {
int remainder = original % 10;
result += Math.pow(remainder, digits);
original /= 10;
}
if (result == num) {
System.out.println(num + " is an Armstrong number");
} else {
System.out.println(num + " is not an Armstrong number");
}
}
}
Output:
Enter a number: 153
153 is an Armstrong number
Print ASCII Value of a Character
Code:
import java.util.Scanner;
public class ASCIIValue {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = sc.next().charAt(0);
int asciiValue = (int) ch;
System.out.println("ASCII value of " + ch + " is " + asciiValue);
}
}
Output:
Enter a character: A
ASCII value of A is 65