You are on page 1of 22

Question 22.

Write a program to accept a date in the string format dd/mm/yyyy and accept name of the day as on 1st of
January of the corresponding year. Find the day for the given date.
Example: Input Date: 5/7/2001 Day on 1st January : MONDAY
Output Day on 5/7/2001 : THURSDAY
Test run the program on the following inputs:
Input-Date Day on 1st January Output Day for Input-Date
4/9/1998 THURSDAY FRIDAY
31/8/1999 FRIDAY TUESDAY
6/12/2000 SATURDAY WEDNESDAY
The program should include the part for validating the inputs namely the date and the day on 1st January of
that year.

PROGRAM
import java.util.*;
class CurrentDayName
{
public static void main (String args[])
{Scanner scan = new Scanner(System.in);int i,dd,mm,yy,tdays, index,r;
String date,dayName;
System.out.println("Enter a date in the string format dd/mm/yyyy: ");
date = scan.nextLine();
dd=mm=yy=0;
dd = Integer.parseInt(date.substring(0,2));
mm = Integer.parseInt(date.substring(2,4));
yy = Integer.parseInt(date.substring(4,date.length()));
System.out.println("Enter the name of the day on 1st of January ");
dayName = scan.nextLine();
int dom[] = {31,28,31,30,31,30,31,31,30,31,30,31};
if(yy%4 == 0)
dom[1] = 29;
if(dd < 0 || dd > dom[mm-1])
{
System.out.println("INVALID DATE.");
}else
{
String days[] = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY","FRIDAY", "SATURDAY", "SUNDAY"};
tdays = 0;
for(i = 0; i < mm-1; i++)
tdays+= dom[i];
tdays+= dd;
index = 0;
for(i = 0; i < 7; i++)
{
if(dayName.equalsIgnoreCase(days[i])){
index = i;
break;
}}
r = tdays%7 + index - 1;
if( r >= 7)
r -= 7;
System.out.println("Day on " + dd+"/"+mm+"/"+yy + " : " + days[r]);
}}}

OUTPUT
Enter a date in the string format dd/mm/yyyy:
28112002
Enter the name of the day on 1st of January
Tuesday
Day on 28/11/2002 : THURSDAY

VARIABLE DESCRIPTION
VARIABLE TYPE VARIABLE NAME DESCRIPTION
String date To store the date
String dayName To store name of the day.
int i Forloop variable.
int dd To store the date entered by the user.
int mm To store the month entered by the user.
int yy To store the year entered by the user.
int tdays To store total days.
int index To store the index.
int dom array to store no. days in month.

ALGORITHM
Step 1:Start
Step 2: Declare all the variables get the date and name of the date on 1​st​ January
Step 3: Check the days.
Step 4: Print the results.
Step 5:Stop
Question 23.
Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or ‘!’ only. The words are to
be separated by a single blank space and are in uppercase.
Perform the following tasks:
a) Check for the validity of the accepted sentence.
b) Convert the non-palindrome words of the sentence into palindrome words by concatenating the word by
its reverse (excluding the last character).
Example: The reverse of the word HELP would be LEH (omitting the last alphabet) and by concatenating
both, the new palindrome word is HELPLEH. Thus, the word HELP becomes HELPLEH.
Note: The words which end with repeated alphabets, for example ABB would become ABBA and not ABBBA
and XAZZZ becomes XAZZZAX.
[Palindrome word: Spells same from either side. Example: DAD, MADAM etc.]
c) Display the original sentence along with the converted sentence.
Test your program for the following data and some random data:
Example 1
Input: The bird is flying.
Output:The bird is Flying
Theht birdrib isi flyingniylf

