Question
Write a program to input a natural number less than 1000 and display it in words.
Example 1:
INPUT :
29
OUTPUT :
TWENTY NINE
Example 2:
INPUT :
17001
OUTPUT :
INVALID INPUT
Example 3:
INPUT :
119
OUTPUT :
ONE HUNDRED NINETEEN
Example 4:
INPUT :
500
OUTPUT :
FIVE HUNDRED
Program
import [Link].*;
class ISC11Q1 {
int n;
String str;
String ones[] = {"", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"};
String teens[] = {"TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN",
"SEVENTEEN", "EIGHTEEN", "NINETEEN"};
String tens[] = {"", "", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY",
"NINETY"};
ISC11Q1(int num) {
n = num;
str = "";
}
void check() {
if (!(n > 0 && n < 1000)) {
[Link]("Invalid Input");
[Link](0);
}
}
void process() {
int h = n / 100;
int t = n % 100;
if (h > 0) {
str += ones[h] + " HUNDRED";
if (t > 0) {
str += " ";
}
}
if (t > 0) {
if (t >= 20) {
str += tens[t / 10];
if (t % 10 > 0) {
str += " " + ones[t % 10];
}
} else if (t > 9 && t < 20) {
str += teens[t - 10];
} else {
str += ones[t];
}
}
}
void display() {
[Link](str);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("ENTER A NUMBER: ");
int num = [Link]();
[Link]("NUMBER IN WORDS:-");
ISC11Q1 obj = new ISC11Q1(num);
[Link]();
[Link]();
[Link]();
}
}
Algorithm
Step 1 : START
Step 2 : ACCEPT a number and STORE into n
Step 4 : CALL process()
Step 5 : IF n < 1 OR n > 999 THEN
Step 6 : PRINT “INVALID INPUT”
Step 7 : GO TO Step XX
Step 8 : ENDIF
Step 9 : INITIALIZE units and tens string arrays
Step 10: CALCULATE hundreds digit h = n / 100
Step 11: CALCULATE remainder r = n % 100
Step 12: IF h > 0 THEN
Step 13: SET s = units[h] + ' HUNDRED'
Step 14: ENDIF
Step 15: IF h > 0 AND r > 0 THEN
Step 16: SET s = s + ' AND '
Step 17: ENDIF
Step 18: IF r > 0 THEN
Step 19: IF r < 20 THEN
Step 20: SET s = s + units[r]
Step 21: ELSE
Step 22: CALCULATE tens digit t = r / 10
Step 23: CALCULATE units digit u = r % 10
Step 24: SET s = s + tens[t]
Step 25: IF u > 0 THEN
Step 26: SET s = s + ' ' + units[u]
Step 27: ENDIF
Step 28: ENDIF
Step 29: ENDIF
Step 30: CALL display()
Step 31: DISPLAY s
Step 32: STOP
Variable Description
Data Type Variable Name Description
int n To store the input number.
String s To store the final word representation.
int a Used as the constructor argument for the input number.
String[] units Array to store words for numbers 1-19.
String[] tens Array to store words for multiples of ten (20, 30, etc.).
int h To store the hundreds digit of the number.
int r To store the remainder after extracting the hundreds part.
int t To store the tens digit of the remainder.
int u To store the units digit of the remainder.
Scanner sc To read input from the user.
int inputNum To store the number read from the scanner.
ISC11Q1 obj Object of the class.
Input/Output
Test 1:
INPUT :
29
OUTPUT :
TWENTY NINE
Test 2:
INPUT :
17001
OUTPUT :
OUT OF RANGE
Test 3:
INPUT :
119
OUTPUT :
ONE HUNDRED AND NINETEEN
Test 4:
INPUT :
500
OUTPUT :
FIVE HUNDRED
Question
Encryption is a technique of coding messages to maintain their secrecy.
Write a program to accept the size of string array of size 'n' where n is greater than 1 and less than
10, and which stores single sentences (each sentence ends with a full stop) in each row of the
array. Display an appropriate message if the size is not satisfying the given condition.
Define a string array of the inputted size and fill it with sentences row-wise. Change the sentence
of the odd rows with an encryption of two characters ahead of the original characters. Also change
the sentence of the even rows by storing the sentence in the reverse order.
Display the encrypted sentences as per the sample data given below:
Example 1:
INPUT :
n=4
IT IS CLOUDY.
IT MAY RAIN.
THE WEATHER IS FINE.
IT IS COOL.
OUTPUT :
KV KU ENQWFA.
RAIN MAY IT.
VJG YGCVJGT KU HKPG.
COOL IS IT.
Example 2:
INPUT :
n = 13
OUTPUT :
INVALID ENTRY
Program
import [Link].*;
class ISC11Q2 {
int n;
String s[];
ISC11Q2(int a) {
n = a;
s = new String[n];
}
void check() {
if (n < 2 || n >= 10) {
[Link]("Invalid Input");
[Link](0);
}
}
String encrypt(String str) {
String res = "";
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if ([Link](ch)) {
char base = [Link](ch) ? 'A' : 'a';
ch = (char) (((ch - base + 2) % 26) + base);
}
res += ch;
}
return res;
}
String reverse(String str) {
String res = "";
str = [Link](0, [Link]() - 1); // remove the full stop
String word = "";
String words[] = new String[[Link]()];
int wc = 0;
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if (ch != ' ') {
word += ch;
} else {
words[wc++] = word;
word = "";
}
}
if ([Link]() > 0) {
words[wc++] = word;
}
for (int i = wc - 1; i >= 0; i--) {
res += words[i] + " ";
}
return [Link]() + ".";
}
void process() {
for (int i = 0; i < n; i++) {
if ((i + 1) % 2 != 0) {
[Link](encrypt(s[i]));
} else {
[Link](reverse(s[i]));
}
}
}
void display() {
process();
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("n=");
int size = [Link]([Link]());
ISC11Q2 obj = new ISC11Q2(size);
[Link]();
for (int i = 0; i < obj.n; i++) {
obj.s[i] = [Link]();
}
[Link]();
}
}
Algorithm
Step 1 : START
Step 2 : DECLARE integer n, string array s
Step 3 : READ array size n
Step 4 : IF n < 2 OR n >= 10 THEN
Step 5 : DISPLAY 'INVALID ENTRY'
Step 6 : STOP
Step 7 : ENDIF
Step 8 : CREATE string array s of size n
Step 9 : FOR i = 0 TO n-1
Step 10: READ sentence into s[i]
Step 11: ENDFOR
Step 12: CALL process()
Step 13: FOR i = 0 TO n-1
Step 14: IF (i+1) is odd THEN
Step 15: CALL encrypt(s[i]) and DISPLAY returned value
Step 16: ELSE
Step 17: CALL reverse(s[i]) and DISPLAY returned value
Step 18: ENDIF
Step 19: ENDFOR
Step 20: STOP
Variable Description
Data Type Variable Name Description
int n To store the size of the string array.
String[] s To store the input sentences.
int a Used as the constructor argument for the array size.
String str Parameter to hold the string in encrypt/reverse methods.
String res To build the resulting string in encrypt/reverse methods.
char ch To hold each character of the string during encryption.
char base To determine the base character ('A' or 'a') for encryption logic.
String[] words Array to store words of a sentence for reversal.
int i Loop control variable.
Scanner sc To read input from the user.
int size To store the array size read from the scanner.
ISC11Q2 obj Object of the class.
Input/Output
Test 1:
INPUT :
n=4
IT IS CLOUDY.
IT MAY RAIN.
THE WEATHER IS FINE.
IT IS COOL.
OUTPUT :
KV KU ENQWFA.
NIAR YAM TI
VJG YGCVJGT KU HKPG.
LOOC SI TI
Test 2:
INPUT :
n = 13
OUTPUT :
INVALID ENTRY
Question
Design a program which accepts your date of birth in DD MM YYYY format.
Check whether the date entered is a valid date or not. If it is valid, display "valid date".
Also compute and display the day number of the year for the date of birth.
If it is invalid, display "INVALID DATE" and then terminate the program.
Example 1:
INPUT :
Enter your date of birth in DD MM YYYY format.
05
01
2010
OUTPUT :
VALID DATE
5
Example 2:
INPUT :
Enter your date of birth in DD MM YYYY format.
03
04
2010
OUTPUT :
VALID DATE
93
Example 3:
INPUT :
Enter your date of birth in DD MM YYYY format.
34
06
2010
OUTPUT :
INVALID DATE
Program
import [Link].*;
public class Encode
{
String str;
int shift;
Encode(String s, int n)
{
str = s;
shift = n;
}
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence (ending with a full stop): ");
String s = [Link]();
[Link]("Enter the shift value: ");
int n = [Link]();
Encode obj = new Encode(s, n);
[Link]();
[Link]();
}
void check()
{
int len = [Link]();
if(len == 0 || [Link](len - 1) != '.')
{
[Link]("INVALID INPUT");
[Link](0);
}
}
void process()
{
String encrypted = encrypt();
String reversed = reverse(encrypted);
display(reversed);
}
String encrypt()
{
String res = "";
for(int i = 0; i < [Link](); i++)
{
char ch = [Link](i);
if(ch >= 'A' && ch <= 'Z')
{
ch = (char)(((ch - 'A' + shift) % 26) + 'A');
}
else if(ch >= 'a' && ch <= 'z')
{
ch = (char)(((ch - 'a' + shift) % 26) + 'a');
}
res = res + ch;
}
return res;
}
String reverse(String sentence)
{
String res = "";
int len = [Link]();
String word = "";
for(int i = 0; i < len; i++)
{
char ch = [Link](i);
if(ch != ' ' && ch != '.')
{
word = word + ch;
}
else
{
for(int j = [Link]() - 1; j >= 0; j--)
{
res = res + [Link](j);
}
if(ch == ' ')
{
res = res + " ";
}
word = "";
}
}
res = res + ".";
return res;
}
void display(String s)
{
[Link](s);
}
}
Algorithm
Step 1 : START
Step 2 : DECLARE integers d, m, y
Step 3 : READ d, m, y from user
Step 4 : CALL processAndDisplay()
Step 5 : CALL isValid()
Step 6 : IF isValid() returns true THEN
Step 7 : DISPLAY 'VALID DATE'
Step 8 : CALL getDayNumber() and store result in n
Step 9 : DISPLAY n
Step 10: ELSE
Step 11: DISPLAY 'INVALID DATE'
Step 12: ENDIF
Step 13: STOP
Variable Description
Data Type Variable Name Description
int d To store the day of the month.
int m To store the month of the year.
int y To store the year.
int a, b, c Constructor arguments for day, month, and year.
int[] daysInMonth Array to store the number of days in each month.
int dayNum To store the calculated day number of the year.
int i Loop control variable.
BufferedRe br To read line-based input from the user.
ader
int day, month, Local variables in main to read input.
year
ISC11Q3 obj Object of the class.
Input/Output
Test 1:
INPUT :
05
01
2010
OUTPUT :
VALID DATE
5
Test 2:
INPUT :
03
04
2010
OUTPUT :
VALID DATE
93
Test 3:
INPUT :
34
06
2010
OUTPUT :
INVALID DATE
Test 4:
INPUT :
29
02
2023
OUTPUT :
INVALID DATE
Test 5:
INPUT :
29
02
2024
OUTPUT :
VALID DATE
60