Three Solved Problems

View previous topic View next topic Go down

Three Solved Problems

Post by BIT0112-Rokon on Mon Dec 14, 2009 6:13 am

1. Write a program that will reverse  a string .String Reverse
Code:
 import java.util.Scanner;

public class StringReverse {
      static Scanner scr = new Scanner(System.in);

            public static String Reverse(String t) {
            String string = "";
            for (int i = t.length() - 1; i >= 0; i--) {
                    string += t.charAt(i);
            }
        return string; 
    }

public static void main(String[] args) {
            System.out.println("Enter your String: ");
            String str = scr.next();
            System.out.println("Your Reverse String is: " + Reverse(str));
       }

}
2. Write Program that will print the Fibonacci series.
Code:
import java.util.Scanner;

public class Fibonacci {
          static Scanner scr = new Scanner(System.in);
          static int num;
                    public static int fib(int n) {
                 if (n < 2) 
                         return n;
                 else 
                         return fib(n - 1) + fib(n - 2);
                   
         }

          public static void main(String[] args) {
                   System.out.println("koto projonta chao...?");
                   num = scr.nextInt();
                   for (int i = 1; i <= num; i++)
                   System.out.print(fib(i) + ", ");
           }
}
3. Write a program that will print factorial of number.
Code:
import java.util.Scanner;

public class Factorial {
         static Scanner scr = new Scanner(System.in);

         public static long factorial(int n) {
                  if (n <= 1)
                        return 1;
                  else 
                        return (n * factorial(n - 1));
         }

         public static void main(String[] args) {
                  int num;
                  System.out.print("enter your number..?");
                  num = scr.nextInt();
                  System.out.print("your factorial: " + factorial(num));

          }
}
Best of luck...

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Re: Three Solved Problems

Post by BIT0122-Amit on Mon Dec 14, 2009 5:18 pm

you should've posted this the day before exam mate Razz

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum