IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Three Solved Problems

2 posters

Go down

Three Solved Problems Empty Three Solved Problems

Post by BIT0112-Rokon 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...
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Three Solved Problems Empty Re: Three Solved Problems

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

you should've posted this the day before exam mate Razz
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Back to top

- Similar topics

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