0% found this document useful (0 votes)
17 views13 pages

Find Smallest Integer with Digit Sum

AI

Uploaded by

xagopen600
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)
17 views13 pages

Find Smallest Integer with Digit Sum

AI

Uploaded by

xagopen600
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

Question

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

Example 3
INPUT:
M = 99
N = 11
OUTPUT:
INVALID INPUT

Example 4
INPUT:
M = 112
N = 130
OUTPUT:
INVALID INPUT
Program
import [Link].*;

class ISC15Q1 {
int m, n, r, d;

ISC15Q1(int mm, int nn) {


m = mm;
n = nn;
r = 0;
d = 0;
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("INPUT:");
[Link]("M = ");
int a = [Link]();
[Link]("N = ");
int b = [Link]();

ISC15Q1 obj = new ISC15Q1(a, b);


[Link]();
[Link]();
[Link]();
}

void check() {
if (m < 100 || m > 10000 || n <= 0 || n >= 100) {
[Link]("OUTPUT:");
[Link]("INVALID INPUT");
[Link](0);
}
}

void process() {
int c = m + 1;
while (true) {
if (sum(c) == n) {
r = c;
break;
}
c++;
}
int t = r;
while (t > 0) {
d++;
t /= 10;
}
}

int sum(int x) {
int s = 0;
while (x > 0) {
s += x % 10;
x /= 10;
}
return s;
}
void display() {
[Link]("OUTPUT:");
[Link]("The required number = " + r);
[Link]("Total number of digits = " + d);
}
}Algorithm

Step 1: START
Step 2: DECLARE integer variables m, n, r, d
Step 3: DEFINE function sumDigits(num)
Step 4: INITIALIZE sum = 0
Step 5: WHILE num > 0 DO
Step 6: sum = sum + num MOD 10
Step 7: num = num / 10
Step 8: ENDWHILE
Step 9: RETURN sum
Step 10: DEFINE function process()
Step 11: INITIALIZE c = m + 1
Step 12: WHILE true DO
Step 13: IF CALL sumDigits(c) equals n THEN
Step 14: SET r = c
Step 15: BREAK loop
Step 16: ENDIF
Step 17: INCREMENT c
Step 18: ENDWHILE
Step 19: INITIALIZE t = r, d = 0
Step 20: WHILE t > 0 DO
Step 21: INCREMENT d
Step 22: SET t = t / 10
Step 23: ENDWHILE
Step 24: DEFINE main method
Step 25: DISPLAY message to enter M and N
Step 26: READ m_in, n_in
Step 27: IF m_in >= 100 AND m_in <= 10000 AND n_in < 100 AND n_in > 0 THEN
Step 28: CREATE object with m_in, n_in
Step 29: CALL process()
Step 30: CALL display() which prints r and d
Step 31: ELSE
Step 32: DISPLAY "INVALID INPUT"
Step 33: ENDIF
Step 34: STOP
Variable Description
Data Type Variable Name Description
int m Stores the user-provided lower bound number.
int n Stores the user-provided target sum of digits.
int r Stores the required smallest integer found.
int d Stores the total number of digits in the required number.

Input/Output
Test 1
INPUT:
M = 100
N = 11
OUTPUT:
THE REQUIRED NUMBER = 119
TOTAL NUMBER OF DIGITS = 3

Test 2
INPUT:
M = 1500
N = 25
OUTPUT:
THE REQUIRED NUMBER = 1699
TOTAL NUMBER OF DIGITS = 4

Test 3
INPUT:
M = 99
N = 11
OUTPUT:
INVALID INPUT

Test 4
INPUT:
M = 112
N = 130
OUTPUT:
INVALID INPUT
Question
Write a program to declare a square matrix A[][] of order MxM where 'M' is the number of rows
and the number of columns, such that M must be greater than 2 and less than 10. Accept the
value of M as user input.
Display an appropriate message for an invalid input. Allow the user to input integers into this
matrix.
Perform the following tasks:
(a) Display the original matrix.
(b) Rotate the matrix 90° clockwise as shown below:
Original matrix 123 Rotated matrix 741

456 852

789 963

(c) Find the sum of the elements of the four corners of the matrix.
Test your program for the following data and some random data:

