0% found this document useful (0 votes)
9 views4 pages

Writing Algorithms and Java Programs

Uploaded by

rusher2142
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)
9 views4 pages

Writing Algorithms and Java Programs

Uploaded by

rusher2142
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

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

You might also like