0% found this document useful (0 votes)
3 views1 page

Java Programs Revision

Uploaded by

Arshiya Begum
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)
3 views1 page

Java Programs Revision

Uploaded by

Arshiya Begum
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

Java Programs Revision Cheat Sheet

Sum of Digits
Logic: Extract %10, add, /10
Syntax: while(n>0){ sum+=n%10; n/=10; }

Product of Digits
Logic: Extract %10, multiply, /10
Syntax: while(n>0){ prod*=n%10; n/=10; }

Factors of a Number
Logic: Loop i=1..n, check n%i==0
Syntax: while(i<=n){ if(n%i==0) print(i); i++; }

Prime Number
Logic: Count factors, if==2 → Prime
Syntax: while(i<=n){ if(n%i==0) c++; i++; }

Reverse Number
Logic: rev=rev*10+(n%10)
Syntax: while(n>0){ rev=rev*10+(n%10); n/=10; }

Palindrome
Logic: Reverse, compare to original
Syntax: if(m==rev) print('Palindrome')

Factorial
Logic: Multiply 1..n
Syntax: while(i<=n){ f*=i; i++; }

Spy Number
Logic: Compare sum vs product of digits
Syntax: if(sum==prod) print('Spy')

Armstrong Number
Logic: Sum of cubes of digits == n
Syntax: sum+=d*d*d;

Perfect Number
Logic: Sum of proper divisors == n
Syntax: if(sum==n) print('Perfect')

Automorphic Number
Logic: Square ends with number
Syntax: if(sq%pow(10,d)==n) print('Automorphic')

Kaprekar Number
Logic: Split square parts, sum==n
Syntax: if(left+right==n) print('Kaprekar')

Menu-Driven Areas
Logic: switch(choice){ case: formula }
Syntax: switch(ch){ case 1: ... break; }

Menu-Driven Temp
Logic: switch → apply formula
Syntax: case 1: (c*9/5)+32; case 2: c+273;

You might also like