Example 1
INPUT:
M=3
123
456
789
OUTPUT:
ORIGINAL MATRIX
1 2 3
4 5 6
7 8 9
MATRIX AFTER ROTATION
7 4 1
8 5 2
9 6 3
SUM OF THE CORNER ELEMENTS = 20
Example 2
INPUT:
M=4
2946
3579
9546
3867
OUTPUT:
ORIGINAL MATRIX
2 9 4 6
3 5 7 9
9 5 4 6
3 8 6 7
MATRIX AFTER ROTATION
3 9 3 2
8 5 5 9
6 4 7 4
7 6 9 6
SUM OF THE CORNER ELEMENTS = 18
Example 3
INPUT:
M = 14
OUTPUT:
SIZE OUT OF RANGE
Program
import [Link].*;

class ISC15Q2 {
int m;
int[][] a;

ISC15Q2(int mm) {
m = mm;
a = new int[m][m];
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("INPUT:");
[Link]("M = ");
int x = [Link]();
ISC15Q2 obj = new ISC15Q2(x);
[Link]();
[Link](sc);
[Link]();
}

void check() {
if (m <= 2 || m >= 10) {
[Link]("OUTPUT:");
[Link]("SIZE OUT OF RANGE");
[Link](0);
}
}

void process(Scanner sc) {


[Link]("Enter array elements:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = [Link]();
}
}
}

int cornersum() {
return a[0][0] + a[0][m - 1] + a[m - 1][0] + a[m - 1][m - 1];
}

void display() {
[Link]("OUTPUT:");
[Link]("ORIGINAL MATRIX");
print();

[Link]("MATRIX AFTER ROTATION");


rotate();

[Link]("Sum of the corner elements = " + cornersum());


}

void print() {
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
[Link](a[i][j] + "\t");
}
[Link]();
}
}

void rotate() {
for (int i = 0; i < m; i++) {
for (int j = m - 1; j >= 0; j--) {
[Link](a[j][i] + "\t");
}
[Link]();
}
}
}Algorithm

Step 1: START
Step 2: DECLARE integer m, 2D integer array a
Step 3: DEFINE function inputMatrix()
Step 4: DISPLAY message to enter array elements
Step 5: FOR i from 0 to m-1
Step 6: FOR j from 0 to m-1
Step 7: READ a[i][j]
Step 8: ENDFOR
Step 9: ENDFOR
Step 10: DEFINE function calculateCornerSum()
Step 11: RETURN a[0][0] + a[0][m-1] + a[m-1][0] + a[m-1][m-1]
Step 12: DEFINE function display()
Step 13: DISPLAY "ORIGINAL MATRIX"
Step 14: CALL printMatrix(a)
Step 15: DISPLAY "MATRIX AFTER ROTATION"
Step 16: CALL rotateAndPrint()
Step 17: SET sum = CALL calculateCornerSum()
Step 18: DISPLAY "Sum of the corner elements = " + sum
Step 19: DEFINE main method
Step 20: DISPLAY message to enter M
Step 21: READ m_in
Step 22: IF m_in > 2 AND m_in < 10 THEN
Step 23: CREATE object with m_in
Step 24: CALL inputMatrix()
Step 25: CALL display()
Step 26: ELSE
Step 27: DISPLAY "SIZE OUT OF RANGE"
Step 28: ENDIF
Step 29: STOP
Variable Description
Data Type Variable Name Description
int m Stores the order (number of rows/columns) of the square matrix.
int[][] a Stores the integer elements of the square matrix.

Input/Output
Test 1
INPUT:
M=3
123
456
789
OUTPUT:
ORIGINAL MATRIX
1 2 3
4 5 6
7 8 9
MATRIX AFTER ROTATION
7 4 1
8 5 2
9 6 3
SUM OF THE CORNER ELEMENTS = 20
Test 2
INPUT:
M=4
2946
3579
9546
3867
OUTPUT:
ORIGINAL MATRIX
2 9 4 6
3 5 7 9
9 5 4 6
3 8 6 7
MATRIX AFTER ROTATION
3 9 3 2
8 5 5 9
6 4 7 4
7 6 9 6
SUM OF THE CORNER ELEMENTS = 18
Test 3
INPUT:
M = 14
OUTPUT:
SIZE OUT OF RANGE
Question
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. Print an error message if the input does not terminate
with ‘.’ or ‘?’. You can assume that no word in the sentence exceeds 15 characters, so that you get
a proper formatted output.
Perform the following tasks:
(i) Convert the first letter of each word to uppercase.
(ii) Find the number of vowels and consonants in each word and display them with proper
headings along with the words.
Test your program with the following inputs.

