0% found this document useful (0 votes)
8 views49 pages

Document 6

The document contains a comprehensive index of Java programming exercises, covering various topics such as patterns, series, sorting algorithms, and string manipulations. Each section includes a program, its data types, variables, descriptions, and expected outputs. The exercises aim to enhance understanding of Java programming concepts through practical implementation.

Uploaded by

krishnanov92007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views49 pages

Document 6

The document contains a comprehensive index of Java programming exercises, covering various topics such as patterns, series, sorting algorithms, and string manipulations. Each section includes a program, its data types, variables, descriptions, and expected outputs. The exercises aim to enhance understanding of Java programming concepts through practical implementation.

Uploaded by

krishnanov92007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INDEX

[Link] TITLE PAGE


NO.
1 TRIANGLE PATTERN 1-2

2 RECTANGLE PATTERN 3-4

3 SQUARE PATTERN 5-6

4 NUMBER SERIES 7-8

5 SUM OF SERIES 9-10

6 BUBBLE SORTING 11-13


7 SUM OF ELEMENT IN ARRAY 14-16

8 WEIGHT SORTING 17-20

9 INITIALIZING AN ARRAY 21-21

10 ACCESSING ELEMENT IN ARRAY 22-22

11 STRING REVERSE CHARACTER 23-26

12 STRING DISPLAY SURNAME FIRST 27-28

13 STRING REVERSE 29-31


14 STRING DISPLAY THE LONGEST 32-34
WORD
15 STRING ASCII ALPHABET 35-36

16 SPY NUMBER 37-38

17 ABUNDANT NUMBER 39-40

18 PRIME NUMBER 41-42

19 PALINDROME NUMBER 43-44

20 ARMSTRONG NUMBER 45-48


PROGRAM 1
WRITE A PROGRAM IN JAVA TO PRINT TRIANGLE

public class TrianglePattern

public static void main(String[] args)

int rows = 5;

for (int i = 1; i <= rows; i++)

for (int j = 1; j <= i; j++)

[Link]("* ");
}
[Link]();

DATA TYPE VARIABLE DISCRIPTION

int row No. of rows in a triangle

int i Outer loop counter to control row


printing

int j Inner loop counter to print * in each


row

Output
*
**
***
****
*****
PROGRAM 2
WRITE A PROGRAM IN JAVA TO PRINT RECTANGLE

public class RectanglePattern

public static void main(String[] args)

int rows = 4;
int columns = 6;

for (int i = 1; i <= rows; i++)

{
for (int j = 1; j <= columns; j++)
{
[Link]("* ");
}
[Link]();

}
}

DATA TYPE VARIABLE DISCRIPTION

int row No. of rows in a rectangle

int column No. of columns for the rectangle

int i Loop counter for rows

int j Loop counter for column

Output
******
******
******
******
PROGRAM 3
WRITE A PROGRAM IN JAVA TO PRINT SQUARE
public class SquarePattern
{
public static void main(String[] args)
{
int size = 5;
for (int i = 1; i <= size; i++)
{
for (int j = 1; j <= size; j++)
{
[Link]("* ");
}
[Link]();
}
DATA TYPE VARIABLE DISCRIPTION

int row No. of rows in a square

int column No. of columns for the square

int i Loop counter for rows

int j Loop counter for column

Output
*****
*****
*****
*****
*****
PROGRAM 4
WRITE A PROGRAM IN JAVA TO PRINT THE
FOLLOWING SERIES 22 33 44 55…..
public class NumberSeries
{
public static void main(String[] args)
{
int n = 5;
int num = 1;
for (int i = 1; i <= n; i++)
{
[Link](num + " ");
num = num * 10 + 1;
}
}
}
DATA TYPE VARIABLE DISCRIPTION

int n No. of terms series

int num Store the current no. of series

int i Loop counter to repeat n terms

Output
11 22 33 44 55
PROGRAM 5
WRITE A PROGRAM IN JAVA TO FIND THE SUM OF THE
GIVEN SERIES: S= a+a2/2+a3/3+….+a10/10
import [Link];
public class Kboatseries
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
[Link]("Enter a: ");
int a = [Link]();
double sum = 0.0;
for (int i = 1; i <= 10; i++)
{
sum += [Link](a, i) / i;
}
[Link]("Sum = " + sum);
}
}
DATA TYPE VARIABLE DISCRIPTION

int a Input the value given by the user

double sum Store the sum of the series

it i Loop counter from 1 to 10, use as


