Gadgets
Showing posts with label core java. Show all posts
Showing posts with label core java. Show all posts
Monday, 25 January 2016
Monday, 14 December 2015
Check the given string is palindrome or not
Check
the given string is palindrome or not:
If we reverse a string that reverse string and
original string both are equal then that string is palindrome.
Example:
Original string: java
Reverse string: avaj
Both are not equal so not palindrome.
Original string: madam
Reverse string: madam
Both are equal so palindrome.
length():- length() is final method, it is applicable for
String Objects. length() method returns number of characters present in the
String.
chatAt() method:-
Returns the char value at the specified index. An
index ranges from 0 to length()-1. The first char value of the sequence is at
index 0, the next at index 1, and so on, as for array indexing.
import java.util.Scanner;
public class PalindromeTest {
public static void main(String[] arg){
String name,rev="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter Name :");
name=sc.nextLine();
System.out.println("length of the String : "+name.length());
int i=name.length();//i=string length
for(int j=i-1;j>=0;j--){
rev=rev+name.charAt(j);
}
System.out.println("reverse string is : "+rev);
}
}
/*
Output:-
Enter Name :
java
length of the String : 4
reverse string is : avaj
*/
import java.util.Scanner;
public class PalindromeTest {
public static void main(String[] arg){
String name,rev="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter Name :");
name=sc.nextLine();
System.out.println("length of the String : "+name.length());
int i=name.length();//i=string length
for(int j=i-1;j>=0;j--){
rev=rev+name.charAt(j);
}
System.out.println("reverse string is : "+rev);
if(name.equals(rev)){
/* check given string and reverse string are equal or not*/
System.out.println("palindrome ");
}
else{
System.out.println("not palindrome ");
}
}
}
/*
Output-1:-
Enter Name :
java
length of the String : 4
reverse string is : avaj
not palindrome
Output-2:-
Enter Name :
madam
length of the String : 5
reverse string is : madam
palindrome
*/
Sunday, 13 December 2015
find length of the string in java
To find length of the string we use length()
method
length():- length() is final method, it is applicable for
String Objects. Length() method returns number of characters present in the
String.
import java.util.Scanner;
public class PalindromeTest {
public static void main(String[] arg){
Scanner sc=new Scanner(System.in);
System.out.println("Enter Name :");
String name=sc.nextLine();
System.out.println("length of the String : "+name.length());
/*
length() is final method, it is useful to find the length of the string
*/
}
}
/*
Enter Name :
java
length of the String : 4
*/
length vs length() in java
length:-
length
is final variable, it is applicable for arrays and it returns the size of an
array
Example:
int[ ] a={10,20,30};
System.out.print(a.length);
Here the above array size is 3.
In this case if we give length() instead of
a.length then we will get compile time error.
length():- length() is final method, it is applicable for
String Objects. Length() method returns number of characters present in the
String.
Example:
String s="java";
System.out.print(s.length());
Here the above String length is 4(j,a,v,a)
Example:
String[ ] s={"java","html5","css"};
System.out.println(s.length);//3
System.out.println(s.length());//error
System.out.println(s[0].length());//4
System.out.println(s[0].length);//error
In the above example
‘s’ is an array and s[0] is String
s.length and s[0].length() are true
Note: In multi dimensional arrays length variable represents only base size but not total size of an array.
int[ ] a=new int[2][3];
System.out.println(a.length);//2
System.out.println(a[0].length);//3
If we want find total size of multi dimensional array
System.out.println(a[0].length+a[1].length);//3+3=6
Count Even numbers from 1 to n
Here in this program I showed to count the how many even
numbers from 1 to n/given number.
Even numbers from 0 to 20: 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
Total even numbers from 1 to 20 :11
import java.util.Scanner;
public class EvenNumbers {
public static void main(String[] arg){
int number,count=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter any number :");
number=sc.nextInt();
System.out.println("Even Numbers are");
for(int i=0;i<=number;i++){
if(i%2==0){
System.out.print(i+" ");
count++;
}
}
System.out.println();
System.out.println("total even numbers from 1 to "+number+" :"+count);
}
}
/*
Output:
Enter any number :
20
Even Numbers are
2 4 6 8 10 12 14 16 18 20
total even number from 1 to 20 :10
*/
Friday, 11 December 2015
Continue statement in java
Continue statement:
Continue statement is used when we want to
skip the remaining code in the block then we use continue statement.
public class ContinueTest {
public static void main(String arg[]){
for(int i=1;i<=10;i++){
if(i<5){
System.out.println("before continue : "+i);
continue;
}
System.out.println("after continue : "+i);
}
}
}
Thursday, 10 December 2015
Armstrong number in java
A number that is equal to the sum of cube of its
digits
Example:
123=(1*1*1)+(2*2*2)+(3*3*3)
123 != 36 is not equal so it is not Armstrong
371=(3*3*3)+(7*7*7)+(1*1*1)
371==371 is equal, so it is Armstrong
import java.util.Scanner;
public class Armstrong {
public static void main(String[] arg){
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number");
int i=sc.nextInt();
int j=i;
int a,b=0;
while(i>0){
a=i%10;
b=b+(a*a*a);
i=i/10;
}
if(b==j){
System.out.println(j+" is Armstrong number");
}
else{
System.out.println(j+" is not Armstrong number");
}
}
}
/*
Output1:
Enter Number
123
123 is not Armstrong number
Output2:
Enter Number
371
371 is Armstrong number
*/
Palindrome program in java
Check given number is
palindrome or not
If we reverse a number, that reverse number
and original number (given number) both are equal then that number is
palindrome number.
Example:
Given number is 121
After reverse the above number, we get 121
Given number and reverse numbers are equal so
121 is palindrome
123 is not palindrome because, 123 reverse is
321 so both are not equal

