Oop Laab Task 1
Oop Laab Task 1
Question no 1:
Write a Java program to print 'Hello' on screen and your name on a separate line.
Solution:
public class Main
Question no 2:
Write a Java program to input name from user while taking first user name and
second user name separately but displace in single line.
Solution:
import java.util.Scanner;
System.out.println();
pg. 1
}
Question no 3:
Write a Java program to print the sum of two numbers.
Solution :
public class Exercise2
System.out.println(sum);
Question no 4:
Java code takes two numbers as input from the user, calculates their sum, and then
displays the result
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Input the first number: ");
int num1 = input.nextInt();
System.out.print("Input the second number: ");
int num2 = input.nextInt();
int sum = num1 + num2;
System.out.println();
pg. 2
System.out.println("Sum: " + sum);
}
}
Question no 5:
. Write a Java program to divide two numbers and print them on the screen.
Solution:
public class Exercise3
{
public static void main(String[] args)
{
int result = 50 / 3;
System.out.println(result);
}
}
Question no 6:
Write a Java program that take two takes two integer numbers as input from the
user, performs division, and displays the result.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Input the first number: ");
int a = input.nextInt();
System.out.print("Input the second number: ");
int b = input.nextInt();
int d = (a / b);
pg. 3
System.out.println();
System.out.println("The division of a and b is: " + d);
}
}
Question no 7:
Write a Java program to accept a number and check whether the number is even or
not. Prints 1 if the number is even or 0 if odd.
Sample Output:
Solution:
import java.util.*;
public class Exercise49
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input a number: ");
int n = in.nextInt();
if (n % 2 == 0)
{
System.out.println(1);
} else
{
System.out.println(0);
}
}
}
Question no 8:
Write a Java program to convert seconds to hours, minutes and seconds.
Sample Output
Solution:
pg. 4
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();
}
}
pg. 5