You are on page 1of 3

Java Programs using Scanner class:

Program 1:

Write a java program to input three integers and find the difference between their sum

and their average.

import java.util.*;

class Difference

public static void main(String ar[])

int a,b,c,sum;

double avg, diff;

Scanner ob = new Scanner(System.in);

System.out.println("Enter three numbers");

a = ob.nextInt();

b = ob.nextInt();

c = ob.nextInt();

sum = a + b + c;

avg = sum / 3.0;

diff = sum - avg;

System.out.println("Sum= "+sum);

System.out.println("Average= "+avg);

System.out.println("Difference between sum and average= "+diff);

}
Program 2:

Write a program to input the length, breadth and height of a cuboid and find its

Volume and Total Surface Area.

Note: Volume of a cuboid= length*breadth*height

Total Surface Area=2*(length*breadth + breadth*height + height*length)

import java.util.*;

class cuboid

public static void main(String arr[])

Scanner sc=new Scanner(System.in);

float l,b,h,v,a;

System.out.println(“Enter the length:”);

l=sc.nextFloat();

System.out.println(“Enter the breadth:”);

b=sc.nextFloat();

System.out.println(“Enter the height:”);

h=sc.nextFloat();

v=l*b*h;

a=2*(l*b + b*h + h*l);

System.out.println(“Volume= ”+v);

System.out.println(“Area= ”+a);

}
Program 3:

Write a program to input time in seconds. Display the time after converting them into

hours, minutes and seconds.

Sample Input: Time in seconds: 5420

Sample Output: 1 Hour 30 Minutes 20 Seconds

import java.util.*;

class Time

public static void main(String ar[])

int secs, hrs, mins, rsecs;

Scanner sc = new Scanner(System.in);

System.out.println("Enter time in seconds");

secs = sc.nextInt();

hrs = secs / 3600;

mins = (secs % 3600) / 60;

rsecs = (secs % 3600) % 60;

System.out.println(hrs+" Hours "+mins+" Minutes "+rsecs+" Seconds");

You might also like