[Go to site: main page, start]

0% found this document useful (0 votes)
11 views21 pages

Java Programs for Common Conversions

The document contains solutions to 13 Java programming questions provided by a student. Each question is followed by the student's answer which provides the code to solve the problem. The questions cover topics like conversion between time formats, mathematical operations, conditional checking, and more.

Uploaded by

sohailalam5539
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views21 pages

Java Programs for Common Conversions

The document contains solutions to 13 Java programming questions provided by a student. Each question is followed by the student's answer which provides the code to solve the problem. The questions cover topics like conversion between time formats, mathematical operations, conditional checking, and more.

Uploaded by

sohailalam5539
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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);
}
}

Common questions

Powered by AI

To print the English name of a month given its integer representation, use a switch-case structure in Java that maps each integer (1 to 12) to its respective month's name, for instance, `case 1: System.out.println("January");` This construct efficiently handles each possible value of the integer input, covering all scenarios .

To determine if one integer is a multiple of another in Java, use the modulus operator to check for a zero remainder. If `second % first == 0`, it indicates that 'second' is a multiple of 'first', printing 'Multiplies' if true, 'Not Multiplies' otherwise .

The conversion from Celsius to Fahrenheit follows the standard formula: `Fahrenheit = (Celsius * 9/5) + 32`. This formula is directly implemented in the program for conversion operations to reflect accurate Fahrenheit results upon Celsius input .

In Java, to check if two integers or their sum equals a specific number (e.g., 30), use a compound condition: `if ((first + second) == 30 || first == 30 || second == 30)`, allowing the program to decide based on whether any individual integer or sum meets the condition for outputting "true" or "false" .

To calculate the integral quotient and remainder of a division in Java, input the numerator and denominator, then use integer division and modulus operations. The integral quotient is calculated using `numerator/denominator`, and the remainder with `numerator % denominator`. This efficiently computes both results using these basic arithmetic operators .

To compute the average of positive numbers from a set of integers, iterate over the integers, check if each is positive, and accumulate their sum and count. The average is the division of this sum by count, ensuring average computation is only on positive integers: `System.out.println("Average of positives: " + (sum / positive));` .

The program assumes that each month is 30 days long and each year is 365 days, simplifying the conversion calculations by removing the variability of different month and year lengths in a regular calendar. This assumption allows the program to directly use division and modulus operations to break down the total days: `year = m / 365; month = m / 30;` .

To convert an integer representing seconds into hours, minutes, and seconds using Java, the key is to first derive seconds, then derive minutes from the leftover seconds, and finally derive hours. The integer division and modulo operation are used to achieve this: 1) Calculate the seconds remainder after dividing by 60; 2) Derive total minutes by integer division by 60; 3) Use modulo to find the leftover minutes from hours; 4) Divide again to find hours. This can be implemented as follows: `int S = seconds % 60; int H = seconds / 60; int M = H % 60; H = H / 60;`

The program assesses integer positivity or negativity within a loop, checking each integer input: The condition `if (num >= 0)` identifies positive numbers increasing their count with `positive++`, otherwise increments the negative count using `else negative++`. This loop efficiently separates and counts each integer category .

To convert kilometers per hour to miles per hour in Java, apply the conversion rate directly: divide the kilometers per hour (`kilometers`) by 1.6 to obtain miles per hour, e.g., `double miles = kilometers / 1.6;` This transformation scales the speed appropriately based on the conversion rate .

You might also like