both exponent and denominator in
the series
Output
Enter a: 2
Sum: 205.3916015625
PROGRAM 6
WRITE A PROGRAM IN JAVA FOR BUBBLE SORTING
public class BubbleSort
{
public static void main(String[] args)
{

int[] X = {34, 7, 23, 32, 5, 62, 12, 2, 45, 19};


[Link]("Array before sorting:");
printArray(X);
for (int i = 0; i < [Link] - 1; i++)
{
for (int j = 0; j < [Link] - 1 - i; j++)
{
if (X[j] > X[j + 1])
{
int temp = X[j];
X[j] = X[j + 1];
X[j + 1] = temp;
}
}
}
[Link]("Array after sorting:");
printArray(X);
}
public static void printArray(int[] arr)
{
for (int value : arr)
{
[Link](value + " ");
}
[Link]();
}
}
DATA TYPE VARIABLE DISCRIPTION

int[] X Array of integer to be sorted

int i Outer loop counter for Bubble Sort

int j Inner loop counter for comparing


adjacent element

int temp Temporary variable used for swapping


elements

int[] arr Parameter of printArray() method,


refer to array being printed

int value Each element of the array used in


enhance for-loop for display

Output
Array before sorting:
34 7 23 32 5 62 12 2 45 19
Array after sorting:
2 5 7 12 19 23 32 34 45 62
PROGRAM 7
WRITE A PROGRAM IN JAVA THAT CALCULATES THE
SUM OF ELEMENTS AT EVEN OR ODD INDEXES OF AN
ARRAY
public class ArraySum
{
public static void main(String[] args)
{

int[] arr = new int[20];


for (int i = 0; i < [Link]; i++)
{
arr[i] = i + 1;
}
[Link]("Array elements: ");
for (int i = 0; i < [Link]; i++)
{
[Link](arr[i] + " ");
}
[Link]();
int evenIndexSum = 0;
int oddIndexSum = 0;
for (int i = 0; i < [Link]; i++)
{
if (i % 2 == 0)
{
evenIndexSum += arr[i];
}
else
{
oddIndexSum += arr[i];
}
}
[Link]("Sum of elements at even indexes: " +
evenIndexSum);
[Link]("Sum of elements at odd indexes: " +
oddIndexSum);
}
}
DATA TYPE VARIABLE DISCRIPTION

int[] arr Array of 20 integers initializer for 1 to


20

int i Loop counter for iterating through


array indexes

int evenIndexSum Stores sum of elements at even


indexes

int oddIndexSum Stores sum of elements at odd


indexes

Output
Array elements: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
19 20
Sum of elements at even indexes: 110
Sum of elements at odd indexes: 100
PROGRAM 8
WRITE A PROGRAM IN JAVA FOR WEIGHT SORTER
import [Link];

public class WeightSorter {

public static void main(String[] args)


{
Scanner scanner = new Scanner([Link]);
double[] weights = new double[10];
[Link]("Enter the weights of 10 people:");
for (int i = 0; i < [Link]; i++) {
[Link]("Weight " + (i + 1) + ": ");
weights[i] = [Link]();
}
for (int i = 0; i < [Link] - 1; i++)
{
int maxIndex = i;
for (int j = i + 1; j < [Link]; j++)
{
if (weights[j] > weights[maxIndex])
{
maxIndex = j;
}
}
double temp = weights[maxIndex];
weights[maxIndex] = weights[i];
weights[i] = temp;
}
[Link]("\nWeights in descending order:");
for (double weight : weights)
{
[Link](weight);
}

[Link]()
}
}
DATA TYPE VARIABLE DISCRIPTION

double[] weights Array to store weights of 10 peaple

int i Outer loop counter for selecting sort

int j Inner loop counter for selection sort

int maxIndex Index of the maximum value found in


the unsorted part of the array

double temp Temporary variable for swapping


values during sorting

double weight Using in the enhanced for-loop to


print each sorted weights

Output
Enter the weights of 10 people:
Weight 1: 65.5
Weight 2: 72.3
Weight 3: 59.0
Weight 4: 80.0
Weight 5: 68.2
Weight 6: 70.0
Weight 7: 74.6
Weight 8: 66.4
Weight 9: 61.0
Weight 10: 69.5

Weights in descending order:


80.0
74.6
72.3
70.0
69.5
68.2
66.4
65.5
61.0
59.0
PROGRAM 9
WRITE A PROGRAM IN JAVA TO CREATE AND
INITIALIZING AN ARRAY
public class Main
{
public static void main(String[] args)
{
int[] myArray = {1, 2, 3, 4, 5};
[Link]("Array created with elements: " +
[Link](myArray));
}
}

DATA TYPE VARIABLE DISCRIPTION

int[] myArray An integer array initializer with values

Output
Array created with elements: [1, 2, 3, 4, 5]
PROGRAM 10
WRITE A PROGRAM IN JAVA TO ECCESSING ELEMENTS
OF AN ARRAY
public class Main
{
public static void main(String[] args)
{
int[] myArray = {1, 2, 3, 4, 5};
[Link]("The first element is: " + myArray[1]);
[Link]("The fourth element is: " + myArray[4]);
}
}

DATA TYPE VARIABLE DISCRIPTION

int[] myArray An integer array initializer with values

Output
The first element is: 2
The fourth element is: 5
PROGRAM 11
WRITE A PROGRAM TO ACCEPT A STRING.
DISPLAY THE NEW STRING AFTER REVERSING
EACH CHARACTER OF THE WORD.
import [Link].*;
class New_Delhi
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
String s,st="",pw="";char c,ch=' ';int i,len,l,j;
[Link]("Enter the String");
s=[Link]();
s=s+" ";
l=[Link]();
for(i=0;i<l;i++)
{
c=[Link](i);
if(c!=' ')
{
st=st+c;//create word
}
else
{
len=[Link]();
for(j=(len-1);j>=0;j--)
{
ch=[Link](j);
pw=pw+ch;
}
[Link](pw+" ");
st="";pw="";
}
}
}
}
DATA TYPE VARIABLE DISCRIPTION

String s Used to take input from the user

String st Temporary string to store each word


being formed

String pw Reversed version of the word st

char c Character at the current index of the


string

char ch Character used during reversing of


the word

int i Loop variable for scanning the full


input string

int len Length of the current word stored in


st

int l Length of the input string with extra


space added

int j Loop variable for reversing characters


in each word

Output
Enter the String
New Delhi Is Beautiful
weN ihleD sI lufituaeB
PROGRAM 12
WRITE A PROGRAM IN JAVA TO ACCEPT A NAME
CONTAINING THREE WORDS AND DISPLAY THE
SURNAME FIRST, FOLLOWED BY THE FIRST AND
MIDDLE NAMES.
import [Link].*;
public class Gandhi
{
public static void main(String args[])
{
String st, sn="", st1="", st2="";
int i,p;
char chr;
Scanner in=new Scanner([Link]);
[Link]("Enter a full name");
st=[Link]();
p=[Link](' ');
sn=[Link](p+1);
st1=[Link](0,p);
st2=sn+" " +st1;
[Link]("Name as initial with surnamae:");
[Link](st2);
}
}
DATA TYPE VARIABLE DISCRIPTION

String st Input full name entered by the user

String sn Store the last word (Surname)from


the full name

String st 1 Stores the remaining part of the full


name

String St 2 Rearrange string with surname


followed by remaining name

char chr Declared but not used in this program

int i Declared but not used in this program

int p Position of the last space in the full


name

Output
Enter a full name
Mahatma Karamchand Gandhi
Name as initial with surname:
Gandhi Mahatma Karamchand
PROGRAM 13
WRITE A PROGRAM IN JAVA TO ACCEPT A
SENTENCE. DISPLAY THE SENTENCE IN REVERSEING
ORDER OF ITS WORD.
import [Link].*;
public class Computer_Fun
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
[Link]("Enter a sentence:");
String str = [Link]();
str = " " + str;
String word = "";
int len = [Link]();
for (int i = len - 1; i >= 0; i--)
{
char ch = [Link](i);
if (ch == ' ')
{
[Link](word + " ");
word = "";
}
else
{
word = ch + word;
}
}
}
}

DATA TYPE VARIABLE DISCRIPTION

String str Stores the extra sentences with extra


spaces

String word Temporarily stores each word while


reversing

int len Stores the length of the string str

int i Loop counter used to traverse the


string in reverse

char chr Store the character at the i-th index of


the string

Output
Enter a sentence:
I love Java
Java love I
PROGRAM 14
WRITE A PROGRAM TO ENTER A STRING AND
DISPLAY THE LONGEST WORD.
import [Link].*;
class long_word
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
[Link]("Enter string:");
String s=[Link]();
s=s+' ';String w=" "; String lw=" ";
int l=[Link]();
for( int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch!=' ')
{
w=w+ch;
}
else
{
if([Link]()>[Link]())
{
lw=w; }
w="";
}
}
[Link]("Longest word"+lw);
}
}

DATA TYPE VARIABLE DISCRIPTION

String s Store the input string with an added


space at the end

String w Temporarily store the current word


being build

String lw Stores the longest word found so far

int l Stores the length of string s

int i Loop counter used to traverse the


string

char ch Stores the character at the i-th index


of the string
Output
Enter string:
Java programming is fun
Longest word: programming
PROGRAM 15
WRITE A PROGRAM TO PRINT ASCII ALPHABETS
public class AsciiAlphabet
{
public static void main(String[] args)
{
[Link]("Alphabet\tASCII Code");
for (char c = 'a'; c <= 'z'; c++)
{
[Link](c + "\t\t" + (int) c);
}
}
}

DATA TYPE VARIABLE DISCRIPTION

char c Loop variable used to iterate through


