Simple programs in JAVA

Simple programs in Java using operators


"Use desktop site in Smart phones to visit our blog in best manner"



Before going to java program, if anyone want to execute a Java program in, they will need to install JDK on the system.

How to install JDK on the system

The link given above will help you to know about how to install JDK on your system



1. Addition of Two numbers


public class addoftwo
{
   public static void main(String[] args)
   {
     int a=30,b=42;
     int c;
     c=a+b;
     System.out.println("Sum of A and B is : " +c);
   }
}



Sum of A and B is : 72


Explanation :

This the code for adding two numbers, which the variable values are embeded with coding. So, there is no need to get the value from the user. Every coder should remember that, in Java file name and class name should be same. Otherwise, the error will be occured. A and B values are 30 and 42 respectively. According to the coding addition of A and B will be stored in the variable C. So, the output will be shown like that.






2. Subtraction of Two numbers


public class suboftwo
{
   public static void main(String[] args)
   {
     int a=30,b=42;
     int c;
     c=a-b;
     System.out.println("Difference between A and B is : " +c);
   }
}



Difference between A and B is : -12


Explanation :

This the code for subtracting to numbers which variable values are embeded with coding. A and B values are 30 and 42 respectively. According to the coding difference between A and B will be stored in the variable C. So, the output will be shown like that.






3. Multiplication of two numbers using Scanner


import java.util.Scanner;
public class multioftwo
{
   public static void main(String[] args)
   {
     int a,b,c;
     Scanner s= new Scanner(System.in);
     System.out.print("Enter the values of A and B : ");
     a=s.nextInt();
     b=s.nextInt();
     c=a*b;
     System.out.println("Multiplication of A and B is : " +c);
   }
}



Enter the values of A and B : 106 635
Multiplication of A and B is : 67310


Explanation :

This is the code for multipling two numbers which variable values are given by user. A and B values 106 and 635 were respectively given by the user. According to the coding multiplication of A and B will be stored in the variable C. So, the output will be shown like that.

  1. "println" - After printing a statement cursor comes to the next line.
  2. "print"  - After printing a statement cursor won't come to next line





4. Simple formula using Scanner


import java.util.Scanner;
public class formula
{
   public static void main(String[] args)
   {
     int a,b,c;
     Scanner s= new Scanner(System.in);
     System.out.print("Enter the values of A and B : ");
     a=s.nextInt();
     b=s.nextInt();
     c=(a*b)+(b/a);
     System.out.println("Formula : (A*B)+(B/A)");
     System.out.println("Solution for given formula : " +c);
   }
}



Enter the values of A and B : 2 3
Formula : (A*B)+(B/A)
Solution for given formula : 7


Explanation :

A and B values 2 and 3 were respectively given by the user. According to the code solution will be stored in the variable C. So, the output will be shown like that.



I think these examples are useful to create simple java programs. From these examples, we known about

  1. To print a statement
  2. How to save a Java file in your PC
  3. How to use variable which values are embeded with the code
  4. How to get values from the user
  5. Difference between "println" and "print"

In upcoming post, we will see about Control Structures which are in JAVA

Subscribe Knowsomethinks blogspot for more news and coding, the notifications of every post will be updated to you. If you have any doubts on this, you can ask me in contact form which is in menu.

Comments

Popular posts from this blog

Simple PHP programs I

Posters about Villages

Year 2020