Assignment – 7
Question -1
Write a program to display all prime palindrome numbers between 10 and 1000.
[Hint: A number which is prime as well a palindrome is said to be 'Prime Palindrome' number.]
For example: 11, 101, 131, 151,
public class KboatPrimePalindrome
{
public void displayPrimePalindrome() {
int count = 0;
for (int i = 10; i <= 1000; i++) {
int num = i, revNum = 0;
while (num != 0) {
int digit = num % 10;
num /= 10;
revNum = revNum * 10 + digit;
}
if (revNum == i) {
boolean isPrime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(i + " ");
count++;
if (count == 10) {
System.out.println();
count = 0;
}
}
}
}
}
}
Output
Variable declaration table
Variable Name Variable Type/Data type Description
Question 2
In an entrance examination, students have been appeared in English, Maths and Science papers. Write a
program to calculate and display average marks obtained by all the students. Take number of students appeared
and marks obtained in all three subjects by every student along with the name as inputs.
Display the name, marks obtained in three subjects and the average of all the students.
import java.util.Scanner;
public class KboatStudentMarks
{
public void studentMarks() {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int studentCount = in.nextInt();
String names[] = new String[studentCount];
int engMarks[] = new int[studentCount];
int sciMarks[] = new int[studentCount];
int mathsMarks[] = new int[studentCount];
double avgMarks[] = new double[studentCount];
double totalMarks = 0.0;
for (int i = 0; i < studentCount; i++) {
System.out.println("Enter details of student " + (i + 1));
System.out.print("Name: ");
in.nextLine();
names[i] = in.nextLine();
System.out.print("Marks in English: ");
engMarks[i] = in.nextInt();
System.out.print("Marks in Science: ");
sciMarks[i] = in.nextInt();
System.out.print("Marks in Maths: ");
mathsMarks[i] = in.nextInt();
avgMarks[i] = (engMarks[i] + sciMarks[i] + mathsMarks[i]) / 3.0;
totalMarks += avgMarks[i];
}
System.out.println();
for (int i = 0; i < studentCount; i++) {
System.out.println("Details of student " + (i + 1));
System.out.println("Name: " + names[i]);
System.out.println("English: " + engMarks[i]);
System.out.println("Science: " + sciMarks[i]);
System.out.println("Maths: " + mathsMarks[i]);
System.out.println("Average: " + avgMarks[i]);
}
double classAvg = totalMarks / studentCount;
System.out.println("\nAverage of all students is " + classAvg);
}
}
Output
Variable declaration table
Variable Name Variable Type/Data type Description
Question 3
Write a program in Java to enter a number containing three digits or more. Arrange the digits of the entered
number in ascending order and display the result.
Sample Input: Enter a number 4972
Sample Output: 2, 4, 7, 9
import java.util.Scanner;
public class KboatDigitSort
{
public void sortDigits() {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number having 3 or more digits: ");
int OrgNum = in.nextInt();
for (int i = 0; i <= 9; i++) {
int num = OrgNum;
int c = 0;
while (num != 0) {
if (num % 10 == i)
c++;
num /= 10;
}
for (int j = 1; j <= c; j++) {
System.out.print(i + ", ");
}
}
System.out.println();
}
}
Output
Variable declaration table
Variable Name Variable Type/Data type Description
Question 4
Write a program to input a number and check whether it is 'Magic Number' or not. Display the message
accordingly.
A number is said to be a magic number if the eventual sum of digits of the number is one.
Sample Input : 55
Then, 5 + 5 = 10, 1 + 0 = 1
Sample Output: Hence, 55 is a Magic Number.
Similarly, 289 is a Magic Number.
import java.util.Scanner;
public class KboatMagicNum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number to check: ");
int num = in.nextInt();
int n = num;
while (n > 9) {
int sum = 0;
while (n != 0) {
int d = n % 10;
n /= 10;
sum += d;
}
n = sum;
}
if (n == 1)
System.out.println(num + " is Magic Number");
else
System.out.println(num + " is not Magic Number");
}
}
Output
Variable declaration table
Variable Name Variable Type/Data type Description
Question 5
A number is said to be Multiple Harshad number, when divided by the sum of its digits, produces another
'Harshad Number'. Write a program to input a number and check whether it is a Multiple Harshad Number or
not.
(When a number is divisible by the sum of its digit, it is called 'Harshad Number').
Sample Input: 6804
Hint: 6804 ⇒ 6+8+0+4 = 18 ⇒ 6804/18 = 378
378 ⇒ 3+7+8= 18 ⇒ 378/18 = 21
21 ⇒ 2+1 = 3 ⇒ 21/3 = 7
Sample Output: Multiple Harshad Number
import java.util.Scanner;
public class KboatMultipleHarshad
{
public void checkMultipleHarshad() {
Scanner in = new Scanner(System.in);
System.out.print("Enter number to check: ");
int num = in.nextInt();
int dividend = num;
int divisor;
int count = 0;
while (dividend > 1) {
divisor=0;
int t = dividend;
while (t > 0) {
int d = t % 10;
divisor += d;
t /= 10;
}
if (dividend % divisor == 0 && divisor != 1) {
dividend = dividend / divisor;
count++;
}
else {
break;
}
}
if (dividend == 1 && count > 1)
System.out.println(num + " is Multiple Harshad Number");
else
System.out.println(num + " is not Multiple Harshad Number");
}
}
Output
Variable declaration table
Variable Name Variable Type/Data type Description
Question 6
Write the programs to display the following patterns:
(a)
1
31
531
7531
97531
public class KboatPattern
{
public void displayPattern() {
for (int i = 1; i < 10; i = i + 2) {
for (int j = i; j > 0; j = j - 2) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Output
Question 5
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15
public class KboatPattern
{
public void displayPattern() {
int a = 1;
for (int i = 5; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(a++ + "\t");
}
System.out.println();
}
}
}
Output
Variable declaration table
Variable Name Variable Type/Data type Description
Question 7
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
public class KboatPattern
{
public void displayPattern() {
int a = 15;
for (int i = 5; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(a-- + "\t");
}
System.out.println();
}
}
}
Output
Variable declaration table
Variable Name Variable Type/Data type Description
Question 8
1
10
101
1010
10101
public class KboatPattern
{
public void displayPattern() {
int a = 1, b = 0;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
System.out.print(b + " ");
else
System.out.print(a + " ");
}
System.out.println();
}
}
}
Output
Variable declaration table
Variable Name Variable Type/Data type Description
Question 9
55555
4444
333
22
1
public class KboatPattern
{
public void displayPattern() {
for (int i = 0; i < 5; i++) {
for (int j = i; j > 0; j--) {
System.out.print(" ");
}
for (int k = 5 - i; k > 0; k--) {
System.out.print((5 - i) + " ");
}
System.out.println();
}
}
}
Output
Variable declaration table
Variable Name Variable Type/Data type Description
Question 10
12345
22345
33345
44445
55555
public class KboatPattern
{
public void displayPattern() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j < i; j++)
System.out.print(i + " ");
for (int k = i; k <= 5; k++)
System.out.print(k + " ");
System.out.println();
}
}
}
Output
Variable declaration table
Variable Name Variable Type/Data type Description
Question 11
*
*#
*#*
*#*#
*#*#*
public class KboatPattern
{
public void displayPattern() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
System.out.print("# ");
else
System.out.print("* ");
}
System.out.println();
}
}
}
Output
Variable declaration table
Variable Name Variable Type/Data type Description
Question 12
54321
5432
543
54
5
public class KboatPattern
{
public void displayPattern() {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Output
Variable declaration table
Variable Name Variable Type/Data type Description
Question 13
(i)
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
public class KboatPattern
{
public void displayPattern() {
int a = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(a++ + "\t");
}
System.out.println();
}
}
}
Output
Question 14
Write a program to generate a triangle or an inverted triangle till n terms based upon the user's choice.
Example 1:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 1
Enter the number of terms 5
Sample Output:
1
22
333
4444
55555
Example 2:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 2
Enter the number of terms 6
Sample Output:
666666
55555
4444
333
22
1
import java.util.Scanner;
public class KboatPattern
{
public void choosePattern() {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for a triangle");
System.out.println("Type 2 for an inverted triangle");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
System.out.print("Enter the number of terms: ");
int n = in.nextInt();
switch (ch) {
case 1:
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
break;
case 2:
for (int i = n; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
Output
Variable declaration table
Variable Name Variable Type/Data type Description
Question 15
Using the switch statement, write a menu driven program for the following:
(a) To print the Floyd's triangle:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
(b) To display the following pattern:
I
IC
ICS
ICSE
For an incorrect option, an appropriate error message should be displayed.
import java.util.Scanner;
public class KboatPattern
{
public void choosePattern() {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Floyd's triangle");
System.out.println("Type 2 for an ICSE pattern");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) {
case 1:
int a = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(a++ + "\t");
}
System.out.println();
}
break;
case 2:
String s = "ICSE";
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j <= i; j++) {
System.out.print(s.charAt(j) + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
Output
Variable declaration table
Variable Name Variable Type/Data type Description