CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Java · 7 min · free

From Algorithm to Java Program

7 min4 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Write the algorithm first, turn each step into Java, then test with normal, boundary and invalid inputs, ISC practical style. An algorithm is a finite list of clear numbered steps in plain English, ending with Stop.

Follows the syllabus of: ISC Class 11 Computer Science (868), ISC Class 12 Computer Science (868)

On screen in this lesson

What is an algorithm?

A finite list of clear, numbered steps
Written in plain English, not in Java
Says what to read, what to do and what to print
Always ends with a Stop step

Our problem: a Niven number

A Niven number is divisible by the sum of its digits
18: digits add to 9, and 18 % 9 is 0, so NIVEN
19: digits add to 10, and 19 % 10 is 9, so NOT NIVEN
Zero or a negative number: print INVALID INPUT

Dry run the algorithm: n = 18

tt % 10sum
1888
119
0loop ends9

Each step becomes Java

Algorithm stepJava code
Step 2: Read nsc.nextInt()
Step 3: check, stopif with return
Steps 5 and 6while loop
Step 7if-else, println

Testing with sample inputs

Work out the expected output by hand first
Normal values: one that passes, one that fails
Boundary values: 0, 1, the smallest valid input
Invalid values: negative numbers
Run each one and compare with your expected output

Our test table

InputExpectedReason
18NIVEN18 % 9 = 0
19NOT NIVEN19 % 10 = 9
1NIVEN1 % 1 = 0
0INVALID INPUTnot positive
-12INVALID INPUTnegative

Quick answers

Why copy n into t before the digit loop?

The loop destroys t while breaking it into digits, and n is still needed at the end.

Why check invalid input first?

It also protects you from dividing by a digit sum of zero.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. Many students jump straight into typing code, and then get stuck halfway. Toppers do it the other way round. They write the algorithm first, turn each step into Java, and then test with sample inputs. Today we will do exactly that for one full problem, in the style of a board practical paper.

An algorithm is a step by step plan to solve a problem. It is a finite list of clear, numbered steps. It is written in plain English, so it does not depend on any language. Each step says what to read, what to calculate, or what to print. And it always ends, with a Stop step.

Here is our problem, a typical practical question. A Niven number, also called a Harshad number, is divisible by the sum of its digits. Take eighteen. One plus eight is nine, and eighteen mod nine is zero, so it is a Niven number. Take nineteen. One plus nine is ten, and the remainder is nine, so it is not. If the input is zero or negative, the program must print Invalid Input.

Now the algorithm. Step one is Start. Step two reads the number n. Step three checks the input first. If n is zero or less, we print Invalid Input and jump to the last step. Step four sets sum to zero, and copies n into t. We copy it, because we will break t into digits, but we still need n at the end.

Step five checks, are the digits over? If t is zero, go to step seven. Step six adds the last digit, which is t mod ten, to sum. Then it removes that digit by dividing t by ten, and goes back to step five. Step seven prints Niven if n mod sum is zero, else Not Niven. Step eight is Stop.

Before writing any Java, we dry run the algorithm with eighteen. First, t is eighteen, the last digit is eight, and sum becomes eight. Then t is one, the digit is one, and sum becomes nine. Now t is zero, so the loop ends with sum nine. Eighteen mod nine is zero, so the plan prints Niven, which is correct.

Now we translate, one step at a time. Read n becomes a Scanner call, nextInt. Check and stop becomes an if, with a return statement that ends main. Steps five and six, which repeat, become a while loop. Step seven becomes an if else, with println.

Here are steps two and three. A Scanner reads input from the keyboard, and needs import java dot util dot Scanner at the top. nextInt reads the number n. If n is zero or less, we print Invalid Input. Then return ends the main method at once, which is our go to Step eight.

Step four declares sum and t. The while loop runs while t is greater than zero, which is step five. Inside, plus equals adds the last digit to sum, and slash equals removes that digit, exactly as step six says. Step seven uses double equals to compare, and prints Niven or Not Niven. Notice the comments. They link each part of the code to its step, and examiners like that.

While you are still building, you can test the core logic with a fixed value, instead of typing input every time. Here n is eighteen. The same digit loop is written as a for loop, and plus equals means add to sum. It prints true, because eighteen is a Niven number. Once this works, put the Scanner back.

Testing means running the program on chosen inputs, and checking every output. First, work out the expected output by hand, before you run. Pick normal values, one that should pass and one that should fail. Pick boundary values, which sit at the edge, like zero and one. Pick invalid values, like a negative number. Then run each one, and compare.

Here is a test table for our program. Eighteen should print Niven. Nineteen should print Not Niven. Pause and predict, what about one? Its digit sum is one, and one mod one is zero, so Niven. Zero must print Invalid Input, and this also saves us from dividing by a zero sum. Minus twelve must print Invalid Input too.

Now let us see how a board practical paper asks this kind of question. You get a problem statement, with sample inputs and outputs. You write an algorithm first, in clear steps. Then you write the Java program, with comments and meaningful variable names. Next, you run it on the given test data and on some of your own. Finally, the output must match the format shown, including messages like Invalid Input.

Four tips will save you marks. Always check invalid input before any calculation, just as we did in step three. Keep a copy of any value you will need later, like copying n into t. Number your steps, and use the same numbers in your comments. And dry run the algorithm on paper, before you type a single line.

Let us revise. First, write the algorithm as numbered steps in plain English. Next, turn each step into Java, one step at a time, and link them with comments. Then test with normal, boundary and invalid inputs, and compare with answers worked out by hand. In the practical, present the algorithm, the documented program, and the test output.

Courses that teach this

CourseUnit
ISC Class 11 Computer Science (868)Elementary Data Structures and Implementation
ISC Class 12 Computer Science (868)Functions (Methods)

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →