Some basic programs of Java

In this post, we discuss some basic programs of Java.

We used the Eclipse IDE to develop and run these programs. 

1)Addition of two numbers

class addi { 
  public static void main(String ap[]) { 
   int a=10; 
   int b=20; 
   int c=a+b; 
   System.out.println("add is "+c);; 
   } 
 }

2)Addition of any two numbers

import java.util.Scanner;

public class addition {

public static void main(String[] args) {
int a,b,c;
Scanner s = new Scanner(System.in);
System.out.println("enter any two numbers:-");
a=s.nextInt();
b=s.nextInt();
c=a+b;
System.out.println("sum:"+c);
 }
}

3)How to print a<b and a>b

package com.pkg;


import java.util.Scanner;


public class Addition {


public static void main(String[] args) {
// TODO Auto-generated method stub


int a,b,c;


Scanner s=new Scanner(System.in);
System.out.println("Enter 1st no.");
a=s.nextInt();
System.out.println("Enter 2nd no.");
b=s.nextInt();


if(a<b)
{
System.out.println("2nd no. is greater i.e." +b);
}
else if(a>b)
{
System.out.println("1st no. is greater i.e."+a);
}
else
{
System.out.println("Same");
}
}
}

4)Addition, Subtraction, Multiplication, and Division of specified two number

class Calculation {
int z;
public void addition(int x,int y)
{
z = x + y;
System.out.println("the sum of given no is:"+z);
}
public void substraction(int x,int y)
{
  z= x - y;
  System.out.println("the sub of given no is:"+z);
}
}
public class MyCal extends Calculation{
public void multiplication(int x,int y)
{
z = x * y;
System.out.println("the mul of given no is:"+z);
}
public void division(int x,int y)
{
z = x / y;
System.out.println("the div of given no is:"+z);
}

public static void main(String[] args) {
int a=20,b=10;
MyCal demo = new MyCal();
demo.addition(a, b);
demo.substraction(a, b);
demo.multiplication(a, b);
demo.division(a, b);
}
}


5)Reverse string

import java.util.*;

class rev
{
               public static void main(String ap[])
               {
                String str[];
                Scanner sc=new Scanner(System.in);
                System.out.println("Enter the string");
                str=sc.nextLine();
                strrev(Str);
                System.out.println("reverse string is"+str);
               }
}

Note: You can use these codes free. Just copy and paste it into your IDE.

2 Comments

Add a Comment

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