algorithms
algorithms
Start
Initialize variables:
End
import java.util.Scanner;
class Keypad{
int count = 0;
char ch = word.charAt(i);
if(!Character.isLetter(ch)){
System.out.println("INVALID ENTRY");
return;
if("ADGJMPTW".indexOf(ch) >= 0)
count++;
count += 2;
count += 3;
else
count += 4;
}
DATE PROGRAM
1. Start
2. Input the day number, year, and value of N:
o Prompt the user to enter:
dayNum (an integer representing the day of the year).
year (an integer representing the year).
n (an integer representing the number of days to add).
3. Validate the input values:
o Initialize validDayNum = true and validN = true.
o Check the validity of dayNum:
If dayNum < 1 or:
dayNum > 365 for a non-leap year.
dayNum > 366 for a leap year.
Set validDayNum = false and display "INCORRECT DAY
NUMBER".
o Check the validity of n:
If n < 1 or n > 100:
Set validN = false and display "INCORRECT VALUE OF
'N'".
o If either validDayNum or validN is false, terminate the program.
4. Set up month and day arrays:
o Initialize an array day with the number of days in each month for a non-leap
year:
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}.
o Initialize an array month with the names of the months:
{"JANUARY", "FEBRUARY", ..., "DECEMBER"}.
5. Adjust for leap year:
o If the year is a leap year (isLeap(year) returns true):
Set day[1] = 29.
6. Calculate the entered date:
o Initialize index = 0 (to track the current month).
o While dayNum > day[index]:
Subtract day[index] from dayNum.
Increment index.
If index reaches 12 (end of the year):
Reset index = 0.
Increment year.
Adjust day[1] for the new year if it's a leap year.
7. Display the entered date:
o Print "ENTERED DATE: <month[index]> <dayNum>, <year>".
8. Calculate the date N days later:
o While n > 0:
Increment dayNum.
Decrement n.
If dayNum > day[index]:
Reset dayNum = 1.
Increment index.
If index reaches 12 (end of the year):
Reset index = 0.
Increment year.
Adjust day[1] for the new year if it's a leap year.
9. Display the resulting date:
o Print "<n> DAYS LATER: <month[index]> <dayNum>, <year>".
10. Define the helper function isLeap(year) to determine if a year is a leap year:
o Return true if:
(year % 4 == 0) and (year % 100 != 0), or (year % 400 == 0).
o Otherwise, return false.
11. End
import java.util.Scanner;
class LaterDate{
System.out.print("YEAR: ");
System.out.print("N: ");
int n = Integer.parseInt(in.nextLine());
validDayNum = false;
validDayNum = false;
validN = false;
if(!validDayNum || !validN)
return;
int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(isLeap(year))
day[1] = 29;
int index = 0;
dayNum -= day[index++];
if(index == 12){
index = 0;
year++;
if(isLeap(year))
day[1] = 29;
else
day[1] = 28;
dayNum++;
n--;
dayNum = 1;
index++;
if(index == 12){
index = 0;
year++;
if(isLeap(year))
day[1] = 29;
else
day[1] = 28;
return true;
return false;
}
GOLDBACH
Start
Initialize variables:
Input: An integer n.
Initialize a counter f = 0.
Loop through integers from 1 to n:
o If n is divisible by the current integer, increment f.
If f equals 2 (indicating only two divisors: 1 and itself):
o Return true (the number is prime).
Else:
o Return false (the number is not prime).
End
import java.util.Scanner;
class Goldbach{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("N = ");
int n = Integer.parseInt(in.nextLine());
if(n > 50 || n % 2 != 0){
System.out.println("INVALID INPUT");
return;
}
int i = 3;
while(i <= (n - i)){
if(isPrime(i) && isPrime(n - i))
System.out.println(i + ", " + (n - i));
i++;
}
}
public static boolean isPrime(int n){
int f = 0;
for(int i = 1; i <= n; i++){
if(n % i == 0)
f++;
}
return f == 2;
}
}
ANAGRAM
Start
If status is true:
o Print "THEY ARE ANAGRAMS!".
Otherwise:
o Print "THEY ARE NOT ANAGRAMS.".
End
class Anagrams{
String a = in.nextLine().toUpperCase();
String b = in.nextLine().toUpperCase();
if(!valid(a) || !valid(b)){
return;
a = removeSpaces(a);
b = removeSpaces(b);
String x = String.valueOf(a.charAt(0));
a = a.replace(x, "");
b = b.replace(x, "");
if(a.length() != b.length()){
status = false;
break;
if(status)
System.out.println("THEY ARE ANAGRAMS!");
else
char ch = s.charAt(i);
return false;
return true;
char ch = s.charAt(i);
if(Character.isWhitespace(ch))
continue;
str += ch;
return str;
}
PANGRAM PROGRAM
Start
Initialize variables:
shortest = s (to store the shortest word, initialize to the input sentence).
longest = "" (to store the longest word).
isPangram = true (to track if the sentence is a pangram).
End
import java.util.Scanner;
class Pangram{
String s = in.nextLine();
if(s.length() == 0){
System.out.println("INVALID INPUT");
return;
if(".?!".indexOf(last) == -1){
System.out.println("INVALID INPUT");
return;
if(ch == Character.toLowerCase(s.charAt(i))){
match = true;
break;
}
}
if(!match){
isPangram = false;
break;
if(isPangram)
System.out.println("IT IS A PANGRAM");
else
char ch = s.charAt(i);
if(Character.isLetterOrDigit(ch))
word += ch;
else{
}
ROWS PROGRAM
Start
Initialize r = 0 and c = 0 to store the row and column indices of the highest
element.
Iterate through all elements of the matrix:
o If the current element is greater than the element at mat[r][c]:
Update r and c with the indices of the current element.
Print the value of the highest element along with its row and column indices.
End
import java.util.Scanner;
class ShiftRows{
System.out.print("M = ");
int m = Integer.parseInt(in.nextLine());
System.out.print("N = ");
int n = Integer.parseInt(in.nextLine());
return;
int r = 0;
int c = 0;
mat[i][j] = Integer.parseInt(in.nextLine());
System.out.println("ORIGINAL MATRIX:");
display(mat);
a[i] = mat[0][i];
display(mat);
r = i;
c = j;
System.out.println();