core java challenges

1.Write a program to multiply two numbers and display the result.

class multiplication {
    public static void main(String[] args) {
        int a=7;
        int b=77;
        int c=a*b;
        System.out.println("output:\n \t a\t=\t"+a +" \n \t b\t=\t"+b +" \n \t c\t=\t"+(a*b));
    }
}

RESULT:

output:
         a      =       7
         b      =       77
         c      =       539

2.Write a program to divide two numbers and display the quotient.

class Quotient{
    public static void main(String[] args) {
        int a,b,Quotient;
        a=10;
        b=5;
        Quotient=a/b;
        System.out.println("Output:\n \t a\t\t=\t"+a +"\n \t b\t\t=\t"+b +"\n \t Quotient\t=\t"+Quotient);
    }
}

RESULT:

Output:
         a              =       10
         b              =       5
         Quotient       =       2

3.Write a program to find the remainder when one number is divided by another.

class reminder {
    public static void main(String[] args) {
        int a,b,reminder;
        a=22;
        b=5;
        reminder=a%b;
        System.out.println("Output:\n\t a\t\t=\t"+a +"\n\t b\t\t=\t"+b +"\n\t reminder\t=\t"+reminder);
    }
}

RESULT:

Output:
         a              =       22
         b              =       5
         reminder       =       2

4.Write a program to swap two numbers using a temporary variable.

class swapwithtemp{
    public static void main(String[] args) {
      int a=10;
      int b=20;
      System.out.println("Before swapping:");
      System.out.println("a="+a +", b="+b);
      //Swapping using temp variable
      int temp=a;
      a=b;
      b=temp;
      System.out.println("After swapping:");
      System.out.println("a="+a +", b="+b);

    }
}

OUTPUT:

Before swapping:
a=10, b=20
After swapping:
a=20, b=10

5.Write a program to swap two numbers without using a temporary variable.

class swapwithouttemp {
    public static void main(String[] aegs) {
        int a=10,
        b=20;
        System.out.println("Before swapping:\n a="+a +",b="+b);
        //swapping without temp variable
        a=a+b;
        b=a-b;
        a=a-b;
        System.out.println("After swapping:\n a="+a +",b="+b);
    }
}

OUTPUT:

Before swapping:
 a=10,b=20
After swapping:
 a=20,b=10

6.Write a program to calculate the area of a rectangle (length × width).

class areaofrectangle {
    public static void main (String[] arg) {
        Scanner sc= new Scanner(System.in);
        //Declaration
        int length, width, area;
        //initialization
        System.out.println("Enter the length :");
        length=sc.nextInt();
        System.out.println("Enter the width :");
        width=sc.nextInt();
        area=length*width; 
        System.out.println("The area of rectangle is" +area);
        sc.close();
    }
    }

OUTPUT:
Enter the length :
6
Enter the width :
7
The area of rectangle is42

7.Write a program to calculate the perimeter of a rectangle.

import java.util.Scanner;
class perimeterrectangle{
    public static void main (String[] arg) {
        Scanner sc= new Scanner(System.in);
        //Declaration
        int length, width, area;
        //initialization
        System.out.println("Enter the length :");
        length=sc.nextInt();
        System.out.println("Enter the width :");
        width=sc.nextInt();
        area=2*length*width; 
        System.out.println("The perimeter of rectangle is " +area);
        sc.close();
    }
    }

OUTPUT:
Enter the length :
7
Enter the width :
7
The perimeter of rectangle is 98

8.Write a program to calculate the area of a circle (π × r × r).

class areaofcircle {
    public static void  main (String[] args) {
        double radius=7.0;
        double area= Math.PI * radius * radius;
        System.out.println("The circle radius: "+ radius);
        System.out.println("The area of circle is: " +area);
    }
}

OUTPUT:
The circle radius: 7.0
The area of circle is: 153.93804002589985

9.Write a program to calculate the circumference of a circle (2 × π × r).

import java.util.Scanner;
class circumferenceofcircle {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        double circumferencecircle,radius;
        System.out.println("Enter the radius: ");
        radius=sc.nextDouble();
        circumferencecircle=2 * Math.PI * radius;
        System.out.println("The circumference of circle:" + circumferencecircle);
        sc.close();
    }
}

OUTPUT:
Enter the radius:
5.0
The circumference of circle:31.41592653589793

10.Write a program to calculate the average of three numbers.

class average {
    public static void main(String[] args) {
        int num1, num2,num3,total,avg;
        num1=40;
        num2=70;
        num3=90;
        total=num1+num2+num3;
        avg=total/3;
        System.out.println("The average of three numbers is "+avg);
    }
}

OUTPUT:
The average of three numbers is 66

11.Write a program to convert temperature from Celsius to Fahrenheit.

import java.util.Scanner;
class celsiustofarenheit {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        //Declaration
        double celsius ,farenheit ;
        //Initaialization
        System.out.println("Enter the temperature in celsius:");
        celsius=sc.nextDouble();
        farenheit=(celsius*9/5)+32;
        System.out.println("The temperature celsius to farenheit is "+farenheit);
        sc.close();

    }
}

