0% found this document useful (0 votes)
220 views12 pages

Java PRP Programming Examples

The document contains code snippets for various Java programs that solve problems related to strings, numbers, and algorithms. The programs include methods to identify possible words from a string, uppercase the second word of a string, check if a string is a palindrome, calculate the weight of a string, find the most frequent digit in a number, generate user IDs, check password stability, calculate hill weight patterns, find the sum of power of digits in a number, and calculate the Nth Fibonacci number.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
220 views12 pages

Java PRP Programming Examples

The document contains code snippets for various Java programs that solve problems related to strings, numbers, and algorithms. The programs include methods to identify possible words from a string, uppercase the second word of a string, check if a string is a palindrome, calculate the weight of a string, find the most frequent digit in a number, generate user IDs, check password stability, calculate hill weight patterns, find the sum of power of digits in a number, and calculate the Nth Fibonacci number.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1) Identify Possible Words:

[Link]

public class IdentifyPossibleWords {


static String PossibleWords(String input1,String input2)
{
int i,j;
input1 = [Link]();
input2 = [Link]();
String word="", res="";
String[] words = [Link](":");
for(i=0;i<[Link];i++)
{
word = words[i];
if([Link]()==[Link]())
{
for(j=0;j<[Link]();j++)
{
if([Link](j)!='_' && [Link](j)!=[Link](j))
{
break;
}
}
if(j==[Link]())
{
res=res + word+":";
}
}
}
return [Link]()==0?"ERROR-009":[Link](0,[Link]()-1);
}
public static void main(String[] args) {
String fin = PossibleWords("Fi_er","Fixer:Filer:fiber:fibre:tailor:offer");
[Link](fin);
}
}

2) 2nd Word UpperCase:

[Link]

public class Second_upper {


public static String Upper(String s)
{
String str = s;
String ch[] = [Link](" ");
//int len =[Link]();
return ch[1].toUpperCase();

}
public static void main(String[] args) {
String res =Upper("Hello welcome guys");
[Link](res);
}
}

3) IsPalinStr
[Link]
//Write a program to accept a String from the command prompt and display whether the string
is a palindrome or not.
class Pali2{
public static void main(String[] args) {

String s = "fad";
int len = [Link]();
String rev = "";

for (int i = len-1; i>=0; i--) {


rev=rev+[Link](i);
}
[Link](rev);
if([Link](rev))
{
[Link]("Palindrome");
}
else
{
[Link]("Not a Palindrome");
}
}
}
4) WeightOfString:
[Link]

import [Link].*;
import [Link].*;
class WeightOfString
{
public static int Weight(String input1,int input2)
{
String str=[Link]();
int sum=0;
for(int i=0;i<[Link]();i++)
{
if(input2==0)
{
if([Link](i)=='A' || [Link](i)=='E' ||[Link](i)=='I' || [Link](i)=='O' ||
[Link](i)=='U' || ![Link]([Link](i)))
{
continue;
}
else
{
int a=[Link](i)-64;
sum+=a;
}
}
else
{
if(![Link]([Link](i)))
continue;
else
{
int a=[Link](i)-64;
sum+=a;
}
}
}
return sum;
}
public static void main(String[] args) {
int res =Weight("Hello",0);
[Link](res);
}
}
5) MostFrequentDigit
[Link]

Logic 1:
public class MostRepDigit
{
public static void main(String[] args) {
int a=222, b = 312, c = 300,d = 100; //233233
String st = [Link](a)+[Link](b)+[Link](c)
+[Link](d);
[Link](st);
int len = [Link]();
int[] freq =new int[10];//0 to 9
for(int i = 0;i<len;i++)
{
int digit = [Link](i) - '0';
freq[digit]++;
}
/*for(int i=0;i<10;i++)
{
[Link](freq[i]);
}*/
int max = freq[0];
for(int i=0;i<10;i++)
{
if(max<freq[i])
{
max = freq[i];
}
}
[Link]("Number of repeatitons of a number: "+max);
int collect=0;
for(int i=0;i<10;i++)
{
if(freq[i]==max)
{
collect = i;
[Link]("Most Frequent Value: "+collect);
}

}
}
Logic 2:

class Frequncy{
public static void main(String args[]){
int w=222,v=312,c=300,d=100;
int b[]={w,v,c,d};
int a[]=new int[50];
int y[]=new int[10];
int k=0;
for(int i=0;i<[Link];i++)
while(b[i]>0){
int x=b[i]%10;
a[k]=x;
k++;
b[i]=b[i]/10;
}
for(int i=0;i<k;i++){
for(int j=0;j<10;j++){
if(a[i]==j)
y[j]++;
}

}
int max=y[0];
for(int i=1;i<[Link];i++)
{
if(max<y[i])
max=y[i];
}
for(int i=0;i<[Link];i++)
{
if(max==y[i])
[Link](i);

}
}
}
6) UserIdGeneration:
[Link]

