0% found this document useful (0 votes)
12 views5 pages

Oop Laab Task 1

Uploaded by

malikuzair1242
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)
12 views5 pages

Oop Laab Task 1

Uploaded by

malikuzair1242
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
You are on page 1/ 5

Lab task no 1:

Question no 1:
Write a Java program to print 'Hello' on screen and your name on a separate line.

Solution:
public class Main

public static void main(String[] args)

System.out.println(" hello \n BCS 3B");

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;

public class Main

public static void main(String[] args)

Scanner input = new Scanner(System.in);

System.out.print("Input your first name: ");

String fname = input.next();

System.out.print("Input your last name: ");

String lname = input.next();

System.out.println();

System.out.println("Hello \n" + fname + " " + lname);

pg. 1
}

Question no 3:
Write a Java program to print the sum of two numbers.

Solution :
public class Exercise2

public static void main(String[] args)

int sum = 24 + 26;

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

You might also like