Skip to main content

JAVA : Practice Programs


// bye -> short -> int -> float -> long -> double


// Program to print a string

public class java_main
{
    public static void main(String args[])
    {
     
        System.out.println("Hello my name is Animesh ");
    }
}



// Program to get input from user(input Username)

import java.util.Scanner;

public class java_main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        String b=sc.nextInt();
        System.out.println("Entered value of b is " + b);
    }
}



// Program to find sum of two integer

import java.util.Scanner;

public class java_main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        System.out.println("The sum of a and b is " + (a+b));
    }
}


// Program to find Product of two integer

import java.util.Scanner;

public class java_main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        int product =a*b;
        System.out.println("The product of a and b is " + product);
    }
}



// Program to find Area of circle

import java.util.Scanner;

public class java_main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        float rad=sc.nextFloat();
        // int b=sc.nextInt();
        float area =3.14f*rad*rad;
        System.out.println("The cicle area is " + area);
    }
}



// Program to find person can vote or not

import java.util.Scanner;
public class java_main
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter age of person to check he can vote or not : ");
        int age=sc.nextInt();
        if(age<18)
        {
            System.out.println("not able to vote");
        }
        else
        {
            System.out.println("Able to vote");
        }
    }
}



// Create a calculator to perform basic arithmetic operations.

import java.util.Scanner;

public class java_main
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter 1st Number : ");
        int a=sc.nextInt();
        System.out.print("Enter 2nd Number : ");
        int b=sc.nextInt();
        int c;
        System.out.print("Enter operator : ");
        char operator =sc.next().charAt(0);

        switch(operator)
        {
            case '+' :
                c=a+b;
                System.out.println(c);
                break;
            case '-' :
                c=a-b;
                System.out.println(c);
                break;
            case '*' :
                c=a*b;
                System.out.println(c);
                break;
            case '/' :
                c=a/b;
                System.out.println(c);
                break;
            case '%' :
                c=a%b;
                System.out.println(c);
                break;

            default:
                System.out.println("Wrong Input");
        }
    }



// Program to add to number

public class java_main
{
    public static void main(String arg[])
    {
        int c;
        System.out.println("Hello World");
        Scanner scj=new Scanner(System.in);
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        c=a+b;
        System.out.println(c);
    }
}



// Program to find area of circle from given data

public class java_main
{
    public static void main(String arg[])
    {
       
        float area,r;
        System.out.print("Enter radius of circle :");
        Scanner sc=new Scanner(System.in);
        r=sc.nextInt();
        area=3.14f*(r*r);
        System.out.print("Area of Circle is = ");
        System.out.println(area);
    }
}



// Program to find diffremce of two number

public class java_main {
    public static void main(String arg[]) {
        int a,b;
        Scanner sc= new Scanner(System.in);
        System.out.print("Enter Value of A :");
        a=sc.nextInt();
        System.out.print("Enter value of B :");
        b=sc.nextInt();
        if(a>=b){
            System.out.print("A is greater");
        }else{
            System.out.print("B is greater");
        }
    }
}



// Program to find given number is even or odd

public class java_main {
    public static void main(String arg[]) {
        int num;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter Number : ");
        num=sc.nextInt();
        // Condition Check
        if(num%2==0) {
            System.out.println("Number is Even");
        } else {
            System.out.println("Number is Odd");
        }
    }
}
    // We can also write it using Ternary Operator
public class java_main {
    public static void main(String arg[]) {
        int num;String Type;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter Number : ");
        num=sc.nextInt();
        // Condition Check
        Type=(num%2==0)?"Even":"Odd";
        System.out.println("Number is "+ Type);
       
    }
}



// Income Tax Calculator

public class java_main {
    public static void main(String arg[]) {
        int tax,amt;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the amount :");
        amt=sc.nextInt();
        if(amt>=0 && amt<=500000){
            tax=0;
        }else if(amt<500000 && amt<=1000000){
            tax=(int) (amt*0.2);
        }else{
            tax=(int) (amt*0.3);
        }
        System.out.println("Tax amount in one year is : " + tax);
        System.out.println("Tax amount per month is :" + (tax/12));
    }
}