PROGRAM
import java.util.*;
public class palin
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the sentence ");
String st=sc.nextLine();
String w,st2,st3="";
String p="";
palin ob=new palin();
char ch=st.charAt(st.length()-1);
st=st.toUpperCase();
st2=st.substring(0,st.length()-1);
StringTokenizer st1=new StringTokenizer(st2);
if(ch=='.'||ch=='?'||ch=='!')
{
while(st1.hasMoreTokens())
{
w=st1.nextToken();
if(ob.isPalindrome(w))
p+=w+" ";
else
p+=ob.generate(w)+" ";
}
System.out.println("THE SENTENCE IS: "+st);
System.out.println("THE NEW SENTENCE IS: "+p);
}
else
{
System.out.println("INVAID INPUT");
System.exit(0);
}}
public boolean isPalindrome(String w1)
{
String r=new String();
for(int i=w1.length()-1;i>=0;i--)
r+=w1.charAt(i);
return(w1.equalsIgnoreCase(r));
}
public String generate(String w1)
{
String r=new String();
for(int i=w1.length()-2;i>=0;i--)
r+=w1.charAt(i);
while(w1.length()>1 && w1.charAt(w1.length()-1)==w1.charAt(w1.length()-2))
w1=w1.substring(0,w1.length()-1);
return(w1+r);
}
}

OUTPUT
enter the sentence
the bird is flying.
THE SENTENCE IS: THE BIRD IS FLYING.
THE NEW SENTENCE IS: THEHT BIRDRIB ISI FLYINGNIYLF

VARIABLE DESCRIPTION
VARIABLE TYPE VARIABLE NAME DESCRIPTION
String st To store the string entered by the user.
String w to store the next token of st1.
String st2 to store the string.
String p Store the value of w.
char ch To store the last character of sting st.
ALGORITHM
Step 1:Start
Step 2: Declare the variables and get the input form the user.
Step 3:Find the last character of string
Step 4: Convert the sting to upper case.
Step 5:Find its substring and create a new string .
Step 6:Print the new String
Step7 :Stop
Question 24.
Write a program to declare a single-dimensional array a[] and a square matrix b[][] of size N, where N > 2
and N < 10. Allow the user to input positive integers into the single dimensional array.
Perform the following tasks on the matrix:
a) Sort the elements of the single-dimensional array in ascending order using any standard sorting technique
and display the sorted elements.
b) Fill the square matrix b[][] in the following format.
If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8}
Then, the matrix b[][] would fill as below:
1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5
c) Display the filled matrix in the above format.
Test your program for the following data and some random data:
Example1
Input N=3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY : 317
OUTPUT SORTED ARRAY: 137
137
131
113

PROGRAM
import java.io.*;
class ArrayFormat{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("N = ");
int n = Integer.parseInt(br.readLine());
if(n < 3 || n > 9){
System.out.println("MATRIX SIZE OUT OF RANGE.");
return;
}
int a[] = new int[n];
int b[][] = new int[n][n];
System.out.println("ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY:");
for(int i = 0; i < n; i++)
a[i] = Math.abs(Integer.parseInt(br.readLine()));
for(int i = 0; i < n; i++){
for(int j = 0; j < n - 1 - i; j++){
if(a[j] > a[j + 1]){
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
System.out.print("SORTED ARRAY:");
for(int i = 0; i < n; i++)
System.out.print(a[i] + " ");
System.out.println();
int len = n;
for(int i = 0; i < n; i++){
int j = 0;
for(j = 0; j < len; j++)
b[i][j] = a[j];
int k = 0;
while(j < n){
b[i][j] = a[k];
j++;
k++;
}
len--;
}
System.out.println("FILLED MATRIX");
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
System.out.print(b[i][j] + "\t");
}
System.out.println();
}
}
}

OUTPUT
N=3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY:
3
1
7
SORTED ARRAY:1 3 7
FILLED MATRIX
1 3 7
1 3 1
1 1 3

VARIABLE DESCRIPTION
VARIABLE TYPE VARIABLE NAME DESCRIPTION
int n to store the value of n entered by the user.
int a Array of n size.
int b To create an 2-D array.
int i Forloop variable.
int j Forloop variable.
int len Stores the value of n.
int k used as a counter.

