Java Programming Example for Practice

49aa65231961cd3b64ec0482ee5ca636_400x400In this chapter you will learn:

  1. Some more java programming example

The main purpose of adding this chapter is providing your some more java programing example. When you completed this chapter you will be familiar with how to write, save, compile and execute java program. What you all need is to do only write this code is in your favorite code editor, save them with same name of class, compile them and finally run them for getting output.

This chapter contains 4 programming example. You don’t need to dive into complexity; just try to learn how to write and execute java program.

Qu 1: Write a program for adding two numbers. Display the output.
Answer
class AddTwoNumbers
{
  public static void main(String[] args)
  {
    int a, b, result;
    a=20;
    b=30;
    result=a+b;
    System.out.println("Add = "+result);
  }
}
Output

D:\JavaProgram>javac AddTwoNumbers.java

D:\JavaProgram>java AddTwoNumbers
Add = 50

D:\JavaProgram> __

 

Qu 2: Write a program for displaying your good name.
Answer
class DisplayName
{
  public static void main(String[] args)
  {
    System.out.println("My Name is Alex");
  }
}
Output

D:\JavaProgram>javac DisplayName.java

D:\JavaProgram>java DisplayName
My Name is Alex

D:\JavaProgram> __

 

Qu 3: Write a program for displaying “Hello friends, How are you?”.
Answer
class HelloFriends
{
  public static void main(String[] args)
  {
    System.out.println("Hello Friends, How are you?");
  }
}
Output

D:\JavaProgram>javac HelloFriends.java

D:\JavaProgram>java HelloFriends
Hello Friends, How are you?

D:\JavaProgram> __

 

Qu 4: Write a program for multiplying 2 numbers.
Answer
class MultiplyNumbers
{
  public static void main(String[] args)
  {
    int result=20 * 30;
    System.out.println(result);
  }
}
Output

D:\JavaProgram>javac MultiplyNumbers.java

D:\JavaProgram>java MultiplyNumbers
600

D:\JavaProgram>

 

Summary

In this chapter you have learned some more programming examples in java. This is only for making you comfortable with java. The next chapter is Variables and Datatypes in which you will learn creating variable with appropriate data types.

Leave a Reply

Your email address will not be published. Required fields are marked *