OUTPUT:
Enter the temperature in celsius:
32
The temperature celsius to farenheit is 89.6

12.Write a program to convert temperature from Fahrenheit to Celsius.

class farenheittocelsius {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        //Declaration
        double celsius ,farenheit ;
        //Initaialization
        System.out.println("Enter the temperature in farenheit:");
        farenheit=sc.nextDouble();
        celsius=(farenheit-32)*5/9;
        System.out.println("The temperature farenheit to celsius is "+celsius);
        sc.close();

    }
}
OUTPUT:
Enter the temperature in farenheit:
50
The temperature farenheit to celsius is 10.0

13.Write a program to calculate the square of a number.

import java.util.Scanner;
class squareofnumber {
    public static void main(String[] args){
    Scanne
r sc=new Scanner(System.in);
        int num,square;
        System.out.println("Enter the number:");
        num=sc.nextInt();
        square=num*num;
        System.out.println("The square of   number is: " + square);
        sc.close();

    }
}

OUTPUT:
Enter the number:
6
The square of   number is: 36

14.Write a program to calculate the cube of a number.

import java.util.Scanner;
class cube {
    public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
        int num,cube;
        System.out.println("Enter the number:");
        num=sc.nextInt();
        cube=num*num*num;
        System.out.println("The cube of   number is: " + cube);
        sc.close();
     }
    }

OUTPUT:

Enter the number:
5
The cube of   number is: 125

15.Write a program to add the digits of a 2-digit number (e.g., 47 → 4+7=11).

import java.util.Scanner;
class addtwodigits {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int num, digit1, digit2, sum;
        System.out.println("Enter the two digits number: ");
        num = sc.nextInt();
        digit1 = num / 10; //first digit 
        digit2 = num % 10; //second digit
        sum = digit1 + digit2;
        System.out.println("The sum of digits of " + num + " = " + sum);
    }
}

OUTPUT:
Enter the two digits number:
97
The sum of digits of 97 = 16

16.Write a program to find the simple interest (SI = P × R × T / 100).

import java.util.Scanner;
class simpleinterest {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int P,R,T,SI;
    System.out.println("Enter the principle amount(P): ");
    P=sc.nextInt();
    System.out.println("Enter the rate of interest(R): ");
    R=sc.nextInt();
    System.out.println("Enter time in years(T): ");
    T=sc.nextInt();
    SI= (P * R * T)/100;
    System.out.println("SI = (P*R*T)/100");
    System.out.println("Simple Interest= " + SI);
    }
}

OUTPUT:
Enter the principle amount(P): 
5000
Enter the rate of interest(R):
2
Enter time in years(T):
3
SI = (P*R*T)/100
Simple Interest= 300

17.Write a program to find the total and average marks of 5 subjects.

class reportcard {
public static void main(String[] args) {
    int Tamil=50;
    int English=70;
    int Maths=84;
    int Computer=92;
    int Physics=88;
    int tot,avg;
    tot= Tamil + English + Maths + Computer + Physics;
    avg=tot/5;
    boolean pass = (Tamil>=35 && English>=35 && Maths>=35 && Computer>=35 && Physics>=35);
    System.out.println("----------report card---------");
    System.out.println("Tamil mark   : "+Tamil);
    System.out.println("English mark : "+ English);
    System.out.println("Maths mark   : "+ Maths);
    System.out.println("Computer mark: "+ Computer);
    System.out.println("Physics mark : "+ Physics);
    System.out.println("-------------------------------");
    System.out.println("Total marks  = " + tot);
    System.out.println("Average marks= " + avg);
    if(pass) {
        System.out.println("Result: PASS");
    }else{
        System.out.println("Result: FAIL");
    }
  }
  }  

OUTPUT:
----------report card---------
Tamil mark   : 50
English mark : 70
Maths mark   : 84
Computer mark: 92
Physics mark : 88
-------------------------------
Total marks  = 384
Average marks= 76
Result: PASS

18.Write a program to calculate the monthly salary when daily wage and working days are given.

import java.util.Scanner;
class monthlysalary {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int Dailywage, Workingdays, monthlysalary;
        System.out.println("Enter the Daily wage: ");
         Dailywage=sc.nextInt();
        System.out.println("Enter total working days:");
        Workingdays=sc.nextInt();
        monthlysalary= Dailywage*Workingdays;     
        System.out.println("Monthly salary :" + monthlysalary);
        sc.close();
    }
}

OUTPUT:
Enter the Daily wage:
500
Enter total working days:
25
Monthly salary :12500

19.Write a address to java program.

class address {
    public static void main(String[] args){
        System.out.println(" _____________________________________\n|Name \t\t:\t Devika,      |\n|Street \t:\t North street,|\n|Place \t\t:\t Vanavanallur,| \n|Pin code \t:\t 612 901.     |\n _____________________________________");

    }
}


OUTPUT:
____________________________
|Name           :        Devika,            |
|Street           :        North street,   |
|Place           :        Vanavanallur,  |
|Pin code       :        612 901.         |
 _______________________________

Popular posts from this blog

for and while challenges

html task