0% found this document useful (0 votes)
37 views24 pages

Program No

Uploaded by

sc6841562
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
37 views24 pages

Program No

Uploaded by

sc6841562
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 24

INDEX

Progra programs Pg.No.


m no.
1 Write a program to input and store n integers (n > 0) in a
single subscripted variable and print each number with its
frequency. The output should contain number and its
frequency in two different columns.

2 Write a Program in Java to input elements in a 2D square


matrix and check whether it is a Lower Triangular Matrix or
not.

Lower Triangular Matrix: A Lower Triangular matrix is a


square matrix in which all the entries above the main
diagonal [] are zero. The entries below or on the main
diagonal must be non zero values.
3 Write a Program in Java to input elements in a 2-D square
matrix and check whether it is a Scalar Matrix or not.

Scalar Matrix: A scalar matrix is a diagonal matrix where the


left diagonal elements are same.
4 Write a program in Java to enter natural numbers in a
double dimensional array mxn (where m is the number of
rows and n is the number of columns). Shift the elements of
4th column into the 1st column, the elements of 1st column
into the 2nd column and so
on. Display the new matrix
5 Write a program in Java to create a 4 x 4 matrix. Now, swap
the elements of 0th row with 3rd row correspondingly.
Display the result after swapping.

6 A string is given as: Purity of Mind is Essential

Write a program in Java to enter the string. Count and


display:

1. The character with lowest ASCII code in lower case

2. The character with highest ASCII code in lower case

3. The character with lowest ASCII code in upper case

4. The character with highest ASCII code in upper case

7 Write a program in Java to enter a string in a mixed case.


Arrange all the letters of string such that all the lower case
characters are followed by the upper case characters.
8 Write a program in Java to accept two strings. Display the
new string by taking each character of the first string from
left to right and of the second string from right to left. The
letters should be taken alternatively from each string.
Assume that the length of both the strings are same.
9 Write a menu driven program using switch case statement
to find the Arithmetic mean, Geometric mean and
Harmonic mean which are calculated as:

(a) Arithmetic mean = (a + b)/2


(b) Geometric mean = √ab

(c) Harmonic mean = 2ab /(a + b)

10 Write a menu driven program using switch case statement


to perform the given tasks as stated.

Task 1:

A Fibonacci series is given as: 0, 1, 1, 2, 3, 5, 8, 13,.......... and


so on.

Display all the prime Fibonacci numbers in the range from 1


to 1000. For example, 2, 3, 5, 13 are prime fibonacci
numbers.

Task 2:

Prime factors are the factors of a number which are prime


numbers. Display all the prime factors of a number.

11 Write a Program in Java to input a number and check


whether it is a Pronic Number or Heteromecic Number or
not.

Pronic Number: A Pronic number, oblong number,


rectangular number or heteromecic number, is a number
which is the product of two consecutive integers, that is, n
(n + 1).

12 Write a program to accept a list of 20 integers. Sort the first


10 numbers in ascending order and next the 10 numbers in
descending order by using 'Bubble Sort' technique. Finally,
print the complete list of integers
13 The annual examination result of 50 students in a class is
tabulated in a Single Dimensional Array (SDA) is as follows:
Roll No. Subject A Subject B Subject C
….……. ………… ………… ………
….……. ………… ………… ………
….……. ………… ………… ………

Write a program to read the data, calculate and display the


following:
(a) Average marks obtained by each student.

(b) Print the roll number and the average marks of the
students whose average is above. 80.

(c) Print the roll number and the average marks of the
students whose average is below 40.

14 A double dimensional array is defined as N[4][4] to store


numbers. Write a program to find the sum of all even
numbers and product of all odd numbers of the elements
stored in Double Dimensional Array (DDA).
15 Write a program to input a sentence and display the word
of the sentence that contains maximum number of vowels.
16 Write a program to accept a word and convert it into lower
case, if it is in upper case.Display the new word by replacing
only the vowels with the letter following it.
17 An abundant number is a number for which the sum of its
proper divisors (excluding the number itself) is greater than
the original number. Write a program to input number and
check whether it is an abundant number or not.
18 Write a program to input a number and check whether it is
a Harshad Number or not.

[A number is said to be Harshad number, if it is divisible by


the sum of its digits. The program displays the message
accordingly.]
19 Write a program to input twenty names in an array. Arrange
these names in ascending order of letters, using the bubble
sort technique.
20 Write a program to accept 10 names in a Single
Dimensional Array (SDA). Display the names whose first
letter matches with the letter entered by the user
programs

