Sie sind auf Seite 1von 35

Question :

Write a Program in Java to input a number and check whether it is a Unique Number or
not.

ALGORITHM :

Step 1 - Start the program


Step 2 - Enter the number
Step 3 - Convert the integer data type to string to get the length.
Step 4 - Check if the characters in the string are all different if so it is a unique number else not
unique number.

Coding:

import java.util.*;

class UniqueNumber

public static void main(String args[] )

Scanner sc=new Scanner(System.in);

System.out.println(“Enter a number :”);

int n=sc.nextInt();

String s= Integer.toString(n); // converting the given number into String datatype

// Int datatype is converting to String , String helps to extract the character by


charAt() and check each character is matching with the rest.

int l=s.length();
int flag=0,i, j;

//Loops to check the digits are repeated

for(i=0;i<l-1;i++)

for(j=i+1;j<l;j++){

if(s.charAt(i)==s.charAt(j)) // if any digits are repeated, then it is not a


UniqueNumber

{ flag=1;

break; }

if(flag ==0)

System.out.println(” It is an UniqueNumber”);

else

System.out.println(” It is not an UniqueNumber”);

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.

Frequency of unique-digit integers is: 28

VARIABLE DESCRIPTION TABLE:

S.No Variable Data Type Description

1 n int to enter the numbers

2 s String to convert integer to


string

3 l int to get the length of


the string

4 flag int to check if number is


unique or not

5 i int to run the loop

6 j int to run the loop

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 1 - Start the program.

Step 2 - Enter the number.

Step 3 - Check if the given number is the product of two consecutive numbers.

Step 4 - If so the number is a pronic number.

Step 5 - Print the number.

Program Coding:

import java.util.*;

class Pronic

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter number:");

int n=sc.nextInt(); // to get number from user.

boolean x=false; // to hold Boolean value of whether n is a Pronic number or not.

for(int i=1;i<n;i++)
{

if((i*(i+1))==n)

x=true;

if(x)

System.out.println("Entered number is a Pronic number.");

else

System.out.println("Entered number is not a Pronic number.");

Input/Output:

Enter number:

20

Entered number is a Pronic number.

VARIABLE DESCRIPTION TABLE -

S.NO VARIABLE USED Data Type DESCRIPTION


1 n int to enter the number

2 i int to run the loop

3 x int to check if condition


true or false

Result:

The program is executed successfully.

Question -
A smith number is a composite number whose sum of digits is equal to the sum of digits of its
prime factors.

ALGORITHM:

Step 1: Start the pgm.


Step 2: Get input for the number.
Step 3: The function sod(int) calculates the sum of digits of entered number.
Step 4: The function isprime(int) tells if entered number is prime or not.
Step 5: A while loop in the main method calculates the sum of digits of the prime factors.
Step 6: An if condition in main method checks if the sum of digits of the number is equal to sum
of digits of its prime factors, and prints whether it is a smith number or not.
Step 7: Stop the pgm.
Step 8: The ppm is executed successfully.

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

public static boolean isprime(int y)


{ int c=0;
for(int i=1;i<=y;i++)
{ if(y%i==0)
c++;
}
if(c==2)
return true;
else
return false;
}

public static void main()


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number");
int n=sc.nextInt();
int orig = n;
int sf=0,sd=0;
smith ob=new smith();
while(n!=1)
{
for(int i=2;i<=n;i++)
{
if(ob.isprime(i) && n%i==0)
{ System.out.println(i);
sf=sf+ob.sod(i);
n=n/i;
break;
}

}}
sd=ob.sod(orig);
if(sf==sd)
System.out.println("Smith number");
else
System.out.println("Not a Smith number");
}
}

VARIABLE DESCRIPTION TABLE -

s int Stores sum of digits

x int function parameter

y int function parameter

i int loop variable

c int counter

n int number entered

orig int stores number entered

sf int stores sum of digits of prime


factors

sd int stores sum of digits of n

Result:

The program is executed successfully.


Question :
A number is said to be a bouncy number if the digits of the number are unsorted.
For example,
22344 - Is not a bouncy number
774410 - Is not a bouncy number
155349 - Is a bouncy number
Write a ppm in java to accept a number. Check and display whether it is a bouncy number or
not.

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");
}}

VARIABLE DESCRIPTION TABLE:

Variable Name Data Type Description


c int pointer
f int pointer
x string stores the number as a string
l int stores length of string
i int while loop variable
j int for loop variable
n int number entered by user

RESULT -

The program has successfully executed.

Question
Algorithm -

Step 1 -

Program Coding:

import java.util.*;

class Lines

static String a[],words[];

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter number of lines to be entered and enter the sentences:");

int n=sc.nextInt();

a=new String[n];

int c=0,c2=0;

String dummy=sc.nextLine();

for(int i=0;i<n;i++) //to get lines as input

a[i]=sc.nextLine();

for(int i=0;i<n;i++) //to find total no of words in all lines

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

for(int i=0;i<n;i++) //to store all words in array

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.println("The words in reverse order are:");

