0% found this document useful (0 votes)
4 views18 pages

Ix, Java Codings

The document contains Java code examples for various number-related programs, including calculating factorials, reversing numbers, summing digits, checking for Armstrong and prime numbers, and identifying special numbers like Neon, Strong, and Duck numbers. Each section includes a brief description of the program's purpose, the code itself, and a variable data type table (VDT) summarizing the variables used. The programs utilize basic control structures and input/output operations to demonstrate fundamental programming concepts.

Uploaded by

lichula42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

Ix, Java Codings

The document contains Java code examples for various number-related programs, including calculating factorials, reversing numbers, summing digits, checking for Armstrong and prime numbers, and identifying special numbers like Neon, Strong, and Duck numbers. Each section includes a brief description of the program's purpose, the code itself, and a variable data type table (VDT) summarizing the variables used. The programs utilize basic control structures and input/output operations to demonstrate fundamental programming concepts.

Uploaded by

lichula42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

11.

Factorial of a Number

import java.util.Scanner;

class Factorial {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = sc.nextInt();

int fact = 1;

for (int i = 1; i <= n; i++) {

fact *= i;

System.out.println("Factorial = " + fact);

VDT:

Variable Data Type Description

n int Input number

fact int Factorial result

i int Loop counter

12. Reverse of a Number

import java.util.Scanner;

class ReverseNumber {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = sc.nextInt();

int rev = 0;

for (int i = n; i > 0; i = i / 10) {

int d = i % 10;
rev = rev * 10 + d;

System.out.println("Reversed Number = " + rev);

VDT:

Variable Data Type Description

n int Input number

rev int Reversed number

i int Loop variable

d int Digit extracted

13. Sum of Digits

import java.util.Scanner;

class SumDigits {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = sc.nextInt();

int sum = 0;

for (int i = n; i > 0; i = i / 10) {

int d = i % 10;

sum += d;

System.out.println("Sum of digits = " + sum);

VDT:

Variable Data Type Description

n int Input number


sum int Sum of digits

i int Loop variable

d int Extracted digit

14. Armstrong Number (153 = 1³+5³+3³)

import java.util.Scanner;

class Armstrong {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = sc.nextInt();

int sum = 0, temp = n;

for (int i = n; i > 0; i = i / 10) {

int d = i % 10;

sum += d * d * d;

if (sum == temp)

System.out.println(n + " is an Armstrong number.");

else

System.out.println(n + " is NOT an Armstrong number.");

VDT:

Variable Data Type Description

n int Input number

sum int Sum of cubes of digits

temp int Copy of original number

i int Loop variable

d int Extracted digit


15. Prime Number Check

import java.util.Scanner;

class PrimeCheck {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = sc.nextInt();

int count = 0;

for (int i = 1; i <= n; i++) {

if (n % i == 0) count++;

if (count == 2)

System.out.println(n + " is Prime.");

else

System.out.println(n + " is NOT Prime.");

VDT:

Variable Data Type Description

n int Input number

count int Counts number of divisors

i int Loop counter

16. Multiplication Table

import java.util.Scanner;

class Table {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = sc.nextInt();
for (int i = 1; i <= 10; i++) {

System.out.println(n + " x " + i + " = " + (n * i));

VDT:

Variable Data Type Description

n int Input number

i int Loop counter

17. Even Numbers up to N

import java.util.Scanner;

class EvenNumbers {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter limit: ");

int n = sc.nextInt();

for (int i = 2; i <= n; i += 2) {

System.out.print(i + " ");

VDT:

Variable Data Type Description

n int Upper limit

i int Loop counter

18. Odd Numbers up to N

import java.util.Scanner;
class OddNumbers {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter limit: ");

int n = sc.nextInt();

for (int i = 1; i <= n; i += 2) {

System.out.print(i + " ");

VDT:

Variable Data Type Description

n int Upper limit

i int Loop counter

19. Sum of Even Digits of a Number

import java.util.Scanner;

class SumEvenDigits {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = sc.nextInt();

int sum = 0;

for (int i = n; i > 0; i = i / 10) {

int d = i % 10;

if (d % 2 == 0)

sum += d;

System.out.println("Sum of even digits = " + sum);

}
}

VDT:

Variable Data Type Description

n int Input number

sum int Sum of even digits

i int Loop variable

d int Extracted digit

20. Greatest of Two Numbers

import java.util.Scanner;

class GreatestTwo {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter first number: ");

int a = sc.nextInt();

System.out.print("Enter second number: ");

int b = sc.nextInt();

if (a > b)

System.out.println("Greatest = " + a);

else

System.out.println("Greatest = " + b);

VDT:

Variable Data Type Description

a int First number

b int Second number

21: Neon Number

Definition:
A number is called a Neon Number if the sum of digits of its square is equal to the
number itself.

Example: 9 → 9² = 81 → 8 + 1 = 9 (Neon Number)

import java.util.Scanner;

class NeonNumber {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

int n, sq, sum = 0, d;

System.out.print("Enter a number: ");

n = sc.nextInt();

sq = n * n;

for(; sq > 0; sq = sq / 10) {

d = sq % 10;

sum = sum + d;

if(sum == n)

System.out.println(n + " is a Neon Number.");

else

System.out.println(n + " is not a Neon Number.");

VDT

Variable Data Type Description

n int Input number

sq int Square of the number

sum int Sum of digits of square

d int Extracted digit


22: Armstrong Number

Definition:

A number is called an Armstrong Number if the sum of cubes of its digits is equal to
the number itself.

Example: 153 → 1³ + 5³ + 3³ = 153 (Armstrong Number)

import java.util.Scanner;

class Armstrong {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

int n, temp, d, sum = 0;

System.out.print("Enter a number: ");

n = sc.nextInt();

temp = n;

for(; n > 0; n = n / 10) {

d = n % 10;

sum = sum + (d * d * d);

if(sum == temp)

System.out.println(temp + " is an Armstrong Number.");

else

System.out.println(temp + " is not an Armstrong Number.");

VDT

Variable Data Type Description

n int Input number

temp int Copy of number

d int Extracted digit

sum int Sum of cubes of digits


23: Strong Number

Definition:

A number is called a Strong Number if the sum of factorials of its digits is equal to the
number itself.

Example: 145 → 1! + 4! + 5! = 145 (Strong Number)

import java.util.Scanner;

class StrongNumber {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

int n, temp, d, fact, i, sum = 0;

System.out.print("Enter a number: ");

n = sc.nextInt();

temp = n;

for(; n > 0; n = n / 10) {

d = n % 10;

fact = 1;

for(i = 1; i <= d; i++) {

fact = fact * i;

sum = sum + fact;

if(sum == temp)

System.out.println(temp + " is a Strong Number.");

else

System.out.println(temp + " is not a Strong Number.");

}
VDT

Variable Data Type Description

n int Input number

temp int Copy of number

d int Extracted digit

fact int Factorial of digit

i int Loop counter

sum int Sum of factorials

24: Harshad Number

Definition:

A number is called a Harshad Number (or Niven Number) if it is divisible by the sum of
its digits.

Example: 18 → (1 + 8) = 9 → 18 ÷ 9 = 2 (Harshad Number)

import java.util.Scanner;

class Harshad {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

int n, temp, d, sum = 0;

System.out.print("Enter a number: ");

n = sc.nextInt();

temp = n;

for(; n > 0; n = n / 10) {

d = n % 10;

sum = sum + d;

if(temp % sum == 0)

System.out.println(temp + " is a Harshad Number.");

else

System.out.println(temp + " is not a Harshad Number.");


}

VDT

Variable Data Type Description

n int Input number

temp int Copy of number

d int Extracted digit

sum int Sum of digits

25: Automorphic Number

Definition:

A number is called an Automorphic Number if its square ends with the number itself.

Example: 25 → 25² = 625 (ends with 25 → Automorphic Number)

import java.util.Scanner;

class Automorphic {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

int n, sq, digits = 0, div, temp;

System.out.print("Enter a number: ");

n = sc.nextInt();

sq = n * n;

temp = n;

for(; temp > 0; temp = temp / 10) {

digits++;

div = 1;

for(int i = 1; i <= digits; i++) {

div = div * 10;

if(sq % div == n)
System.out.println(n + " is an Automorphic Number.");

else

System.out.println(n + " is not an Automorphic Number.");

VDT

Variable Data Type Description

n int Input number

sq int Square of number

digits int Number of digits

div int Divisor (10^digits)

temp int Copy of number

26: Spy Number

Definition:

A Spy Number is one where the sum of digits = product of digits.

Example: 1124 → Sum = 1+1+2+4 = 8, Product = 1×1×2×4 = 8 → Spy Number

import java.util.Scanner;

class SpyNumber {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

int n, d, sum = 0, prod = 1;

System.out.print("Enter a number: ");

n = sc.nextInt();

int temp = n;

for(; n > 0; n = n / 10) {

d = n % 10;

sum = sum + d;

prod = prod * d;

}
if(sum == prod)

System.out.println(temp + " is a Spy Number.");

else

System.out.println(temp + " is not a Spy Number.");

VDT

Variable Data Type Description

n int Input number

temp int Copy of number

d int Extracted digit

sum int Sum of digits

prod int Product of digits

27: Kaprekar Number

Definition:

A Kaprekar Number is one where the square of the number can be split into two parts,
and their sum equals the number.

Example: 45 → 45² = 2025 → 20 + 25 = 45 → Kaprekar

import java.util.Scanner;

class KaprekarNumber {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

int n;

System.out.print("Enter a number: ");

n = sc.nextInt();

int sq = n * n;

int count = 0;

for(int i = sq; i > 0; i = i / 10) {


count++;

int pow = 1;

for(int i = 1; i <= count / 2; i++) {

pow = pow * 10;

int right = sq % pow;

int left = sq / pow;

if(right + left == n)

System.out.println(n + " is a Kaprekar Number.");

else

System.out.println(n + " is not a Kaprekar Number.");

VDT

Variable Data Type Description

n int Input number

sq int Square of number

count int Number of digits in square

pow int Power of 10 divider

right int Right part of square

left int Left part of squar

28: Disarium Number

Definition:

A Disarium Number is one where the sum of its digits powered with their position =
number itself.

Example: 135 → 1¹ + 3² + 5³ = 135 → Disarium

import java.util.Scanner;
class DisariumNumber {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

int n, d, count = 0;

System.out.print("Enter a number: ");

n = sc.nextInt();

int temp = n;

// count digits

for(int i = n; i > 0; i = i / 10) {

count++;

int sum = 0;

for(int i = n; i > 0; i = i / 10) {

d = i % 10;

int pow = 1;

for(int j = 1; j <= count; j++) { // careful: single loop per stage

pow = pow * d;

sum = sum + pow;

count--;

if(sum == temp)

System.out.println(temp + " is a Disarium Number.");

else

System.out.println(temp + " is not a Disarium Number.");

VDT
Variable Data Type Description

n int Input number

temp int Copy of number

d int Extracted digit

count int Number of digits and position counter

pow int Digit raised to position

sum int Sum of powered digits

29: Duck Number

Definition:

A Duck Number contains zero(s) in it, but not at the beginning.

Example: 507 → Duck Number, but 0123 → Not Duck

import java.util.Scanner;

class DuckNumber {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

String num = sc.next(); // take as string for easy check

if(num.charAt(0) == '0') {

System.out.println(num + " is not a Duck Number.");

} else if(num.contains("0")) {

System.out.println(num + " is a Duck Number.");

} else {

System.out.println(num + " is not a Duck Number.");

VDT

Variable Data Type Description


num String Input number as string

30: Buzz Number

Definition:

A Buzz Number is one which is divisible by 7 or ends with digit 7.

Example: 49 → divisible by 7 → Buzz Number

import java.util.Scanner;

class BuzzNumber {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

int n;

System.out.print("Enter a number: ");

n = sc.nextInt();

if(n % 7 == 0 || n % 10 == 7)

System.out.println(n + " is a Buzz Number.");

else

System.out.println(n + " is not a Buzz Number.");

VDT

Variable Data Type Description

n int Input number

You might also like