Example 1
INPUT:
Intelligence plus character is education.
OUTPUT:
Intelligence Plus Character Is Education.
Word Vowels Consonants
Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4
Example 2
INPUT:
God is great.
OUTPUT:
God Is Great.
Word Vowels Consonants
God 1 2
Is 1 1
Great 2 3
Example 3
INPUT:
All the best!
OUTPUT:
Invalid Input.
Program
import [Link].*;
import [Link].*;

class ISC15Q3 {
String s;
String[] w;
int[] v, c;
int l;

ISC15Q3(String ss) {
s = [Link](0, [Link]() - 1);
StringTokenizer st = new StringTokenizer(s);
l = [Link]();
w = new String[l];
for (int i = 0; i < l; i++) {
w[i] = [Link]();
}
v = new int[l];
c = new int[l];
}

public static void main(String[] args) throws IOException {


BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter Sentence:");
String t = [Link]();

if (t == null || [Link]() == 0) {
[Link]("Invalid Input.");
return;
}

char ch = [Link]([Link]() - 1);


if (ch != '.' && ch != '?') {
[Link]("Invalid Input.");
return;
}

[Link]("INPUT: " + t);


ISC15Q3 obj = new ISC15Q3(t);
[Link]();
[Link]();
[Link](ch);
}

void check() {
if ([Link]() == 0) {
[Link]("OUTPUT:");
[Link]("INVALID INPUT");
[Link](0);
}
}

void process() {
for (int i = 0; i < l; i++) {
count(i);
cap(i);
}
}
void count(int p) {
String t = w[p].toLowerCase();
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if ("aeiou".indexOf(ch) != -1) {
v[p]++;
} else if ([Link](ch)) {
c[p]++;
}
}
}

void cap(int p) {
w[p] = [Link](w[p].charAt(0)) + w[p].substring(1);
}

void display(char ch) {


[Link]("OUTPUT:");
for (int i = 0; i < l; i++) {
[Link](w[i] + " ");
}
[Link](ch);

[Link]("\nWord\t\tVowels\tConsonants");
for (int i = 0; i < l; i++) {
if (w[i].length() > 7) {
[Link](w[i] + "\t" + v[i] + "\t" + c[i]);
} else {
[Link](w[i] + "\t\t" + v[i] + "\t" + c[i]);
}
}
}
}Algorithm

Step 1: START
Step 2: DECLARE String s, String array w, integer arrays v and c, integer l
Step 3: DEFINE function process()
Step 4: FOR i from 0 to l-1
Step 5: CALL countVowelsAndConsonants(i)
Step 6: CALL capitalizeWord(i)
Step 7: ENDFOR
Step 8: DEFINE function countVowelsAndConsonants(index)
Step 9: FOR each character in word w[index]
Step 10: IF character is a vowel THEN
Step 11: INCREMENT v[index]
Step 12: ELSE IF character is a consonant THEN
Step 13: INCREMENT c[index]
Step 14: ENDIF
Step 15: ENDFOR
Step 16: DEFINE function capitalizeWord(index)
Step 17: CONVERT first character of w[index] to uppercase
Step 18: DEFINE main method
Step 19: DISPLAY message to enter a sentence
Step 20: READ sentence
Step 21: GET last character of sentence
Step 22: IF last character is NOT '.' AND NOT '?' THEN
Step 23: DISPLAY "Invalid Input."
Step 24: STOP
Step 25: ENDIF
Step 26: CREATE object with sentence
Step 27: CALL process()
Step 28: CALL display() to print capitalized sentence and table
Step 29: STOP
Variable Description
Data Type Variable Name Description
String s Stores the input sentence without the trailing punctuation.
String[] w Stores the words extracted from the sentence.
int[] v Stores the count of vowels for each corresponding word.
int[] c Stores the count of consonants for each corresponding word.
int l Stores the total number of words in the sentence.

Test 1
INPUT:
Intelligence plus character is education.
OUTPUT:
Intelligence Plus Character Is Education.
Word Vowels Consonants
Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4
Test 2
INPUT:
God is great.
OUTPUT:
God Is Great.
Word Vowels Consonants
God 1 2
Is 1 1
Great 2 3
Test 3
INPUT:
All the best!
OUTPUT:
Invalid Input.

You might also like