0% found this document useful (0 votes)
39 views

Basic Java Programs

The document contains code examples for basic Java programs including: 1) A program to find prime numbers between 1 to 50 by checking if a number is divisible by any number between 2 to itself. 2) A program to reverse a string by iterating through the string from last index to first index. 3) A program to check if a string is a palindrome by reversing the string and comparing it to the original. 4) A program to print a pyramid pattern using nested for loops and print statements.

Uploaded by

lalitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Basic Java Programs

The document contains code examples for basic Java programs including: 1) A program to find prime numbers between 1 to 50 by checking if a number is divisible by any number between 2 to itself. 2) A program to reverse a string by iterating through the string from last index to first index. 3) A program to check if a string is a palindrome by reversing the string and comparing it to the original. 4) A program to print a pyramid pattern using nested for loops and print statements.

Uploaded by

lalitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Basic Programs in JAVA

Prime number
public static void main(String[] args){

int num=50;

int count=0;

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

count=0;

for( int j=2;j<i;j++){

if( i%j==0){

count++;

break;

if(count==0){

System.out.println(i);

String Reversal
public static void main(String[] args){

String str="sairsm";

String revstring="";

for(int i=str.length()-1;i>=0;i--){

revstring+=str.charAt(i);
}

System.out.println(revstring);

}}

Palindrom
Public static void main(String [] args){

String str=sairam;

String revstring=;

for(int i=str.length()-1;i>0;i--){

revstring+=str.charAt(i);

}System.out.println(revstring);}

If(revstring.equalIgnorecase(str)){

System.out.println(Is a palindrom);}

else{

System.out.println(is not a palindrom);}

}s

Pyramid
public static void main(String [] args){

int spaceValue=2;

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

// System.out.println("spaceValue==="+spaceValue);

if(i % 2 ==0){

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

for(int k=1; k<=spaceValue; k++)

System.out.print(" ");

}
System.out.print("*");

spaceValue--;

System.out.print("\n");

// System.out.println(" ");

}}

You might also like