1. import java.util.Scanner;
public class KboatSDAFrequency{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();

if (n <= 0) {
System.out.println("Invalid Input! n should be greater than 0.");
return;
}

int arr[] = new int[n];

System.out.println("Enter array elements:");


for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}

//Sort the array


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}

System.out.println("Number\tFrequency");
int count = 0;
for (int i = 0; i < n - 1; i++) {
count++;
if (arr[i] != arr[i + 1]) {
System.out.println(arr[i] + "\t" + count);
count = 0;
}
}

//Print frequency of last element


count++;
System.out.println(arr[n - 1] + "\t" + count);
}//main close
}//class close

2. import java.util.Scanner;
public class LowerTriangularMatrix{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the size of the matrix: ");
int n = in.nextInt();

int arr[][] = new int[n][n];

System.out.println("Enter elements of the matrix: ");


for (int i = 0; i < n; i++) {
System.out.println("Enter Row "+ (i+1) + " :");
for (int j = 0; j < n; j++) {
arr[i][j] = in.nextInt();
}
}

System.out.println("The Matrix is:");


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}

boolean isTriangular = true;


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((i < j && arr[i][j] != 0)
|| (i >= j && arr[i][j] == 0)) {
isTriangular = false;
break;
}
}

if (!isTriangular) {
break;
}
}

if (isTriangular) {
System.out.println("The Matrix is Lower Triangular");
}
else {
System.out.println("The Matrix is not Lower Triangular");
}
}}

3. import java.util.Scanner;
public class KboatScalarMatrix{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the size of the matrix: ");
int n = in.nextInt();

int arr[][] = new int[n][n];

System.out.println("Enter elements of the matrix: ");


for (int i = 0; i < n; i++) {
System.out.println("Enter Row "+ (i+1) + " :");
for (int j = 0; j < n; j++) {
arr[i][j] = in.nextInt();
}
}

System.out.println("The Matrix is:");


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}

boolean isScalar = true;


int x = arr[0][0];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((i == j && arr[i][j] != x)
|| ( i != j && arr[i][j] != 0)) {
isScalar = false;
break;
}
}

if (!isScalar) {
break;
}
}

if (isScalar) {
System.out.println("The Matrix is Scalar");
}
else {
System.out.println("The Matrix is not Scalar");
}
}}

4. import java.util.Scanner;
public class KboatSDAColShift{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of rows (m): ");
int m = in.nextInt();
System.out.print("Enter number of columns (n): ");
int n = in.nextInt();

int arr[][] = new int[m][n];


int newArr[][] = new int[m][n];

System.out.println("Enter array elements");


for (int i = 0; i < m; i++) {
System.out.println("Enter Row "+ (i+1) + " :");
for (int j = 0; j < n; j++) {
arr[i][j] = in.nextInt();
}
}

System.out.println("Input Array:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}

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


int col = j + 1;
if (col == n) {
col = 0;
}
for (int i = 0; i < m; i++) {
newArr[i][col] = arr[i][j];
}
}

System.out.println("New Shifted Array:");


for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(newArr[i][j] + "\t");
}
System.out.println();
}
}}