import java.util.Scanner;
public class Palindrome {
public static void main(String[] arg){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Number");
int i=sc.nextInt();
int j=i;
int a,rev=0;
while(i>0){
a=i%10;
rev=rev*10+a;
i=i/10;
}
System.out.println("reverse number is :"+rev);
if(j==rev){
System.out.println("given number is palindrome");
}
else{
System.out.println("given number is not palindrome");
}
}
}
/*
Output1:
Enter a Number
123
reverse number is :321
given number is not palindrome
Output2:
Enter a Number
121
reverse number is :121
given number is palindrome
*/
Wednesday, 9 December 2015
Print numbers in triangle format
Print numbers in triangle
format
public class TriangleTest {
public static void main(String[] arg){
int a=1;
for(int i=1;i<=4;i++){
for(int space=i;space<4;space++){
System.out.print(" "); // for print spaces
}
for(int j=1;j<=i;j++){
System.out.print(a+"
");//print number
a++;
}
System.out.println();// for new line
}
}
/*
Output:
1
2 3
4 5 6
7 8 9 10
*/
Print digit sum program in java
Print digit sum
program in java
Here in this program if
we enter any number, calculate and print sum
For example: number is
123
1+2+3=6, sum is 6
import java.util.Scanner;
public class DigitSum {
public static void main(String[] arg){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a digit : ");
int num=sc.nextInt();
int sum=0,a;
System.out.print(num +" sum is ");
while(num > 0 ) {
a = num % 10;
sum = sum + a ;
num = num / 10 ;
}
System.out.println(sum);
}
}
/*
Output:
Enter a digit :
1234
1234 sum is 10
*/
Print sum of digits from 1 to n program in java
Print sum of digits
from 1 to n program in java
The main aim of this
program is print sum of digits, for example if we want calculate sum of digit 5
we can calculate like this way 1+2+3+4+5=15
Sum of 10
digit=1+2+3+4+5+6+7+8+9+10=65
import java.util.Scanner;
public class SumOfDigit {
public static void main(String[] arg){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a digit :");
int num=sc.nextInt();
int sum=0;
for(int i=1;i<=num;i++){
sum+=i;//(or) sum=sum+i;
}
System.out.println("sum of digits from 1 to "+num +" : "+sum);
}
}
Subscribe to:
Comments (Atom)











