Hey🖐, this blog is about Problem Solving in programming and it will help you to solve a problem easily. This blog will cover certain things you should follow while solving a problem, so read the blog and start solving your problems easily.
Introduction
We always try to solve a particular problem in one shot, but this is not the right way to tackle a problem. We always have to break the problem into smaller parts and solve those smaller parts to get to the full solution of the problem. So the steps include to solve a problem are as follows:
Understand the problem
Analyze the problem
Break the problem into small parts
Solve the small parts
Combine the small parts solution to get the full solution
How to think, apply and build?
Before starting to solve a problem, you have to read the problem and understand it deeply. And once you understand the problem deeply, you can write down the steps to solve the problem in the paper because it will help you to solve a problem more efficiently. For solving the problem, you can divide it into small parts and solve those small parts because it may be difficult to solve a big problem in one go but it is always easy to solve a small problem since a small problem doesn't need to include all resources. A small problem only solves a particular problem. And after solving small problems, you can combine those solutions to get the full solution of the problem.
As for building an application, different developers work on different parts of the application such as front-end, back-end, database etc. But what happened if only one developer did all jobs? It would be a very difficult task, right? That's why we have to divide a problem into chunks to make the problem-solving task easier.
Importance of flow-chart
The flow chart is also an important concept to solve a particular problem. Flow charts give step-by-step instructions to solve a problem. Certain symbols are used in the flow chart and each symbol has a certain meaning. An example of a flow-chart "To check for odd/even for an input" is:
In this flow chart, the problem is divided into smaller sections. So first of all the program starts, then a variable is declared named 'n' and then input is taken from the user, then a condition is checked and depending on the result of the condition different statements are printed and at the last, the program stops.
So now it would be easier to write code from this flow chart.
Code:
import java.util.Scanner;
public class Example{
public static void main(String[] args)
{
// Program starts
// Creating object of the scanner class to take input
Scanner o = new Scanner(System.in);
int n; // Initialization of variable
// Taking the input from the user
System.out.println("Enter a number");
n = o.nextInt();
// Now checking whether the number is even or odd
if(n % 2 == 0)
System.out.println("The number is even");
else
System.out.println("The number is odd");
// program stopped
}
}
Example
Let's take a programming problem to understand how to solve a problem practically. We will divide the problem into smaller sections and then solve those smaller sections to get to the full solution of the problem.
Question
Write a program to take a number (n) as input from the user and print n lines of this pattern:
Solution:
Let's first analyze the problem by breaking it into small parts. So the steps to solve this problem are:
- First of all, we have to take the input n
The pattern has n lines. So to print multiple lines we need one loop.
Again in each line, there is an increasing number of stars i.e. with each line the number of stars is increasing by one. So again we need another loop inside the previous loop. That means there are two loops, one is an outer loop and the other one is an inner loop.
Now we have to think about the conditions of the loops. The outer loop is taking care of the number of lines so the outer loop will run for n number of times since there are n lines. And the inner loop will run suppose k times, where k is the nth line because if we observe the pattern we will see the 1st line has one star, 2nd line has two stars, 3rd line has three stars and so on, so it depends on the current position of the line.
Now we use a print statement inside the inner loop to print a star.
And use a line break inside the outer loop and the inner loop finishes.
With this small steps you can finally solve the problem easily like the code below.
import java.util.Scanner;
public class Example{
public static void main(String[] args)
{
// Creating object of the scanner class to take input
Scanner o = new Scanner(System.in);
int n; // Initialization of variable
// Taking the input from the user
System.out.println("Enter a number");
n = o.nextInt();
// Now printing the pattern
//Outer loop
for(int i=0;i<n;i++)
{
// Inner loop
for(int j=0;j<=i;j++)
{
System.out.println("*");
}
System.out.print("\n");
}
}
}
This is it for this blog. Stay tuned for my next blog.
Like, Share and Comment. ♥