for(int i=words.length-1;i>=0;i--) //to print words in reverse order

System.out.print(words[i]+" ");

Input/Output:

Enter number of lines to be entered and enter the sentences:

2
Emotions controlled and directed to work, is character.

By Swami Vivekananda.

The words in reverse order are:

Vivekananda Swami By character is work to directed and controlled Emotions

Variable Description Table:

Question 6:

Algorithm:

Program Coding:

import java.util.*;

class Parenthesis
{

public static void main()

Scanner sc=new Scanner(System.in);

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

System.out.println("Sorry,and invalid String");


}

Input/Output:

Enter sentence:

SUN(A) RISE BEGINS FROM (RT) EAST

SUN RISE BEGINS FROM EAST

Variable Description Table:

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");
}
}
}
}

VARIABLE DESCRIPTION TABLE:

Variable Name Data Type Description


n int size
i int loop variable
j int loop variable
k int loop variable
x char character input by user
y int integer value of character
f int pointer
RESULT -

The program has successfully executed

Question 8:

Algorithm:

Program Coding:

import java.util.*;

class Charac

public static void main()

Scanner sc=new Scanner(System.in);


System.out.println("Enter no of rows and the 3 characters:");

int n=sc.nextInt();

if(n>10)

System.out.println(“Entered value out of range.”);

return;

String dummy=sc.nextLine();

char a=sc.nextLine().charAt(0);

char b=sc.nextLine().charAt(0);

char c=sc.nextLine().charAt(0);

char M[][]=new char[n][n];

for(int i=0;i<n;i++)

M[i][i]=c; //to form 1st diagonal with 3rd char

M[i][n-1-i]=c; //to form 2nd diagonal with 3rd char

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:

Enter no of rows and the 3 characters:

@$$$@

!@$@!

!!@!!

!@$@!

@$$$@
Question 9:

Algorithm:

Program Coding:

import java.util.*;

class Saddlept

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter Matrix length and the elements:");


int n=sc.nextInt();

int A[][]=new int[n][n];

for(int i=0;i<n;i++) //input elements

for(int j=0;j<n;j++)

A[i][j]=sc.nextInt();

boolean x=true;

int i=0,j=0;

outer: for(i=0;i<n;i++) //to find saddle point

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)

System.out.println("Saddle point = "+A[i][j]);

for(i=1;i<n;i++) //sort left diagonal

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(i=0;i<n;i++) //print array with sorted diagonal

for(j=0;j<n;j++)

System.out.print(A[i][j]+" ");

System.out.println();

else

System.out.println("No Saddle Point");

Input/Output:

Enter Matrix length and the elements:

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 2 - Enter the elements of the array

Step 3 - Sort the array A using the bubble sort technique.

Step 4 - Find the index of the middle elements of the array A.

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

System.out.println("\nThe Sorted Array Is"); // Printing the Sorted Array


for(int i=0; i<n; i++)
{
System.out.print(A[i]+" ");
}
int mid = (n-1)/2; //finding index of middle cell
int x = 1, lim = n-1-mid;

//Pendulum Arrangement Starts Here //

B[mid]=A[0]; //putting the minimum element in the middle cell


for(int i=1; i<=lim; i++)
{
if((mid+i)<n) //travelling to the right side
{
B[mid+i]=A[x++];
}
if((mid-i)>=0) //travelling to the left side
{
B[mid-i]=A[x++];
}
}
//Printing the Result//
System.out.println("\n\nThe Result Is");
for(int i=0; i<n; i++)
{
System.out.print(B[i]+" ");
}
}
}

VARIABLE DESCRIPTION TABLE:

S.no VARIABLE Data Type DESCRIPTION


1 n int To store the elements
of the array
4 mid int to store index of the
middle cell
5 x int To get array ellem
6 lim int no of arrangements for
pendulum
RESULT:
The program is successfully executed

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 1 - Start the program.

Step 2 - Enter a date.

Step 3 - Convert the date into string.

Step 4 - Enter the day on the 1st of january.

Step 5 - Store the number of days in each month in array dom[].

Step 6 - Find the index of the entered day name in the day names array.

Step 7 - Get the day for the date given.

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

for(i = 0; i < 7; i++)


{
if(dayName.equalsIgnoreCase(days[i])){
index = i;
break;
}
}
//find the index of the day in the day names array for the current day
r = days%7 + index - 1;
if( r >= 7)
r -= 7;
System.out.println("Day on " + date + " : " + days[r]);
}
}catch(Exception e){
System.out.println("Please enter the date in the specified format.");
System.out.println("Error : "+e);
}
}
}

VARIABLE DESCRIPTION TABLE:

S.NO VARIABLE USED Data Type DESCRIPTION

1 d int To enter the day


2 m int To enter the month
3 y int To enter the year
4 days int To display the total
number of days
5 date String To enter the name in
date format
6 dayname String To enter the name of
day according to the
date

RESULT -

The program has successfully executed

Das könnte Ihnen auch gefallen