ALGORITHM
Step 1: Start
Step 2: Declare the variables and get the input form the user.
Step 3: Create an 2d array and get its value from the user.
Step 4: create the new array.
Step 5:Print the new array.
Step 6:Stop

Question 25.
Design a program to accept a day number (between 1 and 366), year (in 4 digits) from the user to generate
and display the corresponding date. Also accept 'N' (1<=N<=100) from the user to compute and display the
future date corresponding to 'N' days after the generated date. Display error message if the value of the day
number, year and N are not within the limit or not according to the condition specified.
Test your program for the following data and some random data.
Example 1
Input
DAY NUMBER 255
YEAR 2018
DATE AFTER(N DAYS) 22
OUTPUT
DATE 12 TH SEPTEMBER,2018
DATE AFTER 22 DAYS 4​TH​ OCTOBER,2018
Example 2
Input
DAY NUMBER 360
YEAR 2018
DATE AFTER(N DAYS) 45
OUTPUT
DATE 26 TH SEPTEMBER,2018
DATE AFTER 22 DAYS 9TH OCTOBER,2018

PROGRAM
import java.util.*;
public class Date
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("DAY NUMBER:");
int n=sc.nextInt();
System.out.println("YEAR:");
int n1=sc.nextInt();
System.out.println("DATE AFTER (N DAYS):");
int n2=sc.nextInt();
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
int s=0,m=0,d1=0,d4=0,d5=0,q=0,t=0;
String
a1[]={"","JANUARY","FEBUARAY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTMBER","OCTOBE
R","NOVEMBER","DECEMBER"};
if(n<366&&n2>1&&n2<100)
{
while(s<n)
s+=a[m++];
s-=a[m-1];
int d=n-s;
System.out.println("DATE :"+d+" "+a1[m]+","+n1);
s=0;m=0;
if(n+n2<365)
{d1=(n+n2);
while(s<d1)
s+=a[m++];
s-=a[m-1];
d5=d1-s;
System.out.println("DATE AFTER "+n1+" DAYS : "+d5+" "+a1[m]+","+n1);
}s=0;m=0;
if(n+n2>365)
{
int d3=(n+n2)-365;
while(s<d3)
s+=a[m++];
s-=a[m-1];
n1=n1+1;
d4=d3-s;
System.out.println("DATE AFTER "+n1+" DAYS : "+d4+" "+a1[m]+","+n1);
}}
if(n>366)
{
System.out.println("DAY NUMBER OUT OF RANGE");
}
if(n2<1&&n2>100)
{
System.out.println("DAY NUMBER AFTER "+n1+" OUT OF RANGE");
}}}

OUTPUT
DAY NUMBER:
255
YEAR:
2018
DATE AFTER (N DAYS):
21
DATE :12 SEPTMBER,2018
DATE AFTER 2018 DAYS : 3 OCTOBER,2018

VARIABLE DESCRIPTION
VARIABLE TYPE VARIABLE NAME DESCRIPTION
int n To store the day.
int n1 To store the year.
int n2 To store the no of days.
int a array with no of days in month
int s to store the value of a[m++].
int m Used as counter variable
int d1 To store the sum of (n+n2)
int d4 to store d3-s value.
int d5 to store d1-s value.
String a1 to store the name of months in array

ALGORITHM
Step 1:Start
Step 2: Declare variables and get day number, year , date after from the user.
Step 3: create array witn no of days in a minth and name of months.
Step 4: Calculate the date and day
Step 5: Print the date and day.
Step 6:Stop

Question 26.
Given two positive numbers M and N, such that M is between 100 and 10000 and N is less than 100. Find the
smallest integer that is greater than M and whose digits add up to N. For example, if M=100 and N=11, then
the smallest integer greater than 100 whose digits add up to 11 is 119.
Write a program to accept the numbers M and N from the user and print the smallest required number
whose sum of all its digits is equal to N. Also, print the total number of digits present in the required
number. The program should check for the validity of the inputs and display an appropriate message for an
invalid input.
Test your program with the sample data and some random data:
Example 1
Input m= 100
N=11
Output
The required number =119
Total number of digits=3

