Java Assignment
Java Assignment
Question 1 - write a java program to convert a given integer (in seconds) to hours, minutes
and seconds.
Answer - import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input seconds: ");
int seconds = in.nextInt();
int S = seconds % 60;
int H = seconds / 60;
int M = H % 60;
H = H / 60;
System.out.print( H + ":" + M + ":" + S);
System.out.print("\n");
}
}
Output
Question 2 - Write a java program to convert a given integer ( in days ) to years, months
and days assumes that all months have 30 days and all years have 365 days. Test Data : Input
no. of days: 2535
}
System.out.println("Sum of Odd Numbers:"+sumO);
}
}
Output
Question 4 - Write a java program that reads two integers and checks whether
they are multiplied or not
Answer - import java.util.Scanner;
public class Main
{
public static void main (String args[])
{
}else {
System.out.println("Not Multiplies");
}
}
}
Output
Question 5 - Write a java program that reads an integer between 1 and 12 and
print the month of the year in English .
Question 8 – Write a java program that read 5 number and sum of all odd
values between them.
Answer -
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int positive=0,negative=0,sum=0;
for(int i = 1;i<=5;i++)
{
System.out.print("enter the " + i + " number: ");
int num = new Scanner(System.in).nextInt();
if(num % 2 == 1 ){
sum += num;
}
}
System.out.println("Sum of all odd values: " + sum);
}
}
Output
Question 9 - Write java program that converts Centigrade to Fahrenheit.
Answer - import java.util.Scanner;
public class Main {
public static void main(String args[]) {
float Fahrenheit, Celsius;
System.out.print("Enter the temperature in celcius: ");
Celsius= new Scanner(System.in).nextFloat();
Fahrenheit =((Celsius*9)/5)+32;
System.out.println("Temperature in Fahrenheit is: "+Fahrenheit);
}
}
Output
Question 10 – Write a java program that converts Kilo meters per hour to miles
per hour.
Answer -
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
double kilometers;
System.out.print("Please enter kilometers: ");
kilometers = new Scanner(System.in).nextDouble();
double miles = kilometers / 1.6;
System.out.println(miles + " Miles");
}
}
output
Question 11 - Write a java program to check two given integers, and print
true if one of them is 30 or if their sum 30 else print false.
Answer -
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int first,second;
System.out.print("Enter the first integer: ");
first = new Scanner(System.in).nextInt();
System.out.print("Enter the second integer: ");
second = new Scanner(System.in).nextInt();
if ((first + second) == 30 || first == 30 || second == 30) {
System.out.println("true");
} else {
System.out.println(false);
}
}
}
Question 12 - Write a java program that takes hours and minutes as input, and
calculates the total number of minutes.
Answer –
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int hours, minutes, totalMinutes;
System.out.print("Enter the hours: ");
hours = new Scanner(System.in).nextInt();
System.out.print("Enter the minutes: ");
minutes = new Scanner(System.in).nextInt();
totalMinutes = hours * 60 + minutes ;
System.out.println("Total minutes are : "+ totalMinutes);
}
}
Question 13 - Write a java program to integral quotient and remainder of a
division Input numerator: 2500 Input denominator: 235 quotient = 10,
remainder = 15.
Answer -
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int numerator,denominator;
System.out.print("Enter the numerator: ");
numerator = new Scanner(System.in).nextInt();
System.out.print("Enter the denominator: ");
denominator = new Scanner(System.in).nextInt();
System.out.println("quotient are: " + numerator/denominator);
System.out.println("Remainder are: " + numerator % denominator);
}
}