Programme for BUZZ Number
import java.util.Scanner;
class Main {
static boolean checkBuzz(int num)
{
if(num % 10 == 7 || num % 7 == 0)
return true;
else
return false;
}
public static void main(String args[])
{
int n;
Scanner sc=new Scanner(System.in);
n = sc.nextInt();
if (checkBuzz(n))
System.out.println(n + " is a Buzz number");
else
System.out.println(n + " is not a Buzz number");
}
}
Steps to Check Automorphic Number in Java
1. Input a number (num).
2. Square the number (sqr).
3. Count the number of digits of (num) using while loop (c).
4. Compare the last (c) digits of (sqr) with the (num).
5. If they are equal then the number is Automorphic else not.
Check Whether a number is an Automorphic Number in Java
import java.util.Scanner;
public class Automorphic {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Enter a number");
int num = in.nextInt();
int c=0, sqr = num*num;
int temp =num; //copying num
//countint digits of num
while(temp>0){
c++;
temp=temp/10;
}
int lastSquareDigits = (int) (sqr%(Math.pow(10,c)));
if(num == lastSquareDigits)
System.out.println("Automorphic number");
else
System.out.println("Not an Automorphic number");
}
}
Write a menu driven class to accept a number from the user and check
whether it is a Palindrome or a Perfect number.
import java.util.Scanner;
public class KboatPalinOrPerfect
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Palindrome number");
System.out.println("2. Perfect number");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter number: ");
int num = in.nextInt();
switch (choice) {
case 1:
int copyNum = num;
int revNum = 0;
while(copyNum != 0) {
int digit = copyNum % 10;
copyNum /= 10;
revNum = revNum * 10 + digit;
}
if (revNum == num)
System.out.println(num + " is palindrome");
else
System.out.println(num + " is not palindrome");
break;
case 2:
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
if (num == sum)
System.out.println(num + " is a perfect number");
else
System.out.println(num + " is not a perfect number");
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
Write a menu driven program to accept a number from the user and check
whether it is a Prime number or an Automorphic number.
import java.util.Scanner;
public class KboatPrimeAutomorphic
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Prime number");
System.out.println("2. Automorphic number");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter number: ");
int num = in.nextInt();
switch (choice) {
case 1:
int c = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
c++;
}
}
if (c == 2)
System.out.println(num + " is Prime");
else
System.out.println(num + " is not Prime");
break;
case 2:
int numCopy = num;
int sq = num * num;
int d = 0;
/*
* Count the number of
* digits in num
*/
while(num > 0) {
d++;
num /= 10;
}
/*
* Extract the last d digits
* from square of num
*/
int ld = (int)(sq % Math.pow(10, d));
if (ld == numCopy)
System.out.println(numCopy + " is automorphic");
else
System.out.println(numCopy + " is not automorphic");
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
ArmstrongNumberExample2.java
1. import java.util.Scanner;
2. import java.lang.Math;
3. public class ArmstsrongNumberExample2
4. {
5. //function to check if the number is Armstrong or not
6. static boolean isArmstrong(int n)
7. {
8. int temp, digits=0, last=0, sum=0;
9. //assigning n into a temp variable
10. temp=n;
11. //loop execute until the condition becomes false
12. while(temp>0)
13. {
14. temp = temp/10;
15. digits++;
16. }
17. temp = n;
18. while(temp>0)
19. {
20. //determines the last digit from the number
21. last = temp % 10;
22. //calculates the power of a number up to digit times and add the resultant to the su
m variable
23. sum += (Math.pow(last, digits));
24. //removes the last digit
25. temp = temp/10;
26. }
27. //compares the sum with n
28. if(n==sum)
29. //returns if sum and n are equal
30. return true;
31. //returns false if sum and n are not equal
32. else return false;
33. }
34. //driver code
35. public static void main(String args[])
36. {
37. int num;
38. Scanner sc= new Scanner(System.in);
39. System.out.print("Enter the number: ");
40. //reads the limit from the user
41. num=sc.nextInt();
42. if(isArmstrong(num))
43. {
44. System.out.print("Armstrong ");
45. }
46. else
47. {
48. System.out.print("Not Armstrong ");
49. }
50. }
51. }