Example 2
Input m= 1500
N=25
Output
The required number =1699
Total number of digits=4

PROGRAM
import java.util.*;
class appr
{
int sumDig(long n)
{
int sum = 0, d;
while(n>0)
{
d = (int)(n%10);
sum = sum + d;
n = n/10;
}
return sum;
}

int countDig(long n)
{
String s = Long.toString(n);
int len = s.length();
return len;
}

public static void main()throws Exception


{
appr ob = new appr();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a value of 'm' from 100 to 10000 : ");
int m = sc.nextInt();
System.out.print("Enter a value of n from 1 to 99 : ");
int n = sc.nextInt();

if(m<100 || m>10000 || n<1 || n>99)


{
System.out.println("Invalid Input");
}
else
{
long i = (long)m;
while(ob.sumDig(i)!=n)
{
i=i+1;
}
System.out.println("The required number = "+i);
System.out.println("Total number of digits = "+ob.countDig(i));
}
}
}
OUTPUT
Enter a value of 'm' from 100 to 10000 : 100
Enter a value of n from 1 to 99 : 11
The required number = 119
Total number of digits = 3

VARIABLE DESCRIPTION
VARIABLE TYPE VARIABLE NAME DESCRIPTION
int sum to store the sum of d.
int d to store the mod od n.
String s converts long to string
int len Calculate the length of strings s.
int m Stores the value of m entered by the user
int n Stores the value of n entered by the user
long i Stores he value of m.

ALGORITHM
Step 1:Start
Step 2:Get the values of m and n from the user.
Step 3: calculate the sum of mod of d.
Step 4:print the required nos.
Step 5:Stop
Question 27.
An ISBN (International Standard Book Number) is a ten digit code which uniquely identifies a book.
The first nine digits represent the Group, Publisher and Title of the book and the last digit is used to check
whether ISBN is correct or not.
Each of the first nine digits of the code can take a value between 0 and 9. Sometimes it is necessary to make
the last digit equal to ten; this is done by writing the last digit of the code as X.
To verify an ISBN, calculate 10 times the first digit, plus 9 times the second digit, plus 8 times the third and so
on until we add 1 time the last digit. If the final number leaves no remainder when divided by 11, the code is
a valid ISBN.
For Example:
1. 0201103311 = 10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 = 55
Since 55 leaves no remainder when divided by 11, hence it is a valid ISBN.
2. 007462542X = 10*0 + 9*0 + 8*7 + 7*4 + 6*6 + 5*2 + 4*5 + 3*4 + 2*2 + 1*10 = 176
Since 176 leaves no remainder when divided by 11, hence it is a valid ISBN.
3. 0112112425 = 10*0 + 9*1 + 8*1 + 7*2 + 6*1 + 5*1 + 4*1 + 3*4 + 2*2 + 1*5 = 71
Since 71 leaves no remainder when divided by 11, hence it is not a valid ISBN.
Design a program to accept a ten digit code from the user. For an invalid input, display an appropriate
message. Verify the code for its validity in the format specified below:
Test your program with the sample data and some random data:
Example 1
INPUT CODE: 0201530821
OUTPUT : SUM = 99
LEAVES NO REMAINDER – VALID ISBN CODE
Example 2
INPUT CODE: 035680324
OUTPUT : INVALID INPUT
Example 3
INPUT CODE: 0231428031
OUTPUT : SUM = 122
LEAVES REMAINDER – INVALID ISBN CODE

PROGRAM
import java.io.*;
public class isbn
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the number");
long n=Long.parseLong(br.readLine());
long i=10,remain=0,sum=0;
while(n>0)
{
remain=n%10;
sum=sum+i*remain;
n=n/10;
i--;}
if(sum%11==0)
{
System.out.println("SUM = "+sum);
System.out.println("LEAVES NO REMAINDER - VALID ISBN CODE");
}
else
{
System.out.println("SUM = "+sum);
System.out.println("LEAVES REMAINDER - INVALID ISBN CODE");
}}}
OUTPUT
enter the number
0201530821
SUM = 143
LEAVES NO REMAINDER - VALID ISBN CODE