5. import java.util.Scanner;
public class DDARowSwap{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[][] = new int[4][4];

System.out.println("Enter elements of 4x4 array ");


for (int i = 0; i < 4; i++) {
System.out.println("Enter Row "+ (i+1) + " :");
for (int j = 0; j < 4; j++) {
arr[i][j] = in.nextInt();
}
}

System.out.println("Input Array:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}

//Swap 0th and 3rd rows


for (int j = 0; j < 4; j++) {
int t = arr[0][j];
arr[0][j] = arr[3][j];
arr[3][j] = t;
}

System.out.println("Swapped Array:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}

6. import java.util.Scanner;
public class ASCIICode{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the string:");
String str = in.nextLine();
int len = str.length();
char lowerCaseLow = 255;
char lowerCaseHigh = 0;
char upperCaseLow = 255;
char upperCaseHigh = 0;

for (int i = 0; i < len; i++) {


char ch = str.charAt(i);
if (Character.isLowerCase(ch)) {
if (ch < lowerCaseLow) {
lowerCaseLow = ch;
}

if (ch > lowerCaseHigh) {


lowerCaseHigh = ch;
}
}
else if (Character.isUpperCase(ch)) {
if (ch < upperCaseLow) {
upperCaseLow = ch;
}

if (ch > upperCaseHigh) {


upperCaseHigh = ch;
}
}
}

System.out.println("The character with lowest ASCII code in


lower case: "
+ lowerCaseLow);
System.out.println("The character with highest ASCII code in
lower case: "
+ lowerCaseHigh);
System.out.println("The character with lowest ASCII code in
upper case: "
+ upperCaseLow);
System.out.println("The character with highest ASCII code in
upper case: "
+ upperCaseHigh);
}}

7. import java.util.Scanner;
public class KboatStringLowerThenUpper{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter string: ");
String str = in.nextLine();
int len = str.length();
StringBuffer sbLowerCase = new StringBuffer();
StringBuffer sbUpperCase = new StringBuffer();

for (int i = 0; i < len; i++) {


char ch = str.charAt(i);
if (Character.isLowerCase(ch))
sbLowerCase.append(ch);
else if (Character.isUpperCase(ch))
sbUpperCase.append(ch);
}

System.out.println("Input String:");
System.out.println(str);
String newStr = sbLowerCase.append(sbUpperCase).toString();
System.out.println("Changed String:");
System.out.print(newStr);
}}

8. import java.util.Scanner;
public class KboatString{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first String: ");
String s1 = in.nextLine();
System.out.print("Enter second String: ");
String s2 = in.nextLine();
String newStr = "";
int len = s1.length();

for (int i = 0; i < len; i++) {


char ch1 = s1.charAt(i);
char ch2 = s2.charAt(len - 1 - i);
newStr = newStr + ch1 + ch2;
}

System.out.println(newStr);
}}

9. import java.util.Scanner;
public class KboatMean {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 1 for Arithmetic mean");
System.out.println("Enter 2 for Geometric mean");
System.out.println("Enter 3 for Harmonic mean");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter b: ");
int b = in.nextInt();
double mean = 0.0;
switch (choice) {
case 1:
mean = (a + b) / 2.0;
System.out.println("Arithmetic mean = " + mean);
break;

case 2:
mean = Math.sqrt(a * b);
System.out.println("Geometric mean = " + mean);
break;

case 3:
mean = 2.0 * a * b / (a + b);
System.out.println("Harmonic mean = " + mean);
break;

default:
System.out.println("Incorrect Choice!!!");
}
}
}
10. import java.util.Scanner;
public class KboatFibonacciPrimeFactors{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 1 Prime Fibonacci Numbers");
System.out.println("Enter 2 Prime factors");
System.out.print("Enter your choice: ");
int choice = in.nextInt();

switch (choice) {
case 1:
int a = 0, b = 1;
int term = a + b;
while (term <= 1000) {
/*
* Check if the this term of
* Fibonacci Series is Prime
* or not
*/
int c = 0;
for (int i = 1; i <= term; i++) {
if (term % i == 0) {
c++;
}
}

if (c == 2)
System.out.print(term + " ");

a = b;
b = term;
term = a + b;
}
break;

case 2:
System.out.print("Enter Number: ");
int num = in.nextInt();
System.out.print("The prime factors of " + num + " are:
");
for(int i = 2; i < num; i++) {
while (num % i == 0) {
System.out.print(i + " ");
num = num / i;
}
}
if(num > 2) {
System.out.println(num);
}
break;

default:
System.out.println("Incorrect Choice!!!");
}
}
}
11. import java.util.Scanner;
public class KboatPronicNumber{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int num = in.nextInt();

boolean isPronic = false;

for (int i = 1; i <= num - 1; i++) {


if (i * (i + 1) == num) {
isPronic = true;
break;
}
}

if (isPronic)
System.out.println(num + " is a pronic number");
else
System.out.println(num + " is not a pronic number");
}}
12. import java.util.Scanner;
public class KboatSDABubbleSort{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];
System.out.println("Enter 20 numbers:");

for (int i = 0; i < arr.length; i++) {


arr[i] = in.nextInt();
}

//Sort first half in ascending order


for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = 0; j < arr.length / 2 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}

//Sort second half in descending order


