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

Java Programming Lab Exercises

Uploaded by

ajay9861gupta
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)
31 views5 pages

Java Programming Lab Exercises

Uploaded by

ajay9861gupta
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

Lab1

1.Write a Java program to print "Hello, World!" to the console.


Program:-
package Lab1;

public class Qus1 {

public static void main(String[] args) {


System.out.println("Hello, world");//to print hello world

}
}

Output:-

2.Write a program to find the sum of two numbers entered by


the user
Program:-
package lab1;
import java.util.Scanner;
public class Qus2 {
public static void main(String[] args) {
int FirstNum,SecendNum,SumTwo;
System.out.print("Enter the first number ");
Scanner n1= new Scanner(System.in);// takein number form user
FirstNum =n1.nextInt();//typecasting
System.out.print("Enter the second number ");
Scanner n2= new Scanner(System.in);
SecendNum =n2.nextInt();//typecasting
SumTwo =FirstNum+SecendNum;
System.out.println("Sum of two number is " + SumTwo);
}
}
Output:-
3.Write a Java program to check whether a given number is even
or odd

Program:-
package lab1;

import java.util.Scanner;

public class qus3 {

public static void main(String[] args) {


int num;
System.out.print("Enter the number ");
Scanner n1= new Scanner(System.in);// takein number form user
num =n1.nextInt();//typecasting
if (num%2 == 0)//checking if even or odd
{
System.out.println("Giving number is even");
}
else
{
System.out.println("Giving number is even");
}
}
}

Output:-

4.Write a java program to find greatest of 3 numbers


Program:-
package lab1;

public class qus4


{

public static void main(String[] args)

{
int a=401, b=78, c=19;

if(a>=b && a>=c)


{
System.out.println(a+" is the largest Number");
}

else if (b>=a && b>=c)


{
System.out.println(b+" is the largest Number");
}
else {
System.out.println(c+" is the largest number");
}
}
}

0utput:-

5.Write a program to implement a basic calculator that


takes input and evaluates it.
Program:-

0utput:-
6.Write a Java program to check if a given number is
prime or not
Program:-
0utput:-

7.Create a Java program that compares two numbers and


prints the larger one.
Program:-
package lab1;

import java.util.Scanner;

public class qus7{


public static void main(String[] args) {
int FirstNum ,SecendNum,add,sub,mu,div;

System.out.print("Enter the first number ");


Scanner n1= new Scanner(System.in);// takein number form user
FirstNum =n1.nextInt();//typecasting
System.out.print("Enter the second number ");
Scanner n2= new Scanner(System.in);
SecendNum =n2.nextInt();//typecasting
if (FirstNum>SecendNum)
{
System.out.print("first number is greater than second
number");
}
else if(FirstNum==SecendNum)
{
System.out.print("first number is equal to second number");
}
else
{
System.out.print("second numberr is greater than first
number");
}
}
}
0utput:-

You might also like