1. Write a program of Fibonacci series.
public class Fibbonacci {
public static void main(String args[]){
int n1=0, n2=1, i, n3, count=10;
System.out.print(n1 + " " + n2);// printing 0 and 1
for (i=2; i<count; ++i )
{
n3=n1+n2;
System.out.print(" " +n3);
n1=n2;
n2=n3;
}
}
}
//output : 0 1 1 2 3 5 8 13 21 34
2. Write a program of Palindrome Number.
It is a number that is same after reverse.
class PalindromeTest{
public static void main(String args[]){
int r,sum=0,tem;
int n=545;//It is the number variable to be checked for palindrome
temp=n;
while(n>0){
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
3. Write a program of factorial number.
public class Factorial {
public static void main(String aa[]){
int i, fact=1,number=5;
for(i=1; i<=number; i++){
fact=fact*i;
}
System.out.println("factorial of number "+ number + " is:" + fact);
}
}
4. Write a program of reverse a string.
public class StringCharReverse {
public static void main(String[] args) {
String s1="Hello Java";
char[] c= s1.toCharArray(); // toCharArray() is a method to return array of char.
for(int i=c.length-1; i>=0; i--){
System.out.println(c[i]);
}
}
}
public class Fibbonacci {
public static void main(String args[]){
int n1=0, n2=1, i, n3, count=10;
System.out.print(n1 + " " + n2);// printing 0 and 1
for (i=2; i<count; ++i )
{
n3=n1+n2;
System.out.print(" " +n3);
n1=n2;
n2=n3;
}
}
}
//output : 0 1 1 2 3 5 8 13 21 34
2. Write a program of Palindrome Number.
It is a number that is same after reverse.
class PalindromeTest{
public static void main(String args[]){
int r,sum=0,tem;
int n=545;//It is the number variable to be checked for palindrome
temp=n;
while(n>0){
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
3. Write a program of factorial number.
public class Factorial {
public static void main(String aa[]){
int i, fact=1,number=5;
for(i=1; i<=number; i++){
fact=fact*i;
}
System.out.println("factorial of number "+ number + " is:" + fact);
}
}
4. Write a program of reverse a string.
public class StringCharReverse {
public static void main(String[] args) {
String s1="Hello Java";
char[] c= s1.toCharArray(); // toCharArray() is a method to return array of char.
for(int i=c.length-1; i>=0; i--){
System.out.println(c[i]);
}
}
}
5. Write a program to find the length of a string.
Method 1:
public class String_Length_Test {
public static void main(String[] args) {
String str = "I live in Pune";
String[] s2=str.trim().split(" ");
System.out.println(s2.length);
}
}
Method 2:
public class String_Length_Test2 {
public static void main(String[] args) {
System.out.println("Enter a string:");
Scanner sc=new Scanner(System.in);
String s1=sc.nextLine();
int word = 1;
for(int i = 0; i<=s1.length()-1; i++){
if(s1.charAt(i)==' ' && s1.charAt(i+1)!= ' '){
word++;
}
}
System.out.println(word);
}
}
Want star recent high toward interview bed. Subject child majority inside.news today live
ReplyDelete