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

Java Programming Cheat Sheet Guide

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)
18 views1 page

Java Programming Cheat Sheet Guide

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;

Common questions

Powered by AI

A number is verified as a palindrome by reversing its digits and comparing the reversed number to the original. If both numbers are the same, the number is identified as a palindrome .

The differentiation lies in the operation applied to each digit: while sum of digits involves adding each isolated digit using a modulus operation, product of digits multiplies them. Both processes iterate through digits similarly using division by 10 to progress .

The factorial of a number is calculated by multiplying all integers from 1 to the number. The logic involves iterating through these integers while multiplying them sequentially to obtain the factorial .

A number is identified as a Kaprekar number by splitting the square of the number into two parts and checking if their sum equals the original number .

To determine if a number is a prime, the program counts its factors by iterating from 1 to the number itself, incrementing a counter each time the number divides evenly. If the count of factors equals 2, the number is prime .

An Automorphic number is identified by squaring the number and checking if the number is a suffix of its square. This is done by evaluating if the last digits of the square match the number itself .

A number is considered perfect if the sum of its proper divisors (excluding itself) equals the number. The program iterates through its potential divisors, summing those that divide the number evenly, then checks if the sum matches the original number .

A number is identified as a Spy number when the sum of its digits equals the product of its digits. The program calculates these two values and compares them to make the determination .

To verify an Armstrong number, the program calculates the sum of the cubes of its digits and compares it to the original number. If the sum equals the number, it is an Armstrong number .

The modulus operation is crucial in isolating the last digit of a number within each iteration. By applying a modulus of 10, the program extracts the last digit, sums it, and then divides the number by 10 to repeat the process for all digits .

You might also like