class PasswordSmallerLargerNumber {
public static String passw(String input1,String input2,int input3,int input4)
{
String s1 = input1;
String s2 =input2;
int m = input3;
int N= input4;
String longer = "",smaller="";
if([Link]()>[Link]())
{
longer =s1;
smaller =s2;
}
else
{
smaller = s1;
longer = s2;
}
if([Link]()==[Link]())
{
if([Link](s2)>0)
{
longer = s1;
smaller =s2;
}
else
{
smaller =s1;
longer = s2;
}
}
String num = [Link](m);
String result ="";

String r = [Link]([Link]()-
1)+longer+[Link](N-1)+[Link]([Link]()-N);
for(int i=0;i<[Link]();i++)
{
if([Link]([Link](i)))
{
result = result+[Link]([Link](i));
}
else
{
result = result+[Link]([Link](i));
}
}
return result;
}
public static void main(String[] args) {
String res = passw("Hanu","Ram",5647,2);
[Link](res);
}
}

7) FindPasswordStableUnstable:
[Link]

class PasswordStableUnstable {
public static int password(int input1,int input2,int input3,int input4, int
input5)
{
int[] arr = {input1,input2,input3,input4,input5};
int stable=0,unstable=0;
for(int i =0;i<[Link];i++)
{
int[] freq = new int[10];
int arr1 = arr[i];
int max=0;
while(arr1>0)
{
int e =arr1%10;
freq[e]++;
arr1=arr1/10;
if(freq[e]>max) // 0 0 2 0 0 0 0 2 //max=2
{
max = freq[e];
}
}
int l;
for(l=0;l<10;l++)
{
if(freq[l]!=0 && freq[l]!=max)
{
break;
}
}

if(l==10)
{
stable+=arr[i];
}
else
{
unstable+=arr[i];
}

}
return (stable - unstable);
}
public static void main(String[] args) {
int pass = password(455,257,3300,110,303);
[Link](pass);
}
}

8) WeightHillPattern:
[Link]

public class HillsPattern {


public static int totalHillWeight(int input1,int input2,int input3)
{
int sum=0;
for(int i = 0;i<input1;i++)
{
for(int j=0;j<=i;j++)
{
sum = sum+input2;
}
input2 = input2+input3;
}
return sum;
}

public static void main(String[] args) {


int t = totalHillWeight(4,1,5);
[Link](t);
}
}

9) SumOfPowerOfDigits:
[Link]

10) CyclicOrder:
[Link]

11) QuadraticRoots:
12) FindNumberOfProblems:

13) Nth Fibonacii:


public class Demo{
static int fibo(int num){
int first = 0, second = 1, temp;
if (num == 0)
return first;
if (num == 1)
return second;
for (int i = 2; i <= num; i++){
temp = first + second;
first = second;
second = temp;
}
return second;
}
public static void main(String args[]){
int num = 7;
[Link]("The 7th fibonacci number is : ");
[Link](fibo(num));
}
}

14) StepNumber:

public class StepNumber {


static String Step(int input1)
{
String str = [Link](input1);
int len = [Link]();
int[] arr = new int[len];
int rem =0;
String out = "";
if(len == 1)
{
out = str+"-false";
}
else
{
int i=len;
while(input1!=0)
{
rem = input1%10;
arr[i-1] = rem;
input1 = input1/10;
i--;
}
int k =1;
int count=0;
for(int j=0;j<len-1;j++)
{

if(arr[j]-arr[k]==1 || arr[j]-arr[k]==-1) //0 1 //1 2 //2 3


{
count++;
}
k++;

}
if(count ==len-1)
{
out = str+"-true";
}
else
{
out = str+"-false";
}

}
return out;
}
public static void main(String[] args) {
String res = Step(707); //9849
[Link](res);
}
}

15) DigiLock:

public class DigiLock {

public int Lock(int in1,int in2,int in3)


{
int a[] = {in1,in2,in3};
int temp = 0, x = 0, y = 0, z = 0, k = 0, l = 0, m = 0, max = 0, key =
0;
int u[] = new int[3];
int t[] = new int[3];
int h[] = new int[3];
for(int i=0;i<[Link];i++)
{
u[i] = a[i]%10;
temp = a[i]/10;
t[i] = temp%10;
h[i] = a[i]/100;
}
x = [Link](u[0], [Link](u[1], u[2]));
y = [Link](t[0], [Link](t[1], t[2]));
z = [Link](h[0], [Link](h[1], h[2]));
k = [Link](u[0], [Link](u[1], u[2]));
l = [Link](t[0], [Link](t[1], t[2]));
m = [Link](h[0], [Link](h[1], h[2]));
max = [Link](k, [Link](l, m));
key = (1000*max) + (100*z) + (10*y) +x;
return key;
}
public static void main(String[] args) {
DigiLock sol = new DigiLock();
int fin = [Link](143, 540, 034);
[Link](fin);
}

16)

Common questions

Powered by AI

The 'WeightOfString' function calculates the weight based on alphabetic characters. It converts the string to uppercase. If 'input2' is 0, it ignores vowels and non-letter characters; otherwise, it includes vowels. For each considered character, it calculates the weight by subtracting 64 from the ASCII value of the character (since 'A' = 65) and adds this to a cumulative sum. The function returns the total sum as the string's weight .

The 'PasswordStableUnstable' function differentiates by counting digit occurrences in each number. It uses an array 'freq' to hold frequencies of digits 0-9 for each number. It calculates the 'max' frequency for each number. A number is 'stable' if all non-zero frequencies are equal to 'max'; otherwise, it is 'unstable.' The function sums all stable numbers and subtracts the sum of unstable numbers to return the final result .

The 'totalHillWeight' function calculates the total weight by iterating through levels of the hill. For each level (from 0 to 'input1'), it adds the current weight 'input2' multiplied by the number of elements in that level. After each level, 'input2' is incremented by 'input3' to account for the increase in weight at each successive level. The sum of all levels is returned as the total hill weight .

The 'WeightOfString' function calculates the string's weight by iterating through each character. If 'input2' is 0, it ignores vowels and non-alphabetic characters; otherwise, it adds the weight of all alphabetic characters. The weight is determined by subtracting 64 from the ASCII value of each character to convert it to a numerical value (A=1, B=2, etc.). These values are summed to get the total string weight .

The 'PossibleWords' function converts both 'input1' and 'input2' to uppercase to ensure case-insensitivity. It then splits 'input2' by colon ':' to get individual words. For each word, it checks if the length matches 'input1'. If lengths match, it checks each character in 'input1'. If the character is an underscore '_', it ignores it; otherwise, it compares it with the corresponding character in the word. If all characters match or are underscores, the word is considered a match and added to the result string. If no matches are found, it returns 'ERROR-009' .

In the 'UserIdGeneration' function, case transformation is used to alternate the case of characters in the final result string. Starting from a string formed by the concatenation rule, the function checks if each character is lowercase. If it is, it converts it to uppercase and vice versa. This ensures the generated user ID has a sequence of alternating cases, enhancing readability and variability of the ID .

The 'IsPalinStr' function checks for palindromes by first reversing the input string. It does so by iterating over the string from the last character to the first, constructing the reversed string. Then, it compares the reversed string with the original. If they are identical, the string is a palindrome; otherwise, it is not .

The 'FindPasswordStableUnstable' function calculates the difference by processing each input number with frequency analysis. It determines if a number is stable by checking if all digits that appear have the same frequency. Each frequency is compared against the maximum frequency found. Numbers with all non-zero frequencies equal to the max frequency are deemed stable. It sums stable numbers, subtracts sums of unstable numbers, and returns the resulting difference .

The 'MostFrequentDigit' function first concatenates all numbers into a single string and initializes an array 'freq' to count occurrences of each digit (0-9). It increments the count for each digit found in the string. The function then determines the maximum frequency 'max' by checking each frequency count. Finally, it loops through the frequencies to find which digit(s) occurred 'max' times and outputs those as the most frequent digits .

The 'DigiLock' function processes three integers, treating them as three-digit numbers. It extracts units, tens, and hundreds places into separate arrays ('u', 't', 'h'). It finds the minimum value in each of these place arrays and constructs the smallest possible value. Similarly, it finds the maximum values to construct the largest possible number, using these minima and maxima to generate a final result key, which is returned .

You might also like