VARIABLE DESCRIPTION
VARIABLE TYPE VARIABLE NAME DESCRIPTION
long n Store the no. entered by the user.
long i Initialize its value to 10
long remain to store n%10.
long sum to store the sm of i*remain.

ALGORITHM
Step 1: Start
Step 2: Get the isbn no from the user.
Step 3: Mod the no by 10 and store it in remain.
Step 4: add the remain*I to sum
Step 5:if the sum%11 is equal to 0 then the no is valid isbn no. print the results.
Step 6:Stop

Question 28.
A class ModiString is declared as follows :
Class name: ModiString
Data members/instance variables :
Str1, str2, str3= to store three string of maximum
Ch= character which replaces first of each string
Member functions / methods :
void getstring ( )= to accept 3 strings
void changestr ( )= to accept character 'Ch' and to replace the first character in each string with the
character stored in Ch.
e.g. let 3 strings are
Sime
wide
male

and let value of ch is 't', then the strings should be :


time
tide
tale
void nextstr ( ) : to change the original strings by converting each character
to its successive character.
e.g. let 3 strings are
Sime
wide
male
the strings should be :
tjnf
xjef
nbmf
void show ( ):to display the changed strings by calling both functions changestr ( ) and nextstr ( )
Specify the class ModiString giving details of the constructor and the functions void getstring ( ), void
changestr () and void show()

PROGRAM
import java.util.*;
public class ModiString
{String str1,str2,str3;
char ch;
void getstring()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the three string ");
str1=sc.nextLine();
str2=sc.nextLine();
str3=sc.nextLine();
}
void changestr()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the character ");
ch=sc.next().charAt(0);
String st=str1.substring(1,str1.length());
String st1=str2.substring(1,str2.length());
String st2=str3.substring(1,str3.length());
System.out.println("THE STRING AFTER REPLACEMENT : ");
System.out.println(ch+st);
System.out.println(ch+st1);
System.out.println(ch+st2);
}
void nextstr()
{
String st[]=new String[3];
st[0]=str1;
st[1]=str2;
st[2]=str3;
String st3="",st2;char ch;int ascii;int s=0;
System.out.println("THE STRING AFTER ENCRPTION : ");
while(s<3)
{
st2=st[s];
for(int i=0;i<st2.length();i++)
{
ch=st2.charAt(i);
ascii=(int)ch;
if((ch>='A'&&ch<='Y')||(ch>='a'&&ch<='y'))
{st3=st3+(char)(ascii+1);
}
if(ch=='Z')
{
st3=st3+'A';
}
if(ch=='z')
{
st3=st3+'a';
}}
System.out.println(st3);
s++;
st3="";
}}
void display()
{
ModiString ob=new ModiString();
ob.getstring();
ob.changestr();
ob.nextstr();
}
public static void main(String args[])
{
ModiString ob1=new ModiString();
ob1.display();
}}

OUTPUT
enter the three string
man
can
fan
enter the character
m
THE STRING AFTER REPLACEMENT :
man
man
man
THE STRING AFTER ENCRPTION :
nbo
dbo
gbo

VARIABLE DESCRIPTION
VARIABLE TYPE VARIABLE NAME DESCRIPTION
String str1 To Store the String Entered by the user.
String str2 To Store the String Entered by the user.
String str3 To Store the String Entered by the user.
char ch To store char at(0).
String st To Store the substring of str1.
String st1 To Store the substring of str2.
String st2 To Store the substring of str3.
String st3 To store the sum of ascii.
int ascii To store the value of ch.
int s Used as counter.
int i Forloop variable.

