0% found this document useful (0 votes)
17 views

Java Assignment

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java Assignment

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

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 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

Answer - import java.util.Scanner;


public class Main
{
public static void main(String args[])
{
int m, year, month, day;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of days:");
m = s.nextInt();
year = m / 365;
m = m % 365;
System.out.println("No. of years:"+year);
month = m /30;
m = m % 30;
System.out.println("No. of months:"+month);
day = m;
System.out.println("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 java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n, sumO = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of elements in array:");
n = s.nextInt();
int[] a = new int[n];
System.out.println("Enter the elements of the array:");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for(int i = 0; i < n; i++)
{
if(a[i] % 2 != 0)
{
sumO = sumO + a[i];
}

}
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[])
{

int first ,second;


System.out.print ("Enter the first number");
first= new Scanner (System.in).nextInt();
System.out.print ("Enter the Second number");
second = new Scanner (System.in).nextInt();
if (second%first == 0 ){
System.out.println("Multiplies");

}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 .

Answer - import java.util.Scanner;


public class Main {
public static void main(String args[]) {
System.out.print("Enter a number between 1 to 12 to get months name: ");
switch(new Scanner(System.in).nextInt()){
case 1:
System.out.println("January");
break;
case 2:
System.out.println("Febuary");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("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 java.util.Scanner;
public class Main {
public static void main(String args[]) {
int positive=0,negative=0;
for(int i = 1;i<=5;i++)
{
System.out.print("enter the " + i + " number: ");
int num = new Scanner(System.in).nextInt();
if(num>=0)
positive++;
else
negative++;
}
System.out.println("Number of positive numbers: " + positive);
System.out.println("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 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>=0){
positive++;
sum += num;
}
}
System.out.println("Number of positive numbers: " + positive);
System.out.println("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 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);
}
}

You might also like