Wednesday, 9 October 2013

JAVA PROGRAMS 2

B)      The Fibonacci sequence is defined by the following rule:
The first two values in the sequence are 1 and 1. Every subsequent value is the sum of the two values preceding it. Write a Java program that uses both recursive and non recursive functions to print the nth value in the Fibonacci sequence.

Program:
import java.io.*;
class Fibo
{
         int nonRecursive(int n1)
         {
                int f3=0,f1=0,f2=1;
                if(n1==0 || n1==1)        return n1;
                else
                {
                        int count=2;
                        while(count<=n1)
                        {
                                f3=f1+f2;
                                f1=f2;
                                f2=f3;
                                count++;
                        }
                        return f3;
                }
         }
         int recursive(int n1)
         {
                if(n1==0 || n1==1)               return n1;
                else                                      return recursive(n1-1)+recursive(n1-2);
         }

         public static void main(String args[]) throws IOException
         {
           int n;
           Fibo ob=new Fibo();
           BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
           System.out.print("\nEnter The Value Of n: ");
           n=Integer.parseInt(br.readLine());
           System.out.println("RECURSIVE METHOD");
           System.out.println(+n+"th Value In Fibonacci Sequence Is:"+ob.recursive(n));
           System.out.println("\nNON RECURSIVE METHOD ");
           System.out.println(+n+"th Value In Fibonacci Sequence Is:"+ob.nonRecursive(n));
          }
}     


Output:
E:\Lab>java Fibo
Enter The Value Of n: 5
RECURSIVE METHOD
5th Value In Fibonacci Sequence Is:5
NON RECURSIVE METHOD

5th Value In Fibonacci Sequence Is:5

JAVA PROGRAMS

a)      Write a Java program that prints all real solutions to the quadratic equation ax2 + bx +c = 0. Read in a, b, c and use the quadratic formula. If the discriminant b2- 4ac is negative, display a message stating that there are no real solutions.

Program:

import java.io.*;
class SolvingEquation
{
        int desc;
       double r1,r2;
       void solution(int a,int b,int c)
       {
           desc=(b*b)-(4*a*c);
           if(desc>0)
           {
                  r1=(-b+Math.sqrt(desc))/(2*a);
                  r2=(-b-Math.sqrt(desc))/(2*a);
                  System.out.print("\nThe Roots Are: "+r1+"\t"+r2);
           }
           else if(desc==0)           System.out.print("\nThe Roots Are Equal:"+(-b/(2*a)));
           else                               System.out.print("\nThere Are No Real Solutions");               
       }
       public static void main(String args[]) throws IOException
       {
           int a,b,c;
           SolvingEquation obj=new SolvingEquation();
           BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
           System.out.print("\nEnter value of a:");
           a=Integer.parseInt(br.readLine());
           System.out.print("\nEnter value of b:");
           b=Integer.parseInt(br.readLine());
           System.out.print("\nEnter value of c:");
           c=Integer.parseInt(br.readLine());
           obj.solution(a,b,c);
        }
}      



Output:
E:\Lab>javac SolvingEquation.java
E:\Lab>javac SolvingEquation.java
Enter value of a:2
Enter value of b:3
Enter value of c:1

The Roots Are: -0.5     -1.0

Tuesday, 8 October 2013

Soon, apply for passport from your phone.......!!!!!!!

CHENNAI: Soon there will be a smart way to apply for passports. The agency of external affairs is in the final stages of introduction an app that will allow people to concern and pay the charge for passports on their mobile phones.

The new app will be an upgrade edition of mPassport Seva that is available for 
Android, iOSand Windows Phone platform. mPassport Seva currently allows users to find general in sequence, locate nearest passport centre and police station, calculate fees and track the status of applications. More features like provision to file application and pay fees will be added toit.

"The capability to help applicants fill passports and make compensation through the app should be available in one to one-and-a-half months," said Golok Kumar Simli, principal consultant and head (technology), ministry of external affairs. Applicants will be able to log in, file the application and track its status but will have to carry hard copies of the documents required to the passport seva kendra after an ARN number is generated and an appointment fixed, he said. Citing security concerns, he, however, refused to explain how the app will work.

Monday, 7 October 2013

Java program to open Notepad

How to open Notepad through java program: Notepad is a text editor which comes with Windows operating system, It is used for creating and editing text files. You may be developing java programs in it but you can also open it using your java code.

import java.util.*;
import java.io.*;
 
class Notepad {
  public static void main(String[] args) {
    Runtime rs = Runtime.getRuntime();
 
    try {
      rs.exec("notepad");
    }
    catch (IOException e) {
      System.out.println(e);
    }   
  }
}

C program to shutdown or turn off computer

C Program to shutdown your computer: This program turn off i.e shutdown your computer system. Firstly it will asks you to shutdown your computer if you press 'y' the your computer will shutdown in 30 seconds, system function of "stdlib.h" is used to run an executable file shutdown.exe which is present in C:\WINDOWS\system32 in Windows XP. You can use various options while executing shutdown.exe for example -s option shutdown the computer after 30 seconds, if you wish to shutdown immediately then you can write "shutdown -s -t 0" as an argument to system function. If you wish to restart your computer then you can write "shutdown -r".
 
#include <stdio.h>
#include <stdlib.h>
 
main()
{
   char ch;
 
   printf("Do you want to shutdown your computer now (y/n)\n");
   scanf("%c",&ch);
 
   if (ch == 'y' || ch == 'Y')
      system("C:\\WINDOWS\\System32\\shutdown -s");
 
   return 0;
}

FREE HIT COUNTERS