0% found this document useful (0 votes)
12 views23 pages

Star Patterns in Java Programming

The document contains multiple Java programs that perform various tasks such as calculating the sum of even and odd numbers, counting characters, reversing numbers, checking for palindromes, and generating Fibonacci series. Each program includes its source code and output results, showcasing fundamental programming concepts. Overall, it serves as a collection of basic algorithms and operations in Java.
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)
12 views23 pages

Star Patterns in Java Programming

The document contains multiple Java programs that perform various tasks such as calculating the sum of even and odd numbers, counting characters, reversing numbers, checking for palindromes, and generating Fibonacci series. Each program includes its source code and output results, showcasing fundamental programming concepts. Overall, it serves as a collection of basic algorithms and operations in Java.
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

PROGRAM

SUM OF EVEN NUMBER


package [Link];
public class Corejava {
public static void main(String[] args) {
int a = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
a = a + i;
}
}
[Link](a);
}
}

OUTPUT:
30
SUM OF ODD NUMBER
package [Link];
public class Corejava {
public static void main(String[] args) {
int a = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 1) {
a = a + i;
}
}
[Link](a);
}
}

OUTPUT:
25
COUNT OF EVEN NUMBER
package [Link];
public class Corejava {
public static void main(String[] args) {
int a = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
a = a + 1;
}
}
[Link](a);
}
}

OUTPUT:
5

COUNT OF ODD NUMBER


package [Link];
public class Corejava {
public static void main(String[] args) {
int a = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 1) {
a = a + 1;
}
}
[Link](a);
}
}

OUTPUT:
5

REVERSE NUMBER

package [Link];
public class Corejava {
public static void main(String[] args) {
int num = 1234,i=0,j=0;
while(num>0) {
i=num%10;
j=(j*10)+i;
num=num/10;
}
[Link](j);
}
}

OUTPUT:
4321
PALINDRONE

package [Link];
public class Corejava {
public static void main(String[] args) {
int num = 121,i=0,j=0;
int reverse=num;
while(num>0) {
i=num%10;
j=(j*10)+i;
num=num/10;
}
if(reverse==j) {
[Link]("Palindrone");
}
else
{
[Link]("Not palindrone");
}
}
}

OUTPUT:
Palindrone

SUM OF DIGITS OF A NUMBER

package [Link];
public class Corejava {
public static void main(String[] args) {
int num = 1234,i=0,j=0;
while(num>0) {
i=num%10;
j=j+i;
num=num/10;
}
[Link](j);
}
}

OUTPUT:
10
COUNT OF DIGITS
package [Link];
public class Corejava {
public static void main(String[] args) {
int num = 1234,i=0,j=0;
while(num>0) {
i=num%10;
j=j+1;
num=num/10;
}
[Link](j);
}
}
OUTPUT:
4

AMSTRONG NUMBER
package [Link];
public class Corejava {
public static void main(String[] args) {
int num = 153,i=0,j=0;
int reverse=num;
while(num>0) {
i=num%10;
j=j+(i*i*i);
num=num/10;
}
[Link](j);
if (reverse==j) {
[Link]("Amstrong");
}
else {
[Link]("Not Amstrong");
}
}
}

OUTPUT:
153
Amstrong
SWAPPING OF 2 NUMBERS USING 3 NUMBER