// Program to find greatest of three numbers

public class java_main{
    public static void main(String args[]){
        int a,b,c;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter vlaue of A ");
        a=sc.nextInt();
        System.out.print("Enter value of B ");
        b=sc.nextInt();
        System.out.print("Enter value of C ");
        c=sc.nextInt();
        if(a>=b && a>=c){
            System.out.println("A is greatest");
        }else if(b>=c){
            System.out.println("B is gratest");
        }else{
            System.out.println("C is gratest");
        }
    }
}



// Program to check Student is Pass or Fail

public class java_main {
    public static void main(String arg[]) {
        int m;
        String r;
        Scanner sc=new Scanner(System.in);
        System.out.print("Eneter marks ");
        m=sc.nextInt();
        r=(m>=33)?"pass":"fail";
        System.out.println("result is "+ r);
    }
}



// Program to print n natural Number

public class java_main {
    public static void main(String arg[]) {
        int r,c=1;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter Range to print(from one)");
        r=sc.nextInt();
        while(c<=r){
            System.out.print(c + " ");
            c++;
        }
    }
}

// Program to find sum of n natural number

public class java_main {
    public static void main(String arg[]) {
        int r,s=0,c=1;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter Range ");
        r=sc.nextInt();
        while(c<=r){
            // System.out.print(s + " ");
            s=s+c;
            c++;
        }
        System.out.println(s );
    }
}

// Program to print Square Pattern

public class java_main {
    public static void main(String arg[]) {
        for(int i=0;i<4;i++) {
            System.out.println("* * * *");
        }
    }
}


// Program to print Reverse of a number

public class java_main {
    public static void main(String arg[]) {
        Scanner sc=new Scanner(System.in);
        int num=sc.nextInt();
        while(num>0) {
            int ld=num%10;
            System.out.print(ld + " ");
            num=num/10;
        }
        // System.out.print();
    }
}



//Program to find No. of zeros in last.

import java.util.Scanner;

public class java_main
{
    static int checka(int a)
    {
        if(a%10==0){
            return 1+ checka(a/10);
        }
        else
            return 0;
    }
    public static void main(String args[])
    {
        int a=1007000;
        Scanner sc=new Scanner(System.in);
        a=sc.nextInt();
        System.out.println(checka(a));
    }
}





























Popular posts from this blog

C++ : Calculator

  # include < iostream > using namespace std ; int main () {     int a , b ;     char o ;     float c ;     cout << " Enter Value of A \n " ;     cin >> a ;     cout << " Enter Value of B \n " ;     cin >> b ;     cout << " Enter Operation \n " << " + for Addition \n - for substration \n * for multiplication \n / for division \n " ;     cin >> o ;     switch ( o )     {         case ' + ' :       {            c = a + b ;             break ;       }       case ' - ' :       {           c = a - b ;           break ;       }       case ' / ' :       {         ...

C++ : Pointer Program

# include < iostream > using namespace std ; int main () {     int a = 4 ;     int * b =& a ;     // & is address of operator     cout << " Thr address of a is " << & a << endl ;     cout << " Thr address of a is " << b << endl ;     // * is value at dereference operator     cout << " Thr value of a is " << * b << endl ;           // Pointer to pointer     int ** c =& b ; cout << " The adress of b is " << & b << endl ;     cout << " The adress of b is " << c << endl ;     cout << " The value at address c is " << * c << endl ;     cout << " The value at address value_at (value_at( c )) is " << ** c << endl ;     } OUTPUT =>           Thr address of...

Python : Small Projects (CWH)

  # Project 1 : Snake, Water, Game OR Rock,Paper,Scissor import random def game ( comp , you ) :     if comp == you :         return 2     elif comp == " s " :         if you == " w " :             return 0         elif you == " g " :             return 1     elif comp == " w " :         if you == " g " :             return 0         elif you == " s " :             return 1     elif comp == " g " :         if you == " w " :             return 0         elif you == " s " :             return 1 print ( " Computer turn : Snake(s),Water(w) or Gun(g) " ) randno = random . randint ( 1 , 3 ) if randno == 1 :  ...