Palindrome number algorithm
- Get the number to check for palindrome
- Hold the number in temporary variable
- Reverse the number
- Compare the temporary number with reversed number
- If both numbers are same, print "palindrome number"
- Else print "not palindrome number"
- class PalindromeExample{
- public static void main(String args[]){
- int r,sum=0,temp;
- int n=454;//It is the number variable to be checked for palindrome
- temp=n;
- while(n>0){
- r=n%10; //getting remainder
- sum=(sum*10)+r;
- n=n/10;
- }
- if(temp==sum)
- System.out.println("palindrome number ");
- else
- System.out.println("not palindrome");
- }
- }
- out put:
palindrome number
Factorial Program using loop in java
- class FactorialExample{
- public static void main(String args[]){
- int i,fact=1;
- int number=5;//It is the number to calculate factorial
- for(i=1;i<=number;i++){
- fact=fact*i;
- }
- System.out.println("Factorial of "+number+" is: "+fact);
- }
- }
- out put:Factorial of 5 is: 120
Prime Number Program in Java
Prime number in Java: Prime number is a number that is greater than 1 and divided
by 1 or itself. In other words, prime numbers can't be divided by other
numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.
- class PrimeExample{
- public static void main(String args[]){
- int i,m=0,flag=0;
- int n=17;//it is the number to be checked
- m=n/2;
- for(i=2;i<=m;i++){
- if(n%i==0){
- System.out.println("Number is not prime");
- flag=1;
- break;
- }
- }
- if(flag==0)
- System.out.println("Number is prime");
- }
- }
- Output:Number is prime
Armstrong Number in Java: Armstrong number is a number that is equal
to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.
Let's try to understand why 153 is an Armstrong number.
- 153 = (1*1*1)+(5*5*5)+(3*3*3)
- where:
- (1*1*1)=1
- (5*5*5)=125
- (3*3*3)=27
- So:
- 1+125+27=153
- class ArmstrongExample{
- public static void main(String[] args) {
- int c=0,a,temp;
- int n=153;//It is the number to check armstrong
- temp=n;
- while(n>0)
- {
- a=n%10;
- n=n/10;
- c=c+(a*a*a);
- }
- if(temp==c)
- System.out.println("armstrong number");
- else
- System.out.println("Not armstrong number");
- }
Compare String with/without using equals() method.
mport java.util.Scanner;
public class CompareString{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the 1st String: ");
String s1 = in.next();
System.out.println("Enter the 2nd String: ");
String s2 = in.next();
//1st approach without using equals() method
System.out.println("*********compare 1st String ************");
if(s1.length()==s2.length()){
for(int i=0; i<s1.length(); i++){
if(s1.charAt(i)!=s2.charAt(i)){
System.out.println("String "+s1+" is not equal to string "+s2);
break;
}
}
System.out.println("String "+s1+" is equal to string "+s2);
}else{
System.out.println("String "+s1+" is not equal to string "+s2);
}
//2nd approach , just use equals() method
System.out.println("*********compare 2nd String************");
if(s1.equals(s2)){
System.out.println("String "+s1+" is equal to string "+s2);
}else{
System.out.println("String "+s1+" is not equal to string "+s2);
}
}
}
public class CompareString{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the 1st String: ");
String s1 = in.next();
System.out.println("Enter the 2nd String: ");
String s2 = in.next();
//1st approach without using equals() method
System.out.println("*********compare 1st String ************");
if(s1.length()==s2.length()){
for(int i=0; i<s1.length(); i++){
if(s1.charAt(i)!=s2.charAt(i)){
System.out.println("String "+s1+" is not equal to string "+s2);
break;
}
}
System.out.println("String "+s1+" is equal to string "+s2);
}else{
System.out.println("String "+s1+" is not equal to string "+s2);
}
//2nd approach , just use equals() method
System.out.println("*********compare 2nd String************");
if(s1.equals(s2)){
System.out.println("String "+s1+" is equal to string "+s2);
}else{
System.out.println("String "+s1+" is not equal to string "+s2);
}
}
}
OutPut
-------------
Enter the 1st String:
Selenium Webdriver
Enter the 2nd String:
Selenium Webdriver
*********compare 1st String************
String Selenium Webdriver is equal to string Selenium Webdriver
*********compare 2nd String************
String Selenium Webdriver is equal to string Selenium Webdriver
Flyod Trinagle
Note- Floyd Triangle is like
1
2 3
4 5 6
7 8 9 10
------------
Code-
import java.util.Scanner;
public class FloydTriangle{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows which you want in your Floyd Triangle: ");
int r = in.nextInt();
int n=0;
for(int i=0; i<r; i++){
for(int j=0; j<=i; j++){
System.out.print(++n+" ");
}
System.out.println();
}
}
}
Output
Enter the number of rows which you want in your Floyd Triangle:
5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Note- Floyd Triangle is like
1
2 3
4 5 6
7 8 9 10
------------
Code-
import java.util.Scanner;
public class FloydTriangle{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows which you want in your Floyd Triangle: ");
int r = in.nextInt();
int n=0;
for(int i=0; i<r; i++){
for(int j=0; j<=i; j++){
System.out.print(++n+" ");
}
System.out.println();
}
}
}
Output
Enter the number of rows which you want in your Floyd Triangle:
5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15