Java Programs
Java Programs
Scenario:
A school teacher is creating a classroom game where students clap once for each number
starting from 1 up to the number of the current round. For example, in round 1 they clap
once, in round 2 they clap twice (1 2), and so on.
🔍 Task:
Write a program to simulate the first 5 rounds of this game. The output should show the
numbers clapped in each round like this:
Output:
import java.util.*;
public class program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(j+" ");
}
System.out.println("");
}
}
}
Input:
12345
1234
123
12
1
Output:
import java.util.*;
public class program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for (int i = n; i>=1; i--) {
for (int j = 1; j <=i; j++) {
System.out.print(j + " ");
}
System.out.println("");
}
}
}
Alphabet Pyramid
Input:3
Output:
AB
ABC
import java.util.*;
int n = input.nextInt();
System.out.print(ch+" ");
}
System.out.println();
Input: 4
Output:
13
135
1357
import java.util.*;
int n = input.nextInt();
System.out.print(odd+" ");
odd+=2;
System.out.println();
Day 2: 22/06/25:
Input:3
Output:
ABC
AB
A
import java.util.*;
int n = input.nextInt();
for (int i =n;i>=1;i--) {
for(char ch ='A';ch<'A'+i;ch++){
System.out.println("");
Input: 3
Output:
BB
CCC
import java.util.*;
int n = input.nextInt();
System.out.println();
Another Method :
import java.util.*;
int n = input.nextInt();
for(int j ='A';j<=ch;j++){
System.out.println("");
}
Right-Angled Number Pyramid
Input:4 Output:
12
123
1234
import java.util.*;
class program{
int n = input.nextInt();
for(int i=1;i<=n;i++){
for(int j=1;j<=n-i;j++){
System.out.print(" ");
for(int k=1;k<=i;k++){
System.out.print(k+" ");
System.out.println();
}
Diagonal 1s in Square
Input:4
Output:
1000
0100
0010
0001
import java.util.*;
class program{
int n = input.nextInt();
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++){
if(i==j){
System.out.print("1 ");
else {
System.out.print("0 ");
System.out.println("");
}
Input:4 Output:
1010
0101
1010
0101
import java.util.*;
class program {
int n = input.nextInt();
if ((i + j) % 2 == 0) {
System.out.print("1 ");
} else {
System.out.print("0 ");
}
}
System.out.println();
Another Method:
import java.util.*;
class program {
int n = input.nextInt();
if (i % 2 != 0) {
if (j % 2 != 0) {
System.out.print("1 ");
} else {
System.out.print("0 ");
else{
if (j % 2 != 0) {
System.out.print("0 ");
} else {
System.out.print("1 ");
System.out.println("");
Hollow Square
Input:4
Output:
****
* *
* *
****
import java.util.*;
class program {
int n = input.nextInt();
for (int i = 1; i <= n; i++) {
System.out.print(" * ");
else {
System.out.print(" ");
System.out.println(" ");
Example if n=4:
import java.util.*;
class program {
int n = input.nextInt();
value--;
System.out.println(" ");
Input:3
Output:
BC
DEF
import java.util.*;
class program {
int n = input.nextInt();
System.out.print(ch+" ");
ch++;
}
System.out.println(" ");
Pascal’s Triangle
🧠 What is Pascal’s Triangle?
Each number is the sum of the two numbers directly above it.
The triangle starts with 1 at the top, and each row grows with symmetry.
Sample Input:
n=5
Expected Output:
11
121
1331
14641
import java.util.*;
class program {
int n = input.nextInt();
for (int i = 1; i <= n; i++) {
for(int j=0;j<=i;j++){
number = number*(i-j)/(j+1);
System.out.println(" ");
Input: 12345
Output: 5
(Count how many digits are in the given number.)
import java.util.*;
class program {
int n = input.nextInt();
int count=0;
if(n==0){
count=1;
while(n>0){
n=n/10;
count++;
System.out.print(count);
Output:4321
(Reverse the digits using logic, not string conversion.)
import java.util.*;
class program {
int n = input.nextInt();
int rem = 0;
while(n > 0) {
n /= 10;
System.out.print(rem);
}
Check if a number is a palindrome → Input: 121 → Output: Palindrome
Input: 123 → Output: Not a Palindrome
import java.util.*;
class program {
int n = input.nextInt();
int onum = n;
int rem = 0;
if (n < 0) {
} else {
while (n > 0) {
n /= 10;
System.out.println(rem);
if (onum == rem) {
System.out.println("Palindrome");
} else {
System.out.print("Not a palindrome");
}
Display all prime numbers between 1 and N
Input: 10
Output: 2 3 5 7
(Use nested loop: check divisibility up to sqrt(n))
import java.util.*;
class program{
if (i % j == 0) {
isprime = false;
if(isprime){
System.out.print(i+" ");
1x1=1
1x2=2
...
1 x 10 = 10
2x1=2
2x2=4
...
2 x 10 = 20
...
10 x 10 = 100
import java.util.*;
class program{
for(int j=1;j<=10;j++){
}
System.out.println("");
Input: 4
Output:
23
456
7 8 9 10
import java.util.*;
class program{
for(int j=1;j<=i;j++){
System.out.print(ans+" ");
ans++;
}
System.out.println("");
Factorial:
Input: 5
Output:120
class program {
int ans=1;
ans=ans*i;
System.out.print(ans);
}
Using While loop:
import java.util.*;
class program {
int ans=1;
while(num>0){
ans*=num;
num--;
System.out.print(ans);
✅ Example:
✅ Strong Number
145 →
1! + 4! + 5! = 1 + 24 + 120 = 145 →
import java.util.*;
class program {
int sum=0;
while(onum>0){
for(int i=1;i<=digits;i++){
fact*=i;
sum+=fact;
onum/=10;
if(num==sum){
System.out.print("Strong Number");
else{
}
Count the Sum of Digits Until a Single Digit
📝 Input: A number
🔁 Task: Repeatedly sum its digits until a single-digit result is obtained.
📌 Eg: 9875 → 9+8+7+5 = 29 → 2+9 = 11 → 1+1 = 2 → Output = 2
Using Method:
import java.util.Scanner;
class program{
int rems = 0;
rems += digit;
rem /= 10;
return rems;
int rem=0;
while(num>0){
rem +=digits;
num/=10;
if(rem>=10){
int ans = reminder(rem);
if(ans<10) {
System.out.print(ans);
else {
System.out.print(ans2);
else {
System.out.println(rem);
Optimize Method:
import java.util.Scanner;
class program{
temp+=num%10;
num /= 10;
}
num=temp;
System.out.print(num);
📝 Input: A number
🔁 Task: Sum of each digit’s cube = number
📌 Eg: 153 → 1³ + 5³ + 3³ = 153 → Armstrong ✅
import java.util.Scanner;
class program {
int onum=num;
int rem = 0;
rem +=Math.pow(digit,length);
num /= 10;
System.out.println(rem);
if (onum == rem) {
System.out.print("Armstrong Number");
} else {
class program {
int rem=0,count=0;
while(num>0){
int digits=num%10;
rem = digits;
num/=10;
if(rem==0){
count++;
}
System.out.print(count);
📝 Input: A number
🔁 Task: Print its reverse
📌 Eg: 12345 → 54321
import java.util.Scanner;
class program {
int reverse = 0;
num /= 10;
System.out.print(reverse);
class program {
int onum=num;
int rem = 0;
rem += digit;
num /= 10;
if (onum % rem == 0) {
System.out.print("Harsad Number");
} else {
}
Print All Perfect Squares Less Than N
📝 Input: N
🔁 Task: Print all numbers less than N which are perfect squares
📌 Eg: Input: 30 → Output: 1, 4, 9, 16, 25
import java.util.Scanner;
class program {
int ans=0;
for(int i=1;i*i<num;i++){
ans =i*i;
System.out.print(ans+" ");
Sum of Series: 1² + 2² + 3² + … + N²
📝 Input: N
🔁 Task: Print the sum of squares of first N numbers
📌 Eg: Input: 3 → Output: 1 + 4 + 9 = 14
import java.util.Scanner;
class program {
for(int i=1;i<=num;i++){
ans+=i*i;
System.out.println(ans);
📝 Input: N
🔁 Task: Print the first N numbers in Fibonacci sequence
📌 Eg: 0 1 1 2 3 5 8 …
import java.util.Scanner;
class program {
int a=0;
int b=1;
for(int i=0;i<num;i++){
int next=a+b;
a=b;
b=next;
}
}
Day 3: 23/06/25:
class program {
int a = input.nextInt();
int b= input.nextInt();
while(b!=0){
b=a%b;
a=temp;
System.out.print("GCD:"+a);
}
Find LCM of Two Numbers
class program{
int a = input.nextInt();
int b = input.nextInt();
int x=a;
int y=b;
while(b!=0){
int temp=b;
b=a%b;
a=temp;
System.out.println("GCD:"+a);
int gcd=a;
System.out.print("LCM:"+Lcm);
}
Day 4:24/06/25:
👉
1️⃣ Count Even and Odd Digits in a Number
Input: 3456 → Output: Even: 2, Odd: 2
import java.util.*;
class program{
int n = input.nextInt();
int ecount=0,ocount=0;
while(n>0){
n/=10;
if(rem%2==0){
ecount++;
else{
ocount++;
System.out.println(ecount);
System.out.print(ocount);
}
👉
2️⃣ Sum of Digits at Even and Odd Positions
Input: 12345 → Output: Even position sum: 6, Odd: 9 (1st = Odd, 2nd = Even...)
import java.util.*;
class program{
int n = input.nextInt();
int ecount=0,ocount=0;
while(n>0){
n/=10;
if(rem%2==0){
ecount+=rem;
else{
ocount+=rem;
System.out.println(ecount);
System.out.print(ocount);
}
👉
3️⃣ Check if a Number is a Strong Number
👉 ✅
A strong number = sum of factorial of digits equals original number
Eg: 145 → 1! + 4! + 5! = 145
import java.util.*;
class program{
int n = input.nextInt();
int onum=n;
int sum=0;
int rem=0;
while(n>0){
int fact=1;
rem = digit;
for(int i=1;i<=rem;i++){
fact*=i;
sum+=fact;
n/=10;
System.out.println(sum);
if(onum==sum){
System.out.print("Strong Number");
else{
System.out.print("Not a Strong Number");
👉
4️⃣ Product of Digits of a Number
Input: 123 → Output: 6
import java.util.*;
class program{
int n = input.nextInt();
int mul=1;
while(n>0){
mul*=rem;
n/=10;
System.out.print(mul);
👉
5️⃣ Count Prime Digits in a Number
Eg: Input: 23753 → Prime digits = 2, 3, 5, 3 → Count = 4
import java.util.Scanner;
class program {
int n = input.nextInt();
int ans=0;
int count = 0;
while (n > 0) {
if(prime(digit)){
count++;
n /= 10;
System.out.print(count);
if (a % i == 0) {
return false;
return true;
}
👉
6️⃣ Check if Number is a Magic Number
👉 ✅
Sum of digits until single digit = 1
Eg: 199 → 1+9+9 = 19 → 1+9 = 10 → 1+0 = 1
import java.util.Scanner;
class program{
int temp=0;
while (num> 0) {
temp+=num%10;
num /= 10;
num=temp;
System.out.println(num);
if(num==1){
System.out.println("Magic NUmber");
else{
}
👉
7️⃣ Reverse the Number without using Extra Variables
Logic-level test (you can only use 2 variables max)
import java.util.Scanner;
class program{
int n = input.nextInt();
int rem=0;
while(n>0){
rem = (rem*10)+digit;
n/=10;
System.out.print(rem);
👉
8️⃣ Check if a Number is a Neon Number
👉 ✅
Neon = square of the number → sum of digits of square = number
Eg: 9 → 9² = 81 → 8 + 1 = 9
import java.util.Scanner;
class program{
int n= input.nextInt();
int sq = n*n;
int digits=0;
while(sq>0){
digits+=sq%10;
sq/=10;
if(digits==n){
else{
👉
9️⃣ Print All Armstrong Numbers from 1 to n
Input: n = 1000 → Output: 1, 153, 370, 371, 407
import java.util.Scanner;
class program {
int n = input.nextInt();
int sum = 0;
num /= 10;
if (sum == i) {
class program{
int n= input.nextInt();
int onum=n;
int temp =n;
int length=0,mul=0;
while(temp>0){
length++;
temp/=10;
while(n>0){
int digit=0;
digit =n%10;
n/=10;
System.out.println(mul);
if(mul==onum){
System.out.print("Valid Number");
else{
} for 10
Sum of Even Positioned Digits
👉 Input: 123456
👉 Output: Sum of digits in even positions (from right): 2 + 4 + 6 = 12
import java.util.Scanner;
class program {
int n = input.nextInt();
int sum=0;
while(n>0){
if(position%2==0){
sum+=digits;
System.out.println(digits + "-"+position);
n/=10;
position++;
System.out.print(sum);
}
Product of Odd Positioned Digits
👉 Input: 987654
👉 Output: Product of 9 × 7 × 5 = 315
import java.util.Scanner;
class program {
int n = input.nextInt();
while(n>0){
if(position%2!=0){
mul*=digits;
System.out.println(digits + "-"+position);
n/=10;
position--;
System.out.print(mul);
}
Check if a Number is a Duck Number
class program {
int n = input.nextInt();
int onum=n;
while(n>0){
if(digit==0){
ans=true;
n/=10;
if(String.valueOf(onum).charAt(0)=='0'){
else if(ans){
System.out.print("Duck Number");
else{
👉 Input: 124
Digits: 1, 2, 4
→ 124 is divisible by 1, 2, and 4 → Output: 3
import java.util.Scanner;
class program {
int n = input.nextInt();
int count=0;
while(n>0){
count++;
n/=10;
System.out.print(count);
}
}
👉 Input: 125
Digits: 1, 2, 5
→ Only 1 and 5 divide 125 → Output: 1 (2 doesn't divide)
import java.util.Scanner;
class program {
int n = input.nextInt();
int count=0;
while(n>0){
count++;
n/=10;
System.out.print(count);
}
Check if the Number is a Spy Number
class program {
int n = input.nextInt();
while(n>0){
sum+=digit;
mul*=digit;
n/=10;
if(sum==mul){
System.out.print("Spy Number");
else {
}
17. Harshad Numbers from 1 to N
👉 Input: N=50
👉 Print all Harshad (N divisible by sum of its digits) between 1 to 50
import java.util.Scanner;
class program {
int n = input.nextInt();
int temp=i;
int sum = 0;
while (temp>0) {
sum += digit;
temp/=10;
if (i%sum == 0) {
System.out.println(i);
}
Reverse + Add until Palindrome (Bonus Logic)
class program {
int n = input.nextInt();
while (true) {
int onum = n;
int reverse = 0;
onum /= 10;
if (reverse == n) {
System.out.print("Palindrome "+n);
break;
} else {
n =n+reverse;
}
}
👉 Input: 102030
👉 Output: 3
import java.util.Scanner;
class program {
int n = input.nextInt();
int count=0;
while(n>0){
if(digit ==0){
count++;
n/=10;
System.out.print(count);
}
Day 5:25/06/25:
class program {
int n = input.nextInt();
int sq=n*n;
if(sq%last==n){
System.out.println("Automorphoic Number");
else{
Another Method:
import java.util.Scanner;
class program {
public static void main(String[] args) {
int n = input.nextInt();
int sq=n*n;
int mod=1;
int temp=n;
while(temp>0){
mod*=10;
temp/=10;
if(sq%mod==n){
System.out.print("True");
else{
System.out.print("False");
}
Check for Trimorphic Number
👉
A number is called Trimorphic if its cube ends with the number itself.
Input: 24 → 24³ = 13824 → Ends with 24 → Output: Trimorphic
Number
import java.util.Scanner;
class program{
int n =input.nextInt();
if(n<=0){
System.out.print("Invalid Input");
return;
if(sq%digit==n){
else{
}
Count Digits Greater than 5 in a Number
class program{
int n =input.nextInt();
int count=0;
while(n>0){
if(digit>5){
count++;
n/=10;
System.out.print(count);
}
Check if a Number is a Tech Number
A number is a tech number if it has an even number of digits and the square of the
👉
sum of its two halves is equal to the number itself.
Input: 2025 → 20 + 25 = 45 → 45² = 2025 → Output: Tech Number
import java.util.Scanner;
class program{
int n =input.nextInt();
if(length%2!=0){
System.out.print(“Invalid Number”);
return;
int Fh = n/divisor;
int Lh=n%divisor;
int sum=Fh+Lh;
int sq = sum*sum;
if(sq==n){
else{
}}
}
Check if Sum of Even Digits is Divisible by 4
import java.util.Scanner;
class program{
int n = input.nextInt();
int sum=0;
while(n>0){
if(digit%2==0){
sum+=digit;
n/=10;
if(sum%4==0){
System.out.print("Yes");
else{
System.out.print("No");
}
Find Difference Between Product of Even and Odd Digits
class program{
int n = input.nextInt();
int emul=1,omul=1;
while(n>0){
if(digit%2==0){
emul*=digit;
else{
omul*=digit;
n/=10;
System.out.print(sub);
}
Find Sum of Prime Digits Present in a Number
class program {
int n = input.nextInt();
int sum=0;
while(n>0){
if(isPrime(digit)){
sum +=digit;
n/=10;
System.out.print(sum);
}
Count Total Digits Before First 0 Appears
class program {
String n = input.nextLine();
int count = 0;
if (n.charAt(i) == '0') {
break;
count++;
if (count == n.length()) {
} else {
System.out.print(count);
class program{
int n = input.nextInt();
boolean ps = false;
if(i*i==n){
ps =true;
break;
if(ps){
System.out.print("Prefect Square");
else{
class program {
int n = input.nextInt();
double sqrt = Math.sqrt(n);
System.out.print("Perfect Square");
} else {
👉
A number is twisted prime if both the number and its reverse are prime.
Input: 13 → Reverse = 31 → Both prime → Output: Twisted Prime
import java.util.Scanner;
class program{
int n = input.nextInt();
System.out.println(reverse);
System.out.print("Twisted Prime");
else{
}
public static int reverse(int b){
int rev=0;
while(b>0) {
b /= 10;
return rev;
if (a < 2) {
return false;
} else {
if (a % i == 0) {
return false;
return true;
}
Kaprekar Number Check
If a number’s square can be split into two parts that sum to the number, it's a
👉
Kaprekar number.
Input: 45 → 45² = 2025 → 20 + 25 = 45 → Output: Kaprekar Number
import java.util.Scanner;
class program{
int n =input.nextInt();
int sq = n*n;
int Fh = sq/divisor;
int Lh=sq%divisor;
int sum=Fh+Lh;
if(n==sum){
else{
}}
}
Another Method Using reverse:
import java.util.Scanner;
class program {
int n = input.nextInt();
int original = n;
int reverse = 0;
while (n > 0) {
n /= 10;
int count = 0;
if (digit == 0) {
break;
count++;
reverse /= 10;
}
System.out.println("Digits before first 0: " + count);
👉
Count Total Digits Before First Zero Appears (from right side)
Input: 1234056 → Output: 2
(From right: 6, 5, 0 → count = 2 digits before 0 appears)
import java.util.Scanner;
class program {
int n = input.nextInt();
int count = 0;
while (n > 0) {
if (digit==0) {
break;
count++;
n /= 10;
if (count == len) {
}
else{
System.out.print(count);
👉
13. Check if Number is a Perfect Number
A number is perfect if the sum of its proper divisors equals the
👉
number.
👉
Input: 28 → Output: Perfect Number
Input: 12 → Output: Not a Perfect Number
import java.util.Scanner;
class program {
int n = input.nextInt();
int sum=0;
if(n%i==0){
sum+=i;
if(sum==n){
System.out.print("Perfect Number");
}
else {
👉
14. Print Digits in Even Positions (from left to right)
Input: 123456 → Output: 2 4 6
(2nd, 4th, 6th digit)
import java.util.Scanner;
class program {
int n = input.nextInt();
int onum=n;
int rev=0;
while(n>0){
rev=(rev*10)+digit;
n/=10;
System.out.println(rev);
while(rev>0){
int digit =rev%10;
if(rev%2==0){
System.out.print(digit+" ");
rev/=10;
position++;
class program {
String n = input.next();
}
👉
15. Check if Product of Digits is Prime
👉
Input: 235 → Product = 2×3×5 = 30 → Output: Not Prime
👉
Input: 23 → Product = 6 → Output: Not Prime
👉
Input: 37 → Product = 21 → Output: Not Prime
Input: 23 → Output: Not Prime
import java.util.Scanner;
class program {
int n = input.nextInt();
int mul=1;
while(n>0){
mul*=digit;
n/=10;
System.out.println(mul);
if(isPrime(mul)) {
System.out.print("Prime number");
else{
return false;
else{
for(int i=2;i<a/2;i++){
if(a%i==0){
return false;
return true;
👉
16. Print Digits Whose Square is Even
Input: 248159 → Output: 2 4 8
(Squares: 4, 16, 64 → All even)
import java.util.Scanner;
class program {
int n = input.nextInt();
temp /= 10;
}
while(rev>0){
rev/=10;
if(sq%2==0){
System.out.print(digit+" ");
👉
17. Find Number of Digits Which Are Multiple of 3
Input: 3695 → Output: 3
(Digits 3, 6, 9 are multiples of 3)
import java.util.Scanner;
class program {
int n = input.nextInt();
int count=0;
while(n>0){
int digits = n%10;
count++;
n/=10;
System.out.print(count);
👉
18. Find Digits Which Are Same as Position (from left)
👉
Input: 123456 → Output: 1 2 3 4 5 6
Input: 124365 → Output: 1 2 3
(Position starts from 1)
import java.util.Scanner;
class program {
int n = input.nextInt();
temp /= 10;
}
int position =1;
while(rev>0){
if(position==digit){
System.out.print(digit+" ");
rev/=10;
position++;
👉
19. Count Number of Times Highest Digit Appears
Input: 8989234 → Highest digit = 9 → Output: 2
import java.util.Scanner;
class program {
int n = input.nextInt();
int temp = n;
int max=0;
int digit=temp%10;
if(max<digit){
max=digit;
temp /= 10;
System.out.println(max);
int count=0;
while ( n> 0) {
int digit=n%10;
if(max==digit){
count++;
n/= 10;
System.out.print(count);
Another Method :
import java.util.Scanner;
class Program {
int max = 0;
int count = 0;
while (n > 0) {
max = digit;
count++;
n /= 10;
👉
20. Count Consecutive Repeating Digits
Input: 1223334444 → Output: 3
(Repeating groups: 22, 333, 4444 → Total = 3)
Day6: 26/06/25:
👉
the product of digits is odd.
👉
Input: 123 → Sum = 6, Product = 6 → Output: Not Lucky
👉
Input: 124 → Sum = 7, Product = 8 → Output: Not Lucky
Input: 135 → Sum = 9, Product = 15 → Output: Lucky
import java.util.Scanner;
class program {
int n = input.nextInt();
while (n > 0) {
sum += digit;
mul *= digit;
n /= 10;
System.out.print("Lucky Number");
} else {
}}
}
👉
2️⃣. Count Digits Whose Cube is a Multiple of 3
Input: 236 → Cubes: 8, 27, 216 → Count = 2 (3 and 6)
import java.util.Scanner;
class program {
int n = input.nextInt();
int count=0;
while(n>0){
if((int)Math.pow(digit,3)%3==0){
count++;
n/=10;
System.out.print(count);
👉
Right)
👉
Input: 1234 → Output: Yes
Input: 421 → Output: No
import java.util.Scanner;
class program {
while (num>0){
if(curDigit>=prevDigit){
ans=false;
break;
prevDigit=curDigit;
num/=10;
if(ans){
System.out.println("Yes");
else{
System.out.println("No");
}
👉
4️⃣. Find Sum of Digits at Prime Positions (Left to Right)
Input: 123456 → Prime positions: 2, 3, 5 → Digits: 2, 3, 5 →
Sum = 10
import java.util.Scanner;
class program {
int rev=0;
while(num>0){
rev =rev*10+digit;
num/=10;
int sum=0;
while(rev>0){
if(isPrime(position)){
sum+=digit;
position++;
rev/=10;
System.out.println(sum);
}
if(a<2){
return false;
else{
for(int i=2;i<=Math.sqrt(a);i++){
if(a%i==0){
return false;
return true;
👉
5️⃣. Count Number of Digits Which Are Perfect Squares
Input: 239145 → Perfect squares: 1, 4, 9 → Count = 3
import java.util.Scanner;
class program {
int n = input.nextInt();
int count=0;
while(n>0){
if (i * i == digit) {
count++;
System.out.println(digit);
break;
n/=10;
System.out.print("Count: "+count);
👉
6️⃣. Print Digits in Fibonacci Positions (1, 2, 3, 5, 8, ...)
Input: 123456789 → Positions: 1, 2, 3, 5, 8 → Output: 1 2 3 5 8
👉
7️⃣. Check if the Number is an Evil Number
A number is Evil if it has an even number of 1s in its binary
👉
representation.
👉
Input: 9 → Binary = 1001 → Two 1s → Output: Evil
Input: 7 → Binary = 111 → Three 1s → Output: Not Evil
import java.util.Scanner;
class program {
String binary="";
int count=0;
while(num>0){
if(bit==1){
count++;
num/=2;
if(count%2==0){
System.out.println("Evil Number");
else{
}
👉
8️⃣. Find the Difference Between First and Last Digit of a Number
Input: 5832 → First = 5, Last = 2 → Output: 3
import java.util.Scanner;
class program {
System.out.println(digit);
👉
9️⃣. Count the Digits that are Powers of 2 (i.e., 1, 2, 4, 8)
Input: 231486 → Output: 5
import java.util.Scanner;
class program {
while(num>0){
if(digit==1||digit==2||digit==4||digit==8){
count++;
num/=10;
System.out.println(count);
class program {
int rev = 0;
num /= 10;
int pow = 1;
int sum = 0;
rev /= 10;
pow++;
if (onum == sum) {
System.out.println(sum+"-Disarium Number");
else{
}
CONVERTING DECIMAL INTO BINARY:
import java.util.Scanner;
class program {
String binary="";
while(num>0){
num/=2;
System.out.println(binary);
👉
Count the Digits Whose Cube is a Prime Number
Example: Input: 239 → Cubes: 8, 27, 729 → Only 8 is a
prime? → Count = ?
import java.util.Scanner;
class program {
int count=0;
while(num>0){
if(isPrime(cube)){
count++;
num/=10;
System.out.println(count);
if(a<2){
return false;
else{
for(int i=2;i<=a/2;i++){
if(a%i==0){
return false;
}
}
return true;
👉
2️⃣ Check if All Digits Are Same
Example: Input: 5555 → Output: Yes
👉 Example: Input: 525 → Output: No
import java.util.Scanner;
class program {
int ldigit=num%10;
if(digit!=ldigit){
allSame =false;
break;
num /= 10;
if(allSame){
System.out.println("Yes");
}
else{
System.out.println("No");
👉
3️⃣ Find the Maximum Digit at an Even Position (from left)
Input: 235147 → Even positions: 3, 1, 7 → Output: 7
import java.util.Scanner;
class program {
while(num>0){
rev= rev*10+digit;
num/=10;
int max=0;
while(rev>0){
if(position%2==0){
if(max<digit){
max=digit;
rev/=10;
position++;
System.out.println(max);
👉
4️⃣ Check if a Number is a Harshad Number
👉
A number is Harshad if it is divisible by the sum of its digits.
Example: Input: 18 → 1 + 8 = 9 → 18 % 9 == 0 → Output:
Harshad
import java.util.Scanner;
class program {
int sum=0;
while(num>0){
sum+=digit;
num/=10;
}
if(onum%sum==0){
System.out.println("Harsad Number");
else{
👉
7️⃣ Check if Number is Centered Hexagonal
A number is Centered Hexagonal if: N = 3n(n−1)+1 for some
👉
integer n.
Example: 1, 7, 19, 37, 61, …
import java.util.Scanner;
class program {
int ans = 3 * i * (i - 1) + 1;
if (ans == num) {
isHex = true;
break;
if (isHex) {
} else {
👉
9️⃣ Count the Number of 0s in Even Positions (from left)
Input: 102040506 →Even positions: 0s at 2, 4, 6..→ Output: 4
import java.util.Scanner;
class program {
while(num>0){
num/=10;
int count=0;
while(rev>0){
count++;
rev/=10;
position++;
System.out.println(count);
class program {
if(num%7==0 || ldigit==7 ){
System.out.println("Buzz");
else{
System.out.println("Not Buzz");
👉
2️⃣ Find the Second Highest Digit in the Number
Input: 53921 → Digits: 5, 3, 9, 2, 1 → Second highest: 5
import java.util.Scanner;
class program {
while(num>0){
if(digit> max1){
max2=max1;
max1=digit;
}
else if(digit!=max1 && digit>max2){
max2=digit;
num/=10;
System.out.println(max2);
👉
5️⃣ Check if Digits are in Non-Decreasing Order (Left to Right)
Input: 1123456 → Output: Yes
👉 Input: 321 → Output: No
import java.util.Scanner;
class program {
while(num>0){
num/=10;
int max=0;
while(rev>0){
int digit = rev%10;
if(digit>=max){
max=digit;
else{
System.out.print("No");
return;
rev/=10;
System.out.println("Yes");
👉
7️⃣ Check if Sum of First Half Digits = Second Half Digits
Input: 123321 → 1+2+3 == 3+2+1 → Output: Yes
import java.util.Scanner;
class program {
if (len % 2 != 0) {
int lsum=0;
while(lDigit>0){
int digit=lDigit%10;
lsum+=digit;
lDigit/=10;
int fsum=0;
while(fDigit>0){
int digit=fDigit%10;
fsum+=digit;
fDigit/=10;
if(lsum==fsum){
System.out.println("Yes");
else {
System.out.println("No");
}
twin prime upto upr limit, diff b/w the prime should be
2.input:14,output:(3,5),(11,13):
import java.util.*;
class program {
if (n % j == 0) return false;
return true;
}
You have n coins and you want to build a staircase with these coins.
The staircase consists of k rows where the ith row has exactly i coins.
The last row of the staircase may be incomplete.
class program {
int row = 0;
row++;
n -= row;
System.out.println(row);