Java Assignment - 01
Name - Sohail alam
Branch – CSE , B tech
Enrollment no. – 0157CS211176
Class roll no. - 62
Question 1 - write a java program to convert a given integer (in seconds) to hours, minutes
and seconds.
Answer - import [Link].*;
public class Main {
public static void main(String[] args)
{
Scanner in = new Scanner([Link]);
[Link]("Input seconds: ");
int seconds = [Link]();
int S = seconds % 60;
int H = seconds / 60;
int M = H % 60;
H = H / 60;
[Link]( H + ":" + M + ":" + S);
[Link]("\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
Answer - import [Link];
public class Main
{
public static void main(String args[])
{
int m, year, month, day;
Scanner s = new Scanner([Link]);
[Link]("Enter the number of days:");
m = [Link]();
year = m / 365;
m = m % 365;
[Link]("No. of years:"+year);
month = m /30;
m = m % 30;
[Link]("No. of months:"+month);
day = m;
[Link]("No. of days:"+day);
}
}
Output
Question 3 - Write a java program that read 5 numbers and sum of all odd values between
them
Answer - import [Link];
public class Main
{
public static void main(String[] args)
{
int n, sumO = 0;
Scanner s = new Scanner([Link]);
[Link]("Enter the number of elements in array:");
n = [Link]();
int[] a = new int[n];
[Link]("Enter the elements of the array:");
for(int i = 0; i < n; i++)
{
a[i] = [Link]();
}
for(int i = 0; i < n; i++)
{
if(a[i] % 2 != 0)
{
sumO = sumO + a[i];
}
}
[Link]("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 [Link];
public class Main
{
public static void main (String args[])
{
int first ,second;
[Link] ("Enter the first number");
first= new Scanner ([Link]).nextInt();
[Link] ("Enter the Second number");
second = new Scanner ([Link]).nextInt();
if (second%first == 0 ){
[Link]("Multiplies");
}else {
[Link]("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 .
Answer - import [Link];
public class Main {
public static void main(String args[]) {
[Link]("Enter a number between 1 to 12 to get months name: ");
switch(new Scanner([Link]).nextInt()){
case 1:
[Link]("January");
break;
case 2:
[Link]("Febuary");
break;
case 3:
[Link]("March");
break;
case 4:
[Link]("April");
break;
case 5:
[Link]("May");
break;
case 6:
[Link]("June");
break;
case 7:
[Link]("July");
break;
case 8:
[Link]("August");
break;
case 9:
[Link]("September");
break;
case 10:
[Link]("October");
break;
case 11:
[Link]("November");
break;
case 12:
[Link]("December");
break;
default:
[Link]("Invalid Input");
}
}
}
Output
Question 6 - Write a java program that read 5 numbers and counts the
number of positive numbers and negative numbers.
Answer - import [Link];
public class Main {
public static void main(String args[]) {
int positive=0,negative=0;
for(int i = 1;i<=5;i++)
{
[Link]("enter the " + i + " number: ");
int num = new Scanner([Link]).nextInt();
if(num>=0)
positive++;
else
negative++;
}
[Link]("Number of positive numbers: " + positive);
[Link]("Number of negative numbers: " + negative);
}
}
Output
Question 7 - Write a program that read 5 numbers and counts the number of
positive numbers and print the average of all positive values.
Answer - import [Link];
public class Main {
public static void main(String args[]) {
int positive=0,negative=0,sum=0;
for(int i = 1;i<=5;i++)
{
[Link]("enter the " + i + " number: ");
int num = new Scanner([Link]).nextInt();
if(num>=0){
positive++;
sum += num;
}
}
[Link]("Number of positive numbers: " + positive);
[Link]("Average value of the said positive numbers: " +
sum/positive);
}
}
Output
Question 8 – Write a java program that read 5 number and sum of all odd
values between them.
Answer -
import [Link];
public class Main {
public static void main(String args[]) {
int positive=0,negative=0,sum=0;
for(int i = 1;i<=5;i++)
{
[Link]("enter the " + i + " number: ");
int num = new Scanner([Link]).nextInt();
if(num % 2 == 1 ){
sum += num;
}
}
[Link]("Sum of all odd values: " + sum);
}
}
Output
Question 9 - Write java program that converts Centigrade to Fahrenheit.
Answer - import [Link];
public class Main {
public static void main(String args[]) {
float Fahrenheit, Celsius;
[Link]("Enter the temperature in celcius: ");
Celsius= new Scanner([Link]).nextFloat();
Fahrenheit =((Celsius*9)/5)+32;
[Link]("Temperature in Fahrenheit is: "+Fahrenheit);
}
}
Output
Question 10 – Write a java program that converts Kilo meters per hour to miles
per hour.
Answer -
import [Link];
public class Main {
public static void main(String args[]) {
double kilometers;
[Link]("Please enter kilometers: ");
kilometers = new Scanner([Link]).nextDouble();
double miles = kilometers / 1.6;
[Link](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 [Link];
public class Main {
public static void main(String args[]) {
int first,second;
[Link]("Enter the first integer: ");
first = new Scanner([Link]).nextInt();
[Link]("Enter the second integer: ");
second = new Scanner([Link]).nextInt();
if ((first + second) == 30 || first == 30 || second == 30) {
[Link]("true");
} else {
[Link](false);
}
}
}
Question 12 - Write a java program that takes hours and minutes as input, and
calculates the total number of minutes.
Answer –
import [Link];
public class Main {
public static void main(String args[]) {
int hours, minutes, totalMinutes;
[Link]("Enter the hours: ");
hours = new Scanner([Link]).nextInt();
[Link]("Enter the minutes: ");
minutes = new Scanner([Link]).nextInt();
totalMinutes = hours * 60 + minutes ;
[Link]("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 [Link];
public class Main {
public static void main(String args[]) {
int numerator,denominator;
[Link]("Enter the numerator: ");
numerator = new Scanner([Link]).nextInt();
[Link]("Enter the denominator: ");
denominator = new Scanner([Link]).nextInt();
[Link]("quotient are: " + numerator/denominator);
[Link]("Remainder are: " + numerator % denominator);
}
}