Algorithm
An algorithm is defined as a step-by-step set of instructions to required to get the desired result
or to perform specific task. These steps may be written in simple English language.
Steps to write Algorithm
1. Begin the algorithm with word "Start".
2. Algorithm should finish a word with "End or Stop".
3. Give a number to each steps.
4. After understanding the problem write each steps in seperate lines.
Q1. Write an algorithm and Java Program to add two numbers.
Algorithm
1. Start
2. Input two numbers: A and B
3. Calculate SUM = A + B
4. Display SUM
5. Stop
Java Program
import [Link];
class AddNumbers
public static void main( )
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int A = [Link]();
[Link]("Enter second number: ");
int B = [Link]();
int SUM = A + B;
[Link]("Sum = " + SUM);
} //end of main function
}// end of class
Q2. Write an algorithm and Java program to Calculate Profit or Loss.
Algorithm
1. Start
2. Input the Cost Price (CP)
3. Input the Selling Price (SP)
4. If SP > CP, then
Profit = SP – CP
Display “Profit = ” Profit
5. Else if CP > SP, then
Loss = CP – SP
Display “Loss = ” Loss
6. Else
Display “No Profit, No Loss”
7. Stop
Java Program
import [Link];
class ProfitLossCalculator
public static void main( )
Scanner sc = new Scanner([Link]);
[Link]("Enter Cost Price (CP): ");
double CP = [Link]();
[Link]("Enter Selling Price (SP): ");
double SP = [Link]();
if (SP > CP)
{
double profit = SP - CP;
[Link]("Profit = " + profit);
else if (CP > SP)
double loss = CP - SP;
[Link]("Loss = " + loss);
else
[Link]("No Profit, No Loss");
} //end of main function
}// end of class
Q3. Write an algorithm and Java Program to check whether the no. is Palindrome Number or
not.
A palindrome number is a number that remains the same when its digits are reversed.
👉 Example: 121, 1331, 454, 12321
Algorithm
1. Start
2. Input the number N
3. Store the value of N in another variable (say, TEMP)
4. Initialize REV = 0
5. Repeat the steps while N > 0:
a. REM = N mod 10 (get the last digit)
b. REV = REV × 10 + REM (build the reverse number)
c. N = N ÷ 10 (remove the last digit)
6. If REV = TEMP, then
Display “Number is Palindrome”
7. Else
Display “Number is Not Palindrome”
8. Stop
Java Program
import [Link].*;
class PalindromeCheck
{
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int N = [Link]();
int TEMP = N;
int REV = 0, REM;
while (N > 0)
{
REM = N % 10; // Get last digit
REV = REV * 10 + REM; // Build reverse number
N = N / 10; // Remove last digit
}
if (REV == TEMP)
[Link](TEMP + " is a Palindrome number.");
else
[Link](TEMP + " is NOT a Palindrome number.");
}
} //end of main function
}// end of class
Q4. Write an algorithm and Java Program to check whether the String. is Palindrome Number
or not.
A palindrome string is a number that remains the same when it gets reversed.
👉 Example: MADAM, DAD, ARORA, etc
Algorithm
1. Start
2. Input a string from the user
3. Convert the string into lowercase and store it in a variable original
4. Initialize an empty string reversed
5. Find the length of original
6. Set a loop counter i = length - 1
7. Repeat until i >= 0
i. Take the character at position i from original
ii. Add this character to reversed
iii. Decrease i by 1
8. End loop → Now reversed contains the reverse of original
9. Compare original and reversed
i. If both are equal → Print “Palindrome”
ii. Else → Print “Not a Palindrome”
10. Stop
Java Program
import [Link];
class PalindromeString
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String original = [Link]();
String reversed = "";
for (int i = [Link]() - 1; i >= 0; i--)
{
reversed = reversed + [Link](i);
}
if ([Link](reversed))
[Link](str + " is a Palindrome.");
else
[Link](str + " is NOT a Palindrome.");
} //end of main function
}// end of class