Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, February 13, 2015

Java program to reverse a given number

Java program to reverse a given number

class Reverse
{
public static void main(String...s)
{
int n,rev=0,i;
n=Integer.parseInt(s[0]);

while(n!=0)
{
i=n%10;
rev=(rev*10)+i;
n/=10;
}

System.out.println("
Reverse="+rev);

}
}
Read more »

Thursday, February 12, 2015

How to Write Compile and Run Your First Java Program

Before running Java programs you need to install and setup java. For installation guide read: Java Installation: Downloading, Installing and Setting Path

How to write and run Java programs?

  • Open notepad and write below program.


class demo
{
                public static void main(String...S)
                {
                                System.out.println("Java Programming");
                }
}

First Java Program

  • Now save the file with name demo.java on desktop. Here you can replace demo with any other name that you want.
  • Then open command prompt (cmd) and type cd desktop and press enter to change current directory to desktop. We changed directory because our program is saved on desktop.

cd desktop

  • To compile java program type javac demo.java and hit enter. Type java demo to run the program. Remember that here we have to compile with program name and execute with class name that we have used in program. The java program name and class name can be different.

compile and run


Understand the Program


Understand the Program


System: Get reference id of the device on which program is running. Here the device is cmd.
out: Store the reference id of device.
println: Throw the output on device.


A.java
A.java
A.java


class A
 {

}
class A
{
public static void main()
{
}
}
compile
Yes
Yes
Yes
.class file
Not created
Created
Created
execute
No
No
Yes
Read more »