for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = arr.length / 2; j < arr.length - i - 1; j++)
{
if (arr[j] < arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}

//Print the final sorted array


System.out.println("\nSorted Array:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}}
13. import java.util.Scanner;
public class KboatExamResult{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 50;
Scanner in = new Scanner(System.in);

int rollNo[] = new int[TOTAL_STUDENTS];


int sA[] = new int[TOTAL_STUDENTS];
int sB[] = new int[TOTAL_STUDENTS];
int sC[] = new int[TOTAL_STUDENTS];
double avg[] = new double[TOTAL_STUDENTS];

for (int i = 0; i < TOTAL_STUDENTS; i++) {


System.out.println("Enter student " + (i+1) + "
details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
System.out.print("Subject A Marks: ");
sA[i] = in.nextInt();
System.out.print("Subject B Marks: ");
sB[i] = in.nextInt();
System.out.print("Subject C Marks: ");
sC[i] = in.nextInt();
avg[i] = (sA[i] + sB[i] + sC[i]) / 3.0;
}

System.out.println("\nRoll No\tAverage Marks");


for (int i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println(rollNo[i] + "\t" + avg[i]);
}

System.out.println("\nStudents with Average above 80:");


for (int i = 0; i < TOTAL_STUDENTS; i++) {
if (avg[i] > 80)
System.out.println(rollNo[i] + "\t" + avg[i]);
}

System.out.println("\nStudents with Average below 40:");


for (int i = 0; i < TOTAL_STUDENTS; i++) {
if (avg[i] < 40)
System.out.println(rollNo[i] + "\t" + avg[i]);
}
}}
14 import java.util.Scanner;
public class KboatDDAEvenOdd{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N[][] = new int[4][4];
long evenSum = 0, oddProd = 1;
System.out.println("Enter the elements of 4x4 DDA: ");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
N[i][j] = in.nextInt();
if (N[i][j] % 2 == 0)
evenSum += N[i][j];
else
oddProd *= N[i][j];
}
}

System.out.println("Sum of all even numbers = " + evenSum);


System.out.println("Product of all odd numbers = " + oddProd);
}}
15. import java.util.Scanner;
public class KboatMaxVowelWord{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();

str = str + " ";


String word = "", mWord = "";
int count = 0, maxCount = 0;
int len = str.length();

for (int i = 0; i < len; i++) {

char ch = Character.toUpperCase(str.charAt(i));

if (ch == 'A' ||
ch == 'E' ||
ch == 'I' ||
ch == 'O' ||
ch == 'U') {
count++;
}

if (ch == ' ') {


if (count > maxCount) {
maxCount = count;
mWord = word;
}
word = "";
count = 0;
}
else {
word += ch;
}
}

System.out.println("The word with maximum number of vowels: "


+ mWord);
}}
16. import java.util.Scanner;
public class KboatVowelReplace{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a word: ");
String str = in.nextLine();
str = str.toLowerCase();
String newStr = "";
int len = str.length();

for (int i = 0; i < len; i++) {


char ch = str.charAt(i);

if (str.charAt(i) == 'a' ||
str.charAt(i) == 'e' ||
str.charAt(i) == 'i' ||
str.charAt(i) == 'o' ||
str.charAt(i) == 'u') {

char nextChar = (char)(ch + 1);


newStr = newStr + nextChar;

}
else {
newStr = newStr + ch;
}
}

System.out.println(newStr);
}}
17. import java.util.Scanner;
public class KboatAbundantNumber{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = in.nextInt();
int sum = 0;

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


if (n % i == 0)
sum += i;
}

if (sum > n)
System.out.println(n + " is an abundant number");
else
System.out.println(n + " is not an abundant number");
}}
18. import java.util.*;
public class KboatHarshadNumber{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = in.nextInt();
int x = num, rem = 0, sum = 0;

while(x > 0) {
rem = x % 10;
sum = sum + rem;
x = x / 10;
}

int r = num % sum;

if(r == 0)
System.out.println(num + " is a Harshad Number.");
else
System.out.println(num + " is not a Harshad Number.");
}}
19. import java.util.Scanner;
public class KboatArrangeNames{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String names[] = new String[20];
System.out.println("Enter 20 names:");
for (int i = 0; i < names.length; i++) {
names[i] = in.nextLine();
}

//Bubble Sort
for (int i = 0; i < names.length - 1; i++) {
for (int j = 0; j < names.length - 1 - i; j++) {
if (names[j].compareToIgnoreCase(names[j + 1]) > 0) {
String temp = names[j + 1];
names[j + 1] = names[j];
names[j] = temp;
}
}
}

System.out.println("\nSorted Names");
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
}}
20. import java.util.Scanner;
public class KboatSDANames{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String names[] = new String[10];
System.out.println("Enter 10 names");

for (int i = 0; i < names.length; i++) {


names[i] = in.nextLine();
}

System.out.print("Enter a letter: ");


char ch = in.next().charAt(0);
ch = Character.toUpperCase(ch);

for (int i = 0; i < names.length; i++) {


if (Character.toUpperCase(names[i].charAt(0)) == ch) {
System.out.println(names[i]);
}
}
}}

You might also like