0% found this document useful (0 votes)
0 views102 pages

Java Programs

The document contains a series of programming tasks and solutions in Java, focusing on various patterns, number manipulations, and algorithms. It includes examples such as clapping games, alphabet pyramids, number triangles, and checks for prime, palindrome, and Armstrong numbers. Each task is accompanied by sample input and output, along with the corresponding Java code implementation.

Uploaded by

sanjaym12389
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)
0 views102 pages

Java Programs

The document contains a series of programming tasks and solutions in Java, focusing on various patterns, number manipulations, and algorithms. It includes examples such as clapping games, alphabet pyramids, number triangles, and checks for prime, palindrome, and Armstrong numbers. Each task is accompanied by sample input and output, along with the corresponding Java code implementation.

Uploaded by

sanjaym12389
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/ 102

Day 1: 21/06/25:

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

Print alphabets row-wise starting from 'A'.

Input:3

Output:

AB

ABC
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 (char ch = 'A'; ch<'A'+i;ch++) {

System.out.print(ch+" ");
}

System.out.println();

Odd Number Triangle

A student wants to practice counting odd numbers only. Print the


triangle using odd numbers.

Input: 4

Output:

13

135

1357

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++) {

int odd =1;


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

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

odd+=2;

System.out.println();

Day 2: 22/06/25:

Reverse Alphabet Pyramid

Print a pattern that starts with A to some letter and shrinks.

Input:3

Output:

ABC

AB

A
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(char ch ='A';ch<'A'+i;ch++){

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

System.out.println("");

Letter Repeat Pattern

Print rows like:

Input: 3

Output:

BB

CCC
import java.util.*;

public class LetterRepeatPattern {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

char ch = 'A'; // start with A


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

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

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

ch++; // move to next letter

System.out.println();

Another Method :
import java.util.*;

public class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

for (char ch = 'A';ch< 'A'+n;ch++) {

for(int j ='A';j<=ch;j++){

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

System.out.println("");

}
Right-Angled Number Pyramid

Align numbers to the right:

Input:4 Output:

12

123

1234
import java.util.*;

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<=n-i;j++){

System.out.print(" ");

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

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

System.out.println();

}
Diagonal 1s in Square

Print 1 on diagonals, 0 elsewhere.

Input:4

Output:

1000

0100

0010

0001
import java.util.*;

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<=n;j++){

if(i==j){

System.out.print("1 ");

else {

System.out.print("0 ");

System.out.println("");
}

Checkerboard Pattern (0 & 1)

Alternate 0 and 1 in a grid.

Input:4 Output:
1010

0101

1010

0101
import java.util.*;

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 <= n; j++) {

if ((i + j) % 2 == 0) {

System.out.print("1 ");

} else {

System.out.print("0 ");

}
}

System.out.println();

Another Method:
import java.util.*;

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++) {

if (i % 2 != 0) {

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

if (j % 2 != 0) {

System.out.print("1 ");

} else {

System.out.print("0 ");

else{

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

if (j % 2 != 0) {

System.out.print("0 ");
} else {

System.out.print("1 ");

System.out.println("");

Hollow Square

Print a square with * on border only.

Input:4

Output:

****

* *

* *

****
import java.util.*;

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 <= n; j++) {

if (i==1 || j == 1 || i==n || j==n) {

System.out.print(" * ");

else {

System.out.print(" ");

System.out.println(" ");

Half Pyramid Numbers from n to 1

Example if n=4:

import java.util.*;

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++) {

int value =n;

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


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

value--;

System.out.println(" ");

Alphabet Count Triangle

Show characters increasing like this:

Input:3

Output:

BC

DEF
import java.util.*;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

char ch= 'A';

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

for (int j='A';j<'A'+i;j++) {

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

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

int number =1;

for(int j=0;j<=i;j++){

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

number = number*(i-j)/(j+1);

System.out.println(" ");

Count the digits in a number using while loop

Input: 12345​
Output: 5​
(Count how many digits are in the given number.)
import java.util.*;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int count=0;

if(n==0){

count=1;

while(n>0){

n=n/10;
count++;

System.out.print(count);

Reverse a number using while loop


Input: 1234

Output:4321​
(Reverse the digits using logic, not string conversion.)
import java.util.*;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int rem = 0;

while(n > 0) {

int digit = n%10;

rem = rem*10 + digit;

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int onum = n;

int rem = 0;

if (n < 0) {

System.out.print("Negative Numbers cannot be a palindrome");

} else {

while (n > 0) {

int digit = n % 10;

rem = rem * 10 + digit;

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.*;

import static java.lang.Math.sqrt;

class program{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

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

boolean isprime =true;

for (int j= 2; j <=sqrt(i); j++) {

if (i % j == 0) {

isprime = false;

if(isprime){

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

We can also use j*j<=i for instead of sqrt(i);


Multiplication Table from 1 to 10 (Nested Loop)

✅ What is being asked?


You need to print multiplication tables for numbers from 1 to 10, like:

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{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

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

for(int j=1;j<=10;j++){

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

}
System.out.println("");

Print Floyd’s Triangle using nested loops

Input: 4​
Output:

23

456

7 8 9 10
import java.util.*;

class program{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int ans =1;

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

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

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

ans++;

}
System.out.println("");

Factorial:

Input: 5

Output:120

Using For loop:


import java.util.*;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int ans=1;

for (int i=num;i>=1;i--){

ans=ans*i;

System.out.print(ans);

}
Using While loop:
import java.util.*;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int ans=1;

while(num>0){

ans*=num;

num--;

System.out.print(ans);

Check if a number is a Strong Number in Java 🔥

💡 What is a Strong Number?


A Strong number is a number in which sum of the factorials of its digits equals the
number itself.

✅ Example:
✅ Strong Number
145 →​
1! + 4! + 5! = 1 + 24 + 120 = 145 →
import java.util.*;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int onum =num;

int sum=0;

while(onum>0){

int digits = onum%10;

int fact =1;

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

fact*=i;

sum+=fact;

onum/=10;

if(num==sum){

System.out.print("Strong Number");

else{

System.out.print("Not a Strong number");

}
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{

static int reminder(int rem) {

int rems = 0;

while (rem > 0) {

int digit = rem % 10;

rems += digit;

rem /= 10;

return rems;

public static void main(String[] args){

Scanner input = new Scanner(System.in);

int num =input.nextInt();

int rem=0;

while(num>0){

int digits = num%10;

rem +=digits;

num/=10;

if(rem>=10){
int ans = reminder(rem);

if(ans<10) {

System.out.print(ans);

else {

int ans2 = reminder(ans);

System.out.print(ans2);

else {

System.out.println(rem);

Optimize Method:
import java.util.Scanner;

class program{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

while (num >= 10) {

int temp =0;

while (num > 0) {

temp+=num%10;

num /= 10;
}

num=temp;

System.out.print(num);

Check if a Number is an Armstrong Number

📝 Input: A number​
🔁 Task: Sum of each digit’s cube = number​
📌 Eg: 153 → 1³ + 5³ + 3³ = 153 → Armstrong ✅
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int onum=num;

int length =String.valueOf(num).length();

int rem = 0;

while (num > 0) {

int digit = num % 10;

rem +=Math.pow(digit,length);

num /= 10;

System.out.println(rem);
if (onum == rem) {

System.out.print("Armstrong Number");

} else {

System.out.print("Not Armstrong Number");

Count Total Zeros in a Number

📝 Input: A number like 10502070​


🔁 Task: Count how many zeros it has​
📌 Output: 4
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int rem=0,count=0;

while(num>0){

int digits=num%10;

rem = digits;

num/=10;

if(rem==0){

count++;

}
System.out.print(count);

Reverse a Number Without Using String

📝 Input: A number​
🔁 Task: Print its reverse​
📌 Eg: 12345 → 54321
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int reverse = 0;

while (num > 0) {

int digit = num % 10;

reverse = reverse * 10 + digit;

num /= 10;

System.out.print(reverse);

Check if a Number is Harshad (Niven) Number


📝 Input: A number​
🔁 Task: If the number is divisible by the sum of its digits​
📌 Eg: Input: 18 → 1+8=9 → 18 % 9 == 0 → Harshad ✅
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int onum=num;

int rem = 0;

while (num > 0) {

int digit = num % 10;

rem += digit;

num /= 10;

if (onum % rem == 0) {

System.out.print("Harsad Number");

} else {

System.out.print("Not Harsad Number");

}
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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();


int ans=0;

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

ans+=i*i;

System.out.println(ans);

Print First N Fibonacci Numbers

📝 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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int a=0;

int b=1;

for(int i=0;i<num;i++){

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

int next=a+b;

a=b;

b=next;

}
}

Day 3: 23/06/25:

Find GCD of Two Numbers (Using While Loop)

📝 Input: Two integers​


🔁 Task: Find their Greatest Common Divisor using Euclidean Algorithm​
📌 Eg: Input: 24, 36 → Output: 12
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int a = input.nextInt();

int b= input.nextInt();

while(b!=0){

int temp =b;

b=a%b;

a=temp;

System.out.print("GCD:"+a);

}
Find LCM of Two Numbers

📝 Input: Two integers​


🔁 Task: Find their Least Common Multiple using loop​
📌 Eg: Input: 4, 5 → Output: 20
import java.util.Scanner;

class program{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

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;

int Lcm= (x*y)/gcd;

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{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int ecount=0,ocount=0;

while(n>0){

int digit = n%10;

int rem = digit;

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{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int ecount=0,ocount=0;

while(n>0){

int digit = n%10;

int rem = digit;

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{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int onum=n;

int sum=0;

int rem=0;

while(n>0){

int digit = n%10;

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{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int mul=1;

while(n>0){

int digit =n%10;

int rem = digit;

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int ans=0;

int count = 0;

while (n > 0) {

int digit = n % 10;

if(prime(digit)){

count++;

n /= 10;

System.out.print(count);

public static boolean prime(int a) {

if(a<2) return false;

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

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{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

while (num >=10) {

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{

System.out.print("Not a Magic Number");

}
👉
7️⃣ Reverse the Number without using Extra Variables​
Logic-level test (you can only use 2 variables max)
import java.util.Scanner;

class program{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int rem=0;

while(n>0){

int digit = n%10;

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{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n= input.nextInt();
int sq = n*n;

int digits=0;

while(sq>0){

digits+=sq%10;

sq/=10;

if(digits==n){

System.out.print(digits+" Neon Number");

else{

System.out.print("Not a Neon Number");

👉
9️⃣ Print All Armstrong Numbers from 1 to n​
Input: n = 1000 → Output: 1, 153, 370, 371, 407
import java.util.Scanner;

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++) {


int num = i;

int length = String.valueOf(num).length();

int sum = 0;

while (num > 0) {

int digit = num % 10;

sum += Math.pow(digit, length);

num /= 10;

if (sum == i) {

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

🔟 Sum of Digits Raised to Power of Count of Digits​


👉 Input: 89 → 8² + 9² = 64 + 81 = 145 ❌​
👉 Input: 135 → 1³ + 3³ + 5³ = 1 + 27 + 125 = 153 ✅
import java.util.Scanner;

class program{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

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;

mul += (int) Math.pow(digit,length);

n/=10;

System.out.println(mul);

if(mul==onum){

System.out.print("Valid Number");

else{

System.out.print("It is not a Valid Number");

} 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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int position =1;

int sum=0;

while(n>0){

int digits = n%10;

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int length = String.valueOf(n).length();

int position =length;

int mul =1;

while(n>0){

int digits = n%10;

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

👉 A number with zero(s) in it (but not starting with 0)​


👉 Input: 1023 → Output: ✅ Duck Number​
👉 Input: 0123 → Output: ❌ Not Duck Number
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int onum=n;

boolean ans = false;

while(n>0){

int digit =n%10;

if(digit==0){

ans=true;

n/=10;

if(String.valueOf(onum).charAt(0)=='0'){

System.out.print("Not Duck Number");

else if(ans){

System.out.print("Duck Number");

else{

System.out.print("Not Duck Number");


}

Count Digits Dividing the Number

👉 Input: 124​
Digits: 1, 2, 4​
→ 124 is divisible by 1, 2, and 4 → Output: 3

import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int onum =n;

int length = String.valueOf(n).length();

int count=0;

while(n>0){

int digit = n%10;

if(digit!=0 && onum%digit==0){

count++;

n/=10;

System.out.print(count);

}
}

Count Digits Not Dividing the Number

👉 Input: 125​
Digits: 1, 2, 5​
→ Only 1 and 5 divide 125 → Output: 1 (2 doesn't divide)

import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int onum =n;

int length = String.valueOf(n).length();

int count=0;

while(n>0){

int digit = n%10;

if(digit!=0 && onum%digit!=0){

count++;

n/=10;

System.out.print(count);

}
Check if the Number is a Spy Number

👉 Sum of digits = Product of digits​


👉 Example: 1124 → 1+1+2+4=8 & 1×1×2×4=8 → Spy Number ✅
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int onum =n;

int sum =0,mul=1;

while(n>0){

int digit = n%10;

sum+=digit;

mul*=digit;

n/=10;

if(sum==mul){

System.out.print("Spy Number");

else {

System.out.print("Not Spy Number");

}
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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

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

int temp=i;

int sum = 0;

while (temp>0) {

int digit = temp % 10;

sum += digit;

temp/=10;

if (i%sum == 0) {

System.out.println(i);

}
Reverse + Add until Palindrome (Bonus Logic)

👉 Input: 87 → 87 + 78 = 165 → 165 + 561 = 726 → ...​


👉 Repeat until you get a palindrome
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

while (true) {

int onum = n;

int reverse = 0;

while (onum > 0) {

int digit = onum % 10;

reverse = (reverse * 10) + digit;

onum /= 10;

if (reverse == n) {

System.out.print("Palindrome "+n);

break;

} else {

n =n+reverse;

}
}

Count Number of Zeros in a Number

👉 Input: 102030​
👉 Output: 3
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int length = String.valueOf(n).length();

int count=0;

while(n>0){

int digit = n%10;

if(digit ==0){

count++;

n/=10;

System.out.print(count);

}
Day 5:25/06/25:

Automorphic Number Check

👉 A number whose square ends with the same number


👉 Ex: 25² = 625 → ends with 25 → ✅ Automorphic
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int sq=n*n;

int length = String.valueOf(n).length();

int last = (int) Math.pow(10,length);

if(sq%last==n){

System.out.println("Automorphoic Number");

else{

System.out.print("Not Automorphoic Number");

Another Method:
import java.util.Scanner;

class program {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

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{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

int n =input.nextInt();

int sq= n*n*n;

int length =String.valueOf(n).length();

if(n<=0){

System.out.print("Invalid Input");

return;

int digit =(int)Math.pow(10,length);

if(sq%digit==n){

System.out.print("Trimorphoic Number ");

else{

System.out.print("Not Trimorphoic Number ");

}
Count Digits Greater than 5 in a Number

👉 Input: 983452 → Digits greater than 5 are: 9, 8 → Output: 2


import java.util.Scanner;

class program{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

int n =input.nextInt();

int count=0;

while(n>0){

int digit = n%10;

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{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

int n =input.nextInt();

int digit =String.valueOf(n).length();

if(length%2!=0){

System.out.print(“Invalid Number”);

return;

int divisor = (int) Math.pow(10,digit/2);

int Fh = n/divisor;

int Lh=n%divisor;

int sum=Fh+Lh;

int sq = sum*sum;

if(sq==n){

System.out.print("Tech Number "+sq);

else{

System.out.print("Not a Tech Number "+sq);

}}

}
Check if Sum of Even Digits is Divisible by 4

👉 Input: 123456 → Even digits: 2, 4, 6 → Sum = 12 → 12 % 4 == 0 → Output:


YES

import java.util.Scanner;

class program{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int sum=0;

while(n>0){

int digit = n%10;

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

👉 Input: 1234 → Even = 24=8, Odd = 13=3 → Output: 8 - 3 = 5


import java.util.Scanner;

class program{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int emul=1,omul=1;

while(n>0){

int digit = n%10;

if(digit%2==0){

emul*=digit;

else{

omul*=digit;

n/=10;

int sub= emul-omul;

System.out.print(sub);

}
Find Sum of Prime Digits Present in a Number

👉 Input: 5931 → Prime digits: 5, 3 → Sum = 8


import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int sum=0;

while(n>0){

int digit = n%10;

if(isPrime(digit)){

sum +=digit;

n/=10;

System.out.print(sum);

public static boolean isPrime(int d){

return d ==2|| d ==3|| d ==5|| d ==7;

}
Count Total Digits Before First 0 Appears

👉 Input: 123045 → Output: 3 (since 0 appears after 3 digits)


import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String n = input.nextLine();

int count = 0;

for (int i = 0; i <n.length(); i++) {

if (n.charAt(i) == '0') {

break;

count++;

if (count == n.length()) {

System.out.print("No zeros are found");

} else {

System.out.print(count);

Check if Number is Perfect Square Without Using Math.pow()

👉 Input: 49 → Output: Yes (Use loop)


import java.util.Scanner;

class program{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

int n = input.nextInt();

boolean ps = false;

for (int i=0;i*i<=n;i++){

if(i*i==n){

ps =true;

break;

if(ps){

System.out.print("Prefect Square");

else{

System.out.print("Not Prefect Square"); }

Another Method:( Math .sqrt):


import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();
double sqrt = Math.sqrt(n);

if (sqrt == (int) sqrt) {

System.out.print("Perfect Square");

} else {

System.out.print("Not a Perfect Square");

Check if a Number is a Twisted Prime

👉
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{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int reverse = reverse(n);

System.out.println(reverse);

if(isPrime(n)==true && isPrime(reverse)==true){

System.out.print("Twisted Prime");

else{

System.out.print("Not Twisted Prime");

}
public static int reverse(int b){

int rev=0;

while(b>0) {

int digit = b % 10;

rev = rev * 10 + digit;

b /= 10;

return rev;

public static boolean isPrime(int a) {

if (a < 2) {

return false;

} else {

for (int i = 2; i <= Math.sqrt(a); i++) {

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{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

int n =input.nextInt();

int sq = n*n;

int digit =String.valueOf(n).length();

int divisor = (int) Math.pow(10,digit);

int Fh = sq/divisor;

int Lh=sq%divisor;

int sum=Fh+Lh;

if(n==sum){

System.out.print("Kaprekar Number "+sum);

else{

System.out.print("Not a Kaprekar Number "+sum);

}}

}
Another Method Using reverse:
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int original = n;

int reverse = 0;

// Step 1: Reverse the number

while (n > 0) {

int digit = n % 10;

reverse = reverse * 10 + digit;

n /= 10;

// Step 2: Count digits until first 0 appears

int count = 0;

while (reverse > 0) {

int digit = reverse % 10;

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int len = String.valueOf(n).length();

int count = 0;

while (n > 0) {

int digit = n % 10;

if (digit==0) {

break;

count++;

n /= 10;

if (count == len) {

System.out.print("No zeros are Found");

}
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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int sum=0;

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

if(n%i==0){

sum+=i;

if(sum==n){

System.out.print("Perfect Number");

}
else {

System.out.print("Not Perfect Number");

👉
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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int onum=n;

int rev=0;

while(n>0){

int digit =n%10;

rev=(rev*10)+digit;

n/=10;

int position =1;

System.out.println(rev);

while(rev>0){
int digit =rev%10;

if(rev%2==0){

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

rev/=10;

position++;

Another Method :(Using String):


import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String n = input.next();

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

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

}
👉
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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int mul=1;

while(n>0){

int digit = n%10;

mul*=digit;

n/=10;

System.out.println(mul);

boolean ans = isPrime(mul);

if(isPrime(mul)) {

System.out.print("Prime number");

else{

System.out.print("Not Prime number");

public static boolean isPrime(int a){


if(a<2){

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int rev = 0, temp = n;

while (temp > 0) {

rev = rev * 10 + temp % 10;

temp /= 10;
}

while(rev>0){

int digit =rev%10;

int sq= digit*digit;

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int count=0;

while(n>0){
int digits = n%10;

if(digits%3==0 && digits>0){

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int rev = 0, temp = n;

while (temp > 0) {

rev = rev * 10 + temp % 10;

temp /= 10;

}
int position =1;

while(rev>0){

int digit =rev%10;

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int temp = n;

int max=0;

while (temp > 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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = Math.abs(input.nextInt()); // Handle negatives

int max = 0;

int count = 0;
while (n > 0) {

int digit = n % 10;

if (digit > max) {

max = digit;

count = 1; // Reset count since new max found

} else if (digit == max) {

count++;

n /= 10;

System.out.println("Highest digit: " + max);

System.out.println("Occurrences: " + count);

👉
20. Count Consecutive Repeating Digits​
Input: 1223334444 → Output: 3​
(Repeating groups: 22, 333, 4444 → Total = 3)
Day6: 26/06/25:

Check if a Number is a Lucky Number​


A lucky number is a number where the sum of digits is even, and

👉
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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int sum = 0, mul = 1;

while (n > 0) {

int digit = n % 10;

sum += digit;

mul *= digit;

n /= 10;

if (sum % 2 == 0 && mul % 2 != 0) {

System.out.print("Lucky Number");

} else {

System.out.print("Not Lucky Number");

}}

}
👉
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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int count=0;

while(n>0){

int digit = n%10;

if((int)Math.pow(digit,3)%3==0){

count++;

n/=10;

System.out.print(count);

3️⃣. Check if the Digits of a Number are in Increasing Order (Left to

👉
Right)​

👉
Input: 1234 → Output: Yes​
Input: 421 → Output: No
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int prevDigit =10;

boolean ans = true;

while (num>0){

int curDigit = num%10;

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int rev=0;

while(num>0){

int digit = num%10;

rev =rev*10+digit;

num/=10;

int position =1;

int sum=0;

while(rev>0){

int digit = rev%10;

if(isPrime(position)){

sum+=digit;

position++;

rev/=10;

System.out.println(sum);
}

static boolean isPrime(int a){

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int count=0;
while(n>0){

int digit = n%10;

for (int i = 1; i*i<=digit; i++) {

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

String binary="";

int count=0;

while(num>0){

int bit = num%2;

if(bit==1){

count++;

binary = bit +binary;

num/=2;

if(count%2==0){

System.out.println("Evil Number");

else{

System.out.println("Not a Evil Number");

}
👉
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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int len = String.valueOf(num).length();

int pow = (int) Math.pow(10,len-1);

int ldigit= num%10;

int fdigit= num/pow;

int digit =Math.abs(fdigit-ldigit);

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();


int count=0;

while(num>0){

int digit = num%10;

if(digit==1||digit==2||digit==4||digit==8){

count++;

num/=10;

System.out.println(count);

🔟. Check if a Number is a Disarium Number​


👉 A number is Disarium if the sum of its digits powered to their
👉 Input: 135 → 1¹ + 3² + 5³ = 135 → Output: Disarium​
positions equals the number.​

👉 Input: 89 → 8¹ + 9² = 89 → Output: Disarium


import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int onum= num;

int rev = 0;

while (num > 0) {


int digit = num % 10;

rev = rev * 10 + digit;

num /= 10;

int pow = 1;

int sum = 0;

while (rev > 0) {

int digit = rev % 10;

sum += (int) Math.pow(digit, pow);

rev /= 10;

pow++;

if (onum == sum) {

System.out.println(sum+"-Disarium Number");

else{

System.out.println(sum+"-Not Disarium Number");

}
CONVERTING DECIMAL INTO BINARY:
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

String binary="";

while(num>0){

int bit = num%2;

binary = bit +binary;

num/=2;

System.out.println(binary);

Day 10: 30/06/2025:

👉
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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int count=0;

while(num>0){

int digit = num%10;

int cube = digit*digit*digit;

if(isPrime(cube)){

count++;

num/=10;

System.out.println(count);

static boolean isPrime(int a ){

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int ldigit=num%10;

boolean allSame =true;

while (num > 0) {

int digit = 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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int rev =0;

while(num>0){

int digit = num%10;

rev= rev*10+digit;

num/=10;

int position =1;

int max=0;

while(rev>0){

int digit = rev%10;

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int onum =num;

int sum=0;

while(num>0){

int digit = num%10;

sum+=digit;

num/=10;
}

if(onum%sum==0){

System.out.println("Harsad Number");

else{

System.out.println("Not Harsad Number");

👉
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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

boolean isHex = false;

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

int ans = 3 * i * (i - 1) + 1;

if (ans == num) {

isHex = true;

break;

if (ans > num) {


break;

if (isHex) {

System.out.println("Number is Centered Hexagonal");

} else {

System.out.println("Number is Not Centered Hexagonal");

👉
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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int rev =0;

while(num>0){

int digit = num%10;


rev= rev*10+digit;

num/=10;

int position =1;

int count=0;

while(rev>0){

int digit = rev%10;

if(digit==0 && position%2==0){

count++;

rev/=10;

position++;

System.out.println(count);

🔟 Check if a Number is a Buzz Number​


👉 A number is Buzz if it is divisible by 7 or ends with 7.​
👉 Example: Input: 27 → ends with 7 → Output: Buzz​
👉 Example: Input: 35 → divisible by 7 → Output: Buzz
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();


input.close();

int ldigit = num%10;

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int max1 =-1,max2=-1;

while(num>0){

int digit = num%10;

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int rev =0;

while(num>0){

int digit = num%10;

rev = rev *10 + digit;

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

int len = String.valueOf(num).length();

if (len % 2 != 0) {

System.out.println("Odd digit count - not valid for this check.");


return;

int div = (int) Math.pow(10,len/2);

int lDigit = num%div;

int fDigit = num/div;

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 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

for (int i = 2; i <= num-2; i++) {

if (isPrime(i ) && isPrime((i+2))) {

System.out.println("("+i+ "," +(i+2)+")");

public static boolean isPrime(int n){

if (n <=1) return false;

for (int j = 2; j <=Math.sqrt(n); j++) {

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.

Given the integer n, return the number of complete rows of the


staircase you will build.
import java.util.Scanner;

class program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt(); // Total number of coins

int row = 0;

while (n >= row + 1) {

row++;

n -= row;

System.out.println(row);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy