Beruflich Dokumente
Kultur Dokumente
Write a Program in Java to input a number and check whether it is a Unique Number or
not.
ALGORITHM :
Coding:
import java.util.*;
class UniqueNumber
int n=sc.nextInt();
int l=s.length();
int flag=0,i, j;
for(i=0;i<l-1;i++)
for(j=i+1;j<l;j++){
{ flag=1;
break; }
if(flag ==0)
System.out.println(” It is an UniqueNumber”);
else
Input/Output:
Enter lower and upper limits:
2500
2550
2501,2503,2504,2506,2507,2508,2509,2510,2513,2514,2516,2517,2518,2519,2530,2531,2534,2
536,2537,2538,2539,2540,2541,2543,2546,2547,2548,2549.
RESULT:
The program is successfully executed
Question -
Write a Java program to check whether a number is a Pronic Number or
Heteromeric Number or not.A pronic number is a number which is the product of
two consecutive integers, that is, a number of the form n(n + 1).
ALGORITHM:
Step 3 - Check if the given number is the product of two consecutive numbers.
Program Coding:
import java.util.*;
class Pronic
System.out.println("Enter number:");
for(int i=1;i<n;i++)
{
if((i*(i+1))==n)
x=true;
if(x)
else
Input/Output:
Enter number:
20
Result:
Question -
A smith number is a composite number whose sum of digits is equal to the sum of digits of its
prime factors.
ALGORITHM:
CODING -
import java.util.*;
class smith
{
public static int sod(int x)
{ int s=0;
while(x>0)
{ s=s+x%10;
x=x/10;
}
return s;
}
}}
sd=ob.sod(orig);
if(sf==sd)
System.out.println("Smith number");
else
System.out.println("Not a Smith number");
}
}
c int counter
Result:
Algorithm:
Step 1: Start the pgm.
Step 2: Get input for the number from the user.
Step 3: Call the function check(int) to check for bouncy number.
Step 4: The function check splits the digits of the number and stores it in an array.
Step 5: A loop is run from 0 to no. of digits and there are conditions which check if greater than
or lesser than the next number and accordingly have pointers assigned value 1 accordingly, if
both pointers are one the digits are unsorted and the number is bouncy else not bouncy.
CODING:
import java.util.*;
class bouncy
{
static boolean check(int num)
{ int c=0;
int f=0;
String x=Integer.toString(num);
int l=x.length();
int a[]=new int[l];
int i=l-1;
while(num>0)
{ a[i]=num%10;
num=num/10;
i--;
}
for(int j=0;j<l-1;j++)
{ if(a[j]<a[j+1])
c=1;
else if(a[j]>a[j+1])
f=1;
}
if(f==1 && c==1)
return true;
else
return false;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number");
int n=sc.nextInt();
bouncy ob= new bouncy();
if(ob.check(n))
System.out.println("Bouncy number");
else
System.out.println("Not a Bouncy number");
}}
RESULT -
Question
Algorithm -
Step 1 -
Program Coding:
import java.util.*;
class Lines
int n=sc.nextInt();
a=new String[n];
int c=0,c2=0;
String dummy=sc.nextLine();
a[i]=sc.nextLine();
int c1=1;
a[i]=a[i].trim();
for(int j=1;j<(a[i].length()-1);j++)
if((a[i].charAt(j-1)!=' ')&&(a[i].charAt(j-1)!=("'").charAt(0))&&(a[i].charAt(j-1)!='.')&&
(a[i].charAt(j-1)!=',')&&(a[i].charAt(j-1)!=';')&&(a[i].charAt(j-1)!=':')&&
((a[i].charAt(j)=='
‘)||(a[i].charAt(j)==("'").charAt(0))||(a[i].charAt(j)=='.')||(a[i].charAt(j)==',')||(a[i].charAt(j)==';')||
(a[i].charAt(j)==':')))
c1++;
c+=c1;
words=new String[c];
int p=1;
if(Character.isLetter(a[i].charAt(0)))
p=0;
for(int j=1;j<(a[i].length()-1);j++)
if(((a[i].charAt(j)=='
')||(a[i].charAt(j)==("'").charAt(0))||(a[i].charAt(j)=='.')||(a[i].charAt(j)==',')||(a[i].charAt(j)==';')||(
a[i].charAt(j)==':'))&&(j-p>0))
{
words[c2]=a[i].substring(p,j);
p=j+1;
c2++;
if((a[i].charAt(a[i].length()-1)==' ')||(a[i].charAt(a[i].length()-1)==("'").charAt(0))||
(a[i].charAt(a[i].length()-1)=='.')||(a[i].charAt(a[i].length()-1)==',')||
(a[i].charAt(a[i].length()-1)==';')||(a[i].charAt(a[i].length()-1)==':'))
words[c2]=a[i].substring(p,a[i].length()-1);
c2++;
System.out.print(words[i]+" ");
Input/Output:
2
Emotions controlled and directed to work, is character.
By Swami Vivekananda.
Question 6:
Algorithm:
Program Coding:
import java.util.*;
class Parenthesis
{
System.out.println("Enter sentence:");
String s=sc.nextLine();
s=s.toUpperCase();
boolean x=true,t=true;
int c1=0,c2=0,plop=0;
char y=')';
for(int i=0;i<s.length();i++) //to check if there is no nested parentheses and empty parentheses
if(s.charAt(i)=='(')
plop=i;
c1++;
if(y==(')
x=false;
y='(';
if(s.charAt(i)==')')
c2++;
if(y==')')
x=false;
if(i-plop<2)
x=false;
y=')';
if(c1!=c2) //to check if there are an equal number of opening and closing parenthesis
x=false;
if(x)
for(int i=0;i<s.length();i++) //to print the sentence without the parentheses and the contents
inside if String s is valid
if(s.charAt(i)=='(')
t=false;
if(t)
System.out.print(s.charAt(i));
if(s.charAt(i)==')')
t=true;
else
Input/Output:
Enter sentence:
Question -
A new advanced Operating System, incorporating the latest hi-tech features has been designed
by Opera Computer System. The task of generating copy protection codes to prevent software
piracy has been entrusted to the Security Department. The security department has decided to
have codes containing a jumbled combination of alternate uppercase letters of the alphabet
starting from A upto K (namely among A,C,E,G,I,K). The codes may or may not be in a
consecutive series of alphabets.
ALGORITHM -
Step 1: Start the pgm.
Step 2: Get input for size and if its less than 6 create an array of given size and accept
characters.
Step 3: Check if the entered characters match the given rules and print valid if they follow else
print invalid.
Step 4: Stop the pgm.
Step 5: The pgm is executed successfully.
CODING:
import java.util.*;
class prot_code
{
public static void main()
{
Scanner sc= new Scanner(System.in);
System.out.println("enter n");
int f=0;
int n=sc.nextInt();
if(n>6)
System.out.println("error");
else
{
char a[]=new char[n];
System.out.println("enter letters one by one");
for(int i=0;i<n;i++)
{
a[i]=sc.next().charAt(0);
}
for(int i=0;i<n;i++)
{ int y=(int)a[i];
if(y>96)
{
f=1;
}
}
for(int i=0;i<n;i++)
{ char x=a[i];
if(x!='A' && x!='C' && x!='E' && x!='G' && x!='I' && x!='K')
{ f=1;
}
for(int k=0;i<n;i++)
{ for(int j=0;j<n;j++)
{ if(a[k]==a[j] && k!=j)
{
f=1;
}
}
}
if(f==0)
System.out.println("Valid");
else
System.out.println("Invalid");
}
}
}
}
Question 8:
Algorithm:
Program Coding:
import java.util.*;
class Charac
int n=sc.nextInt();
if(n>10)
return;
String dummy=sc.nextLine();
char a=sc.nextLine().charAt(0);
char b=sc.nextLine().charAt(0);
char c=sc.nextLine().charAt(0);
for(int i=0;i<n;i++)
int y=n-1-i;
int x=i;
if(x>y)
y=i;
x=n-1-i;
}
int j=0;
bp: while(true) //to form left side pattern with 2nd character
if(M[i][j]==c)
break bp;
M[i][j]=b;
j++;
for(int k=x+1;k<y;k++) //to form middle section of pattern with 1st char
M[i][k]=a;
int l=n-1;
bp: while(true) //to form right section of pattern with 2nd char
if(M[i][l]==c)
break bp;
M[i][l]=b;
l--;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
System.out.print(M[i][j]);
System.out.println();
Input/Output:
@$$$@
!@$@!
!!@!!
!@$@!
@$$$@
Question 9:
Algorithm:
Program Coding:
import java.util.*;
class Saddlept
for(int j=0;j<n;j++)
A[i][j]=sc.nextInt();
boolean x=true;
int i=0,j=0;
for(j=0;j<n;j++)
x=true;
for(int k=0;k<n;k++)
if(A[i][k]<A[i][j])
x=false;
for(int k=0;k<n;k++)
{
if(A[k][j]>A[i][j])
x=false;
if(x)
break outer;
if(x)
for(j=0;j<i;j++)
if(A[i][i]<A[j][j])
int t=A[i][i];
for(int k=i;k>j;k--)
A[k][k]=A[k-1][k-1];
A[j][j]=t;
}
}
for(j=0;j<n;j++)
System.out.print(A[i][j]+" ");
System.out.println();
else
Input/Output:
12
8
14
Saddle point = 4
4 6 12
2 6 14
138
QUESTION -
Write a program to input a list of integers in an array and arrange them in a way similar
to the to-and-fro movement of a Pendulum.
ALGORITHM :
Step 1 - Start the program
Step 5 - Check the pendulum condition with respect to the middle element.
Step 6 - Print the pendulum arrangement for the following set of numbers from array A to array B.
Example :
Input : 1 3 2 5 4
Output :5 3 1 2 4
Explanation:
The minimum element is 1, so it is moved to the middle.
The next higher element 2 is moved to the right of the
middle element while the next higher element 3 is
moved to the left of the middle element and
this process is continued.
Input : 11 12 31 14 5
Output :31 12 5 11 14
CODING :
import java.util.*;
class Pendulum
{
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
System.out.print("\Enter number of elements: ");
int n = sc.nextInt();
int A[]=new int[n]; //original array
int B[]=new int[n]; //array for storing the result
for(int i=0; i<n; i++)
{
System.out.print("Enter Element "+(i+1) );
A[i] =sc.nextInt();
}
//Sorting the Inputted Array in Ascending Order
int t=0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(A[i]>A[j])
{
t=A[i];
A[i]=A[j];
A[j]=t;
}
}
QUESTION -
write a program to accept a date in the string format dd/mm/yyyy and accept the
name of the day on the 1st of jan of the corresponding year
ALGORITHM:
Step 6 - Find the index of the entered day name in the day names array.
CODING:
import java.util.*;
class CurrentDayName
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
int i,d,m,y,days,index,r;
String date,dayName;
System.out.println("Enter a date in the string format dd/mm/yyyy: ");
date = sc.next();
d=m=y=0;
// Convert the date in string format to day, month and year
try{
d = Integer.parseInt(date.substring(0,2));
m = Integer.parseInt(date.substring(3,5));
y = Integer.parseInt(date.substring(6));
System.out.println("Enter the name of the day on the 1st of January ");
dayName = scan.next();
//Store the days of the months
int dom[] = {31,28,31,30,31,30,31,31,30,31,30,31};
//If it is a leap year, february should have 29 days.
if(yy%4 == 0)
dom[1] = 29;
if(dd < 0 || dd > dom[mm-1])
{
System.out.println("Invalid date.");
}
else
{
//Store the day names of the days
String days[] = {"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"};
//Find the corresponding day of the year
days = 0;
//Add the days of the month
for(i = 0; i < mm-1; i++)
days+= dom[i];
//Add the days of the current month
days+= d;
index = 0;
//Find the index of the entered day name in the day names array
RESULT -