Magic number program
Magic number program: solved Java program for ISC Class 12 Computer Science with complete BlueJ code, explanation and sample output.
Watch java videos
The sum of the number’s digits calculated until the sum equal 1. If it does equal to 1, then it is a magic number. Here is the code to solve the program recursively.
Example 10 is a magic number. Because 1+0=1.
import java.io.*;
class Magic
{
int num;
public static int sumdig(int n)
{
if(n==0)
{
return 0;
}
else
{
int rem=n%10;
return (rem+sumdig(n/10));
}
}
void isMagic()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A NUMBER");
num=Integer.parseInt(br.readLine());
while(num>9)
{
num=sumdig(num);
}
if(num==1)
{
System.out.println("A MAGIC NUMBER");
}
else
{
System.out.println("NOT A MAGIC NUMBER");
}
}
public static void main()throws IOException
{
Magic obj=new Magic();
obj.isMagic();
}
}
Download free Sample papers
Related Posts
Continue learning with these related articles

Logic Gates for ISC Class 11: Truth Tables, Universal Gates, Half and Full Adder
ISC Class 11 Computer Science notes on logic gates: AND, OR, NOT, NAND, NOR, XOR and XNOR with truth tables, why NAND and NOR are universal gates, and half adder and full adder circuits with their Boolean expressions.
Decimal to Binary Conversion in Java: 3 Programs with Examples
How to convert a decimal number to binary on paper and in Java: three programs (String, place value and Integer.toBinaryString), binary to decimal, a dry run and common mistakes.

ISC Class 11 Computer Science Sample Paper 2026-27 with Solutions
A full 70-mark ISC Class 11 Computer Science sample paper for the 2026-27 annual exam: MCQs, number systems, propositional logic, logic gates, Java classes, arrays, strings, searching and sorting, and computing ethics, with complete worked solutions.