Home Blog JAVA Basic Code

JAVA Basic Code

0
class hello
{
    public static void main(String args[])
{
     System.out.println(“Hello Java”);
    }
}

=====================================
import java.util.Scanner;
import java.awt.*;
import java.applet.*;
public class number extends Applet
{
public void init()
{
setBavkground(Color.red);
}
public void paint(Geapgics g)
{
int a;
int b;
Scanner sc=new Scanner(System.in);
System.out.println(“1st value”);
a=sc.nexlnt();
System.out.println(“1st value”);
b=sc.nexlnt();
int c=a+b;
System.out.println(“answer”+c);
String s1=”1st value”+String.valueOf(a);
String s2=”Sum”+String.valueOF(C);
g.drawString(s2,100,100);
}
}
——————————–
<html>
<body>
<applet code=”number” width=400 height=400>
</body>
</applet>
</html>

======================================

Java program to swap two numbers

import java.util.Scanner;

class SwapNumbers
{
   public static void main(String args[])
   {
      int x, y, temp;
      System.out.println(“Enter x and y”);
      Scanner in = new Scanner(System.in);
   
      x = in.nextInt();
      y = in.nextInt();
   
      System.out.println(“Before Swappingnx = “+x+”ny = “+y);
   
      temp = x;
      x = y;
      y = temp;
   
      System.out.println(“After Swappingnx = “+x+”ny = “+y);
   }
}

=======================================================================

Program to check number is prime or not

import java.util.Scanner;
class PrimeCheck
{
   public static void main(String args[])
   {
int temp;
boolean isPrime=true;
Scanner scan= new Scanner(System.in);
System.out.println(“Enter any number:”);
//capture the input in an integer
int num=scan.nextInt();
        scan.close();
for(int i=2;i<=num/2;i++)
{
           temp=num%i;
   if(temp==0)
   {
      isPrime=false;
      break;
   }
}
//If isPrime is true then the number is prime else not
if(isPrime)
   System.out.println(num + ” is a Prime Number”);
else
   System.out.println(num + ” is not a Prime Number”);
   }
}
=============================================================
Java Program to Generate Fibonacci Numbers

import java.util.Scanner;
public class Fibonacci 
{
    public static void main(String[] args) 
    {
        int n, a = 0, b = 0, c = 1;
        Scanner s = new Scanner(System.in);
        System.out.print(“Enter value of n:”);
        n = s.nextInt();
        System.out.print(“Fibonacci Series:”);
        for(int i = 1; i <= n; i++)
        {
            a = b;
            b = c;
            c = a + b;
            System.out.print(a+” “);
        }
    }
}
====================================================

Java program to find factorial

import java.util.Scanner;
class Factorial
{
   public static void main(String args[])
   {
      int n, c, fact = 1;
     
      System.out.println(“Enter an integer to calculate it’s factorial”);
      Scanner in = new Scanner(System.in);
     
      n = in.nextInt();
     
      if (n < 0)
         System.out.println(“Number should be non-negative.”);
      else
      {
         for (c = 1; c <= n; c++)
            fact = fact*c;
     
         System.out.println(“Factorial of “+n+” is = “+fact);
      }
   }
}
======================================================
APPLET RUN COMAAND
c:>javac First.java
c:>appletviewer First.java

LEAVE A REPLY

Please enter your comment!
Please enter your name here