package [Link];
public class Corejava {
public static void main(String[] args) {
int a =10,b=23,c;
c=a;
a=b;
b=c;
[Link]("a value is "+a+"\n"+"b
value is "+b);
}
}

OUTPUT:
a value is 23
b value is 10
SWAPPING OF 2 NUMBERS WITHOUT 3
NUMBER

package [Link];
public class Corejava {
public static void main(String[] args) {
int a=10, b=20;
[Link]("before swapping: "+a +"\n"+
"before swapping: "+b);
a=a+b;
b=a-b;
a=a-b;
[Link]("after swapping: "+a +"\n"+
"after swapping: "+b);
}
}

OUTPUT:
before swapping: 10
before swapping: 20
after swapping: 20
after swapping: 10

FIBONACCI SERIES

package [Link];
public class Corejava {
public static void main(String[] args) {
int a=0,b=1,c;
for (int i = 0; i <=10; i++) {
c=a+b;
a=b;
b=c;
[Link](c);
}
}
}
OUTPUT:
1
2
3
5
8
13
21
34
55
89
144

FACTORIAL

package [Link];
public class Corejava {
public static void main(String[] args) {
int a=1;
for (int i = 1; i <=5; i++) {
a=a*i;
}
[Link](a);
}
}

OUTPUT:
120

COUNT EACH CHARACTER OF STRING OR


OCCURANCE COUNT

package [Link];
import [Link];
import [Link];
public class Corejava {
public static void main(String[] args) {
String s ="yaashith";
Map<Character,Integer> m = new HashMap<>();
char[] ch = [Link]();
for (char c : ch) {
if([Link](c))
{
int x = [Link](c);
[Link](c, x+1);
}
else {
[Link](c, 1);
}
}
[Link](m);
}
}

OUTPUT:
{a=2, s=1, t=1, h=2, y=1, i=1}

REMOVE SPACE AND PRINT STRING

package [Link];
public class Corejava {
public static void main(String[] args) {
String s ="yaashith kumar";
String x = [Link](" ", "");
[Link](x);
}
}

OUTPUT:
Yaashithkumar

COUNT OF EACH WORD

package [Link];
import [Link];
import [Link];
public class Corejava {
public static void main(String[] args) {
String s ="yaashith kumar yaashith";
String [] x = [Link](" ");
HashMap<String, Integer> m = new HashMap<>();
for (String c : x) {
if ([Link](c)) {
int i =[Link](c);
[Link](c, i+1);
}
else
{
[Link](c,1);
}
}
[Link](m);
}
}

OUTPUT:
{yaashith=2, kumar=1}

TRIANGLE PROGRAM

package [Link];;
public class Corejava {
public static void main(String[] args) {
for (int i = 1; i <=5; i++) {
for (int j = 1; j <=i; j++) {
[Link]("*");
}
[Link]();
}
}
}

OUTPUT:
*
**
***
****
*****
REVERSE TRIANGLE

package [Link];;
public class Corejava {
public static void main(String[] args) {
for (int i = 1; i <=5; i++) {
for (int j = 5; j >=i; j--) {
[Link]("*");
}
[Link]();
}
}
}

OUTPUT:
****
***
**
*

VOWEL AND CONSTANT

package [Link];;
public class Corejava {
public static void main(String[] args) {
String a ="Yaashith";
int v=0, n=0;
for (int i = 0; i < [Link](); i++) {
char ch =[Link](i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
v++;
}
else {
n++;
}
}
[Link]("Vowel is "+v);
[Link]("Constant is "+n);
}
}

OUTPUT:
Vowel is 3
Constant is 5
SORTING AN ARRAY
package [Link];
import [Link];
public class Corejava {
public static void main(String[] args) {
int array[] = {10,6,3,2};
[Link](array);
[Link]("sorting elements:");
for (int i = 0; i < [Link]; i++) {
[Link](array[i]);
}
}
}
OUTPUT:
sorting elements:
2
3
6
10

MAXIMUM NUMBER IN AN ARRAY

package [Link];
import [Link];
public class Corejava {
public static void main(String[] args) {
int array[] = {10,324,45,9808};
int max=[Link](array).max().getAsInt();
[Link](max);
}
}
OUTPUT:
9808
PRIME NUMBER

package [Link];
public class Corejava {
public static void main(String[] args) {
int num=100;
boolean flag =false;
for (int i = 2; i <=num/2; i++) {
if (num%i==0) {
[Link]("its not a prime number");
flag=true;
break;
}
}
if(!flag) {
[Link]("its a prime number");
}
}
}

OUTPUT:
its not a prime number

SECOND LARGEST NUMBER IN AN ARRAY

package [Link];
import [Link];
public class Corejava {
public static void main(String[] args) {
int temp,size;
int array[]= {10,20,30,40,50};
for (int i = 0; i < [Link]; i++) {
for (int j = i+1; j < [Link]; j++) {
if(array[i]>array[j]) {
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
[Link](array[[Link]-2]);
}
}

OUTPUT:
40

COUNT THE STRING OF VOWEL AND


CONSTANT, SYMBOL AND SPECIAL
CHARECTER
package [Link];
public class Hierarchial {
public static void main(String[] args) {
String s = "preethi120598@[Link]";
int count=0,constant=0,num=0,special=0;
for (int j = 0; j < [Link](); j++) {
char ch = [Link](j);
if (ch>='a' && ch<='z') {
if (ch=='a' || ch=='e' || ch=='i' || ch=='o' ||
ch=='u') {
count++;
}
else {
constant++;
}
}
else if (ch>='0' && ch<='9') {
num++;
}
else
{
special++;
}
}
[Link](count);
[Link](constant);
[Link](num);
[Link](special);
}
}

FLAMES

package [Link];
import [Link];
public class Corejava {
public static void main(String[] args) {
Scanner s = new Scanner([Link]);
[Link]("Enter the girl name:");
String girl = [Link]();
[Link]("Enter the boy name");
String boy = [Link]();
String girl1 = new String(girl);
String boy1 = new String(boy);
int length = 0;
for (int i = 0; i < [Link](); i++) {
for (int j = 0; j < [Link](); j++) {
if ([Link](i) == [Link](j)) {
String strgirl = removeCharAt1(girl, i);
String strboy = removeCharAt1(boy, j);
girl = strgirl;
boy = strboy;
i--;
j--;
break;
}
}
}
int lengthgirl = [Link]();
int lengthboy = [Link]();
length = lengthgirl + lengthboy;
[Link](length);
String str = "FLAMES";
int lengthone = [Link]();
int lengthfinal = lengthone - 2;
for (int i = 0; i <= lengthfinal; i++) {
int firstlength = length % lengthone;
if (firstlength == 0) {
String str1 = removeCharAt(str, lengthone - 1);
str = str1;
int lengthsecond = [Link]();
lengthone = lengthsecond;
} else {
String str1 = removeCharAt(str, firstlength -
1);
str = str1;
int lengthsecond = [Link]();
lengthone = lengthsecond;
}
}
[Link](str);
switch (str) {
case "F":
[Link](girl1 + "is Friend to " +
boy1);
break;
case "L":
[Link](girl1 + " is Love to " +
boy1);
break;
case "A":
[Link](girl1 + " is Affection to "
+ boy1);
break;
case "M":
[Link](girl1 + " is Marriage to " +
boy1);
break;
case "E":
[Link](girl1 + " is Enemies to " +
boy1);
break;
case "S":
[Link](girl1 + " is Sister to " +
boy1);
break;
}
}

public static String removeCharAt(String str,


int i) {
return [Link](i + 1) + [Link](0,
i);
}

public static String removeCharAt1(String str,


int i) {
return [Link](0, i) + [Link](i +
1);
}
}

ASSIGN CHARACTER VALUE TO INTEGER

package [Link];
public class Dog {
static int i;
static char ch;
public static void main(String[] args) {
for (i = 1,ch='a'; i <=26 && ch<='z'; i++,ch++)
{
[Link](i +"="+ch);
}

}
}

Common questions

Powered by AI

The program employs a loop that iterates from 1 to 26, synchronously incrementing an integer variable and a character variable starting from 'a'. It assigns each integer a corresponding letter by incrementing both the integer and character in tandem, printing them as paired values .

The Java program swaps two numbers using arithmetic operations: it sums the two numbers, assigns the original value of the first number to the second by subtracting the second from the sum, and then assigns the original value of the second number to the first by subtracting the new value of the second from the sum. This method leverages properties of addition and subtraction to achieve the swap without a temporary variable .

The Fibonacci program computes each term by adding the two preceding numbers, starting from 0 and 1. This iterative process produces a sequence where each number is the sum of the two preceding ones, a pattern famously known and represented as: 0, 1, 1, 2, 3, 5, 8, 13, and so forth .

The Java code for checking if a number is a palindrome demonstrates the use of loops, conditionals, and arithmetic operations. The program reverses the input number by extracting digits using modulo and divides operations, constructs the reverse by accumulating digits, and then compares the reversed number with the original to determine if they are the same, indicating a palindrome .

The sorting approach using nested loops applied in the program has a time complexity of O(n^2), which is inefficient for larger datasets. This is because for each element, the inner loop potentially compares it to every other element, leading to quadratic growth in operations as the array size increases. More efficient algorithms like Merge Sort or Quick Sort, which operate in O(n log n) time, are generally preferred for larger arrays .

The program counts character occurrences by iterating over the string and using a HashMap to record each character's frequency. If a character is already in the map, its count is incremented; if not, it is added with an initial count of 1. This data structure efficiently allows for O(1) average-time complexity for insertion and lookup .

The program determines if a number is an Armstrong number by calculating the sum of each digit raised to the power of three and comparing this sum to the original number. The concept underlying this is that for a number to be Armstrong, each digit cubed must sum to the original number. This specific version checks for three-digit numbers, hence cubes each digit .

Both programs use a loop to extract each digit from the number using modulo and division operations. The digit-counting program increments a counter for each digit found, while the sum-of-digits program maintains a cumulative sum of the digits encountered. The primary similarity is in the digit extraction process, but the difference lies in the subsequent handling—counting versus summing .

The program removes spaces by utilizing the String.replace() method, targeting spaces as the substring to replace with an empty string. This operation shifts subsequent characters forward, compacting the string and preserving other characters' order, effectively consolidating the string without spaces .

For the triangle, the program uses nested loops where the outer loop iterates over the number of lines, and the inner loop prints increasing numbers of asterisks (1, 2, 3,...) to form a left-aligned pattern. For the reverse triangle, the inner loop decreases the number of asterisks as it progresses, printing fewer asterisks per line until just one remains .

You might also like