ALGORITHM
Step 1:Start
Step 2: Declare the variables and get the inputs form the user.
Step 3:Find the substring and print it.
Step 4:
Step 5:
Step 6:Stop

Question 29.
class ​Numbers​ contains the following data members and methods to check for triangular numbers.
A ​triangular number​ is formed by the addition of a consecutive sequence of integers starting from 1.
Example:
1+2=3
1+2+3=6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 + 15
Therefore, 3, 6, 10, 15 are triangular numbers.
Class name:​ Numbers
Data members/instance variables:
n: integer to be checked whether it is triangular or not.
Member function/methods:
void getNum(): to accept the integer n.
int check(int): to check if n is triangular.
void display(): to display a suitable message whether n is triangular or not.
Specify the class Numbers giving details of the methods getNum(), check(int) and display().

PROGRAM
import java.util.*;
public class numbers
{
static int n;
void getnum()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the number");
n=sc.nextInt();
}
int check(int sum)
{int k=0,s=0;
for(int i=1;i<n;i++)
{
s=s+i;
if(s==n)
{
k=1;
break;
}}
return(k);
}
void display()
{int t=0;int s=0;
numbers ob=new numbers();
t=ob.check(s);
if(t==1)
{
System.out.println("THE NUMBER "+n+" IS A TRIANGULAR NUMBER");
}
else
{
System.out.println("THE NUMBER "+n+" IS NOT A TRIANGULAR NUMBER");
}}
public static void main(String args[])
{numbers ob1=new numbers();
ob1.getnum();
ob1.display();
}}

OUTPUT
enter the number
6
THE NUMBER 6 IS A TRIANGULAR NUMBER

VARIABLE DESCRIPTION
VARIABLE TYPE VARIABLE NAME DESCRIPTION
int n To store the no. from the user
int k used to return if condition satisfies
int s used to store the sum of i.
int i for loop variable

ALGORITHM
Step 1:Start
Step 2:Decalre a variable n and get its value from the user.
Step 3:check the condition.
Step 4: print the results.
Step 5:Stop

Question 30.
The ​Mobius Function​ M(N) for a natural number N is defined as follows: 
M(N) = 1 if N = 1 
M(N) = 0 if any prime factor of N is contained in N more than once. 
M(N) = -1​p​ if N is a product of p distinct prime factors. 
Example: 
M(78) = -1 (for 78 = 2 * 3 * 13 and M(78) = -1​3​ = -1). 
M(34) = 1 (for 34 = 2 * 17 and M(34) = -1​2​ = 1). 
M(12) = 0 (for 12 = 2 * 2 * 3 and M(12) = 0 for 2 appears two times. 
Write a program to input a positive natural number N and output(N). the user wants
Check your program for n=78, n=34, n=12, n=17, n=666, n=327, n=29 and so on

PROGRAM
import java.util.*;
public class Mobius
{
int n;int a=2,p=0,c=0,q;
void getnum()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the number ");
n=sc.nextInt();
}
int fac(int x)
{
if(x==1)
return 1;
while(x>1)
{
c=0;
while(x%a==0)
{
c++;
p++;
x=x/a;
}
if(c>1)
return 0;
a++;}
q=(int)Math.pow(-1,p);
return q;
}
void display()
{
int r=fac(n);
System.out.println("M("+n+")= "+r);
}
public static void main(String args[])
{Mobius ob=new Mobius();
ob.getnum();
ob.display();
}}

OUTPUT
enter the number
78
M(78)= -1

VARIABLE DESCRIPTION
VARIABLE TYPE VARIABLE NAME DESCRIPTION
int n To get and store the no. from the user.
int a Initialize its value to 2.
int p Used as counter .
int c Used as counter.
int q To store the -1 to the power of p.
int x to store the x/a;
int r to store fac9n0;

ALGORITHM
Step 1:Start
Step 2:Declare the variable ‘n’ and get the value from the user.
Step 3:calculate the fac
Step 4: calculate q.
Step 5: Print the results
Step 6:Stop

You might also like