alphabets from ‘a’ to ‘z’

int (int)c Type casting the character c to its


ASCII value

Output
Alphabet ASCII Code
a 97
b 98
c 99
d 100
e 101
f 102
g 103
h 104
i 105
j 106
k 107
l 108
m 109
n 110
o 111
p 112
q 113
r 114
s 115
t 116
u 117
v 118
w 119
x 120
y 121
z 122
PROGRAM 16
WRITE A PROGRAM TO CHECK WEITHER THE NO.
IS A SPY NO. OR NOT
import [Link];

public class KboatSpyNumber


{
public void spyNumCheck() {
Scanner in = new Scanner([Link]);
[Link]("Enter Number: ");
int num = [Link]();
int digit, sum = 0;
int orgNum = num;
int prod = 1;
while (num > 0)
{
digit = num % 10;

sum += digit;
prod *= digit;
num /= 10;
}
if (sum == prod)
[Link](orgNum + " is Spy Number");
else
[Link](orgNum + " is not Spy Number");

}
}

DATA TYPE VARIABLE DISCRIPTION

int num Stores the no enter by the user

int orgNum Stores the original no. entered

int digit Stores each digit of the no. during


extraction

int sum Stores the sum of the digit of the no.

int prod Stores the product of the digit of the


no.

Output
Enter Number: 123
123 is Spy Number
PROGRAM 17
WRITE A PROGRAM TO CHECK WEITHER THE NO.
IS ABUNDANT NO. OR NOT
import [Link];

public class KboatAbundantNumber


{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
[Link]("Enter the number: ");
int n = [Link]();
int sum = 0;

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


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

if (sum > n)
[Link](n + " is an abundant number");
else
[Link](n + " is not an abundant number");
}
}

DATA TYPE VARIABLE DISCRIPTION

int n Stores the no enter by the user

int sum Stores the sum of the no.

int i Loop counter use to check all the no.


less than n for divisibility

Output
Enter the number: 12
12 is an abundant number
PROGRAM 18
WRITE A PROGRAM TO CHECK WEITHER THE NO.
IS PRIME NO. OR NOT
import [Link];
public class KboatPrimeCheck
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
[Link]("Enter number: ");
int n = [Link]();
int c = 0;
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
{
c++;
}
}

if (c == 2)
{
[Link](n + " is a prime number");
}
else
{
[Link](n + " is not a prime number");
}
}
}

DATA TYPE VARIABLE DISCRIPTION

int n Stores the no enter by the user

int c Counter to count how many no. divide


n exactly

int i Loop counter to check all no. from 1


to n for divisibility

Output
Enter number: 7
7 is a prime number
PROGRAM 19
WRITE A PROGRAM TO CHECK WEITHER THE NO.
IS PALINDROME NO. OR NOT
import [Link];
public class KboatPalindromeNumber
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
[Link]("Enter the number: ");
int num = [Link]();
int copyNum = num;
int revNum = 0;
while(copyNum != 0)
{
int digit = copyNum % 10;
copyNum /= 10;
revNum = revNum * 10 + digit;
}
if (revNum == num)
[Link]("A Palindrome number");
else
[Link]("Not a Palindrome number");
}
}

DATA TYPE VARIABLE DISCRIPTION

int num The no. entered by the user

int copyNum A copy of num, used for reversing


without altering the original

int revNum Store the reverse of the no.

int digit Store the last digit of the copyNum in


each loop iteration

Output
Enter the number:
121
A Palindrome number
PROGRAM 20
WRITE A PROGRAM TO CHECK WEITHER THE NO.
IS ARMSTRONG NO. OR NOT
import [Link];
public class KboatArmstrongNumber
{
public int armstrong(int n)
{
int num = n, cubeSum = 0;
while (num > 0)
{
int digit = num % 10;
cubeSum = cubeSum + (digit * digit * digit);
num /= 10;
}

if (cubeSum == n)
return 1;
else
return 0;
}

public static void main(String args[]) {


Scanner in = new Scanner([Link]);
[Link]("Enter Number: ");
int num = [Link]();

ArmstrongNumber obj = new ArmstrongNumber();


int r = [Link](num);

if (r == 1)
[Link](num + " is an Armstrong number");
else
[Link](num + " is not an Armstrong number");
}
}
DATA TYPE VARIABLE DISCRIPTION

int num The no. entered by the user

int r Store the result returned by the


armstrong() method

int n Passed to Armstrong() method;


copy to input no.

int cubeSum Sum of cube of digit of the no.

int digit Store the last digit of the no.


during loop

int num (inside Local copy of n used for digit


method) extraction and cube sum
calculation

ArmstrongNumber obj Object of the class to call the


armstrong() method

Output
Enter Number: 153
153 is an Armstrong number

You might also like