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

ISC Practical 2027: College Enrolment Code Validator and Stream Report – Algorithm and Java Program
Fully solved ISC Computer Science Practical 2027 question: validate college enrolment codes made of a stream, year and serial number, give the exact reason for each rejection, auto-correct lowercase streams and prepare a stream-wise report.

ISC Practical 2027: Digital Lock Security Code Analyzer – Algorithm and Java Program
Fully solved ISC Computer Science Practical 2027 number-logic question: for a 5 to 10 digit security code, find the first and last digits, odd and even position sums, product of non-zero digits, digit frequency and classify the code as STRONG, MODERATE or WEAK.

Boolean Algebra for ISC Class 12: Laws, Canonical SOP and POS, and Simplification
ISC Class 12 Computer Science notes on Boolean algebra: all the laws and theorems, duality, De Morgan's laws, minterms and maxterms, converting to canonical SOP and POS, and step-by-